了解 Java 中的方法/class 调用

Understanding method / class calls in Java

我正在尝试制作一款非常基本的 Black Jack 游戏,我将其分为三个简单的 类,但我对 Java 非常陌生,而且我是一个糟糕的编码员,但我需要学习这个工作。我很难理解我应该如何调用方法和 类。我想出了游戏的基本结构,比如如何创建卡牌以及如何进入游戏和退出游戏。我只是无法弄清楚游戏本身。这是我到目前为止所创建的。请提供任何建议和指导,这样我才能理解这太好了,我很绝望。

BlackJack.java

import java.util.*;

public class BlackJack4 {

    public static void main(String[] args) {
    // write your code here

        Scanner keyboard = new Scanner(System.in);
        Scanner scan = new Scanner(System.in);

        String playGame = "";

        System.out.print("Wanna play some BlackJack? \n");
        System.out.print("Type yes or no \n");

        playGame = keyboard.nextLine();

        if (playGame.equals ("yes"))
        {
            /*
            This is the area I need help in figuring out. 
             */
        }
        else if (playGame.equals("no")) //Player decided to no play the game
        {
            System.out.print("Thank you for playing with us today.\n");
            System.out.print("To exit the game please press enter.");
            scan.nextLine();
            System.exit(0);
        }
        else
        {
            System.out.print("Sorry you did something wrong. Please try again.\n");
            System.out.print("To exit the game please press enter.");
            scan.nextLine();
            System.exit(0);
        }
    }
}

Deck.java

import java.util.*;

public class Deck {
    ArrayList<Cards> cards = new ArrayList<Cards>();

    String[] Suits = { "Clubs", "Diamonds", "Hearts", "Spades"};
    String[] Ranks = {null, "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

    public Deck() {
        int d = Suits.length * Ranks.length;

        String[] deck = new String[d];
        for (int i = 0; i < Ranks.length; i++) {
            for (int j = 0; j < Suits.length; j++) {
                deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];
            }
        }
    }

    public void shuffle(){//shuffle the deck when its created
        Collections.shuffle(this.cards);
    }
}

Cards.java

public class Cards {
    private String suit;
    private String rank;

    public Cards(){}

    public Cards(String suit, String rank){
        this.suit = suit;
        this.rank = rank;
    }

    public  String getSuit(){
        return suit;
    }
    /* public void setSuit(String suit){
        this.suit = suit;
    }*/
    public String getRank(){
        return rank;
    }
    /*
    public void setRank(String value){
        this.rank = rank;
    }*/
    @Override
    public String toString() {
        return String.format("%s of %s", rank, suit);
    }
}

现在我是 Java 的新手,所以我假设会有更多知识的人出现并能够为您提供更多帮助,但这就是我现在所拥有的:

1) 我建议您删除 public Cards() {} constructor,因为您可能会不小心创建无效的卡片(没有设置任何属性)。

2) 如评论所述,我会做一个 while 循环来检查是否按下了某个键,然后执行相关操作。

while (gameIsRunning) {
   if (key1 is pressed) {
   ...
   }

   ...

   if (key10 is pressed) {
   ...
   System.out.println("You won! Game over.");
   gameIsRunning = false;
   }
}

我会在class或方法的某个地方定义一轮游戏的过程,或者为它编写所有的辅助方法,然后将它们扔到这个循环结构中。 或者,您可以用文本提示用户并等待文本输入以进行下一步操作。

希望能帮到你!

这种任务不是很容易的水平,这里有很多事情要做,也许你必须从一些更基础的东西开始。但是如果你确定你想通过,这里有一些建议和代码: 您必须将 score 字段添加到 Card class(对于 class,单个名称优于复数名称,变量的首字母小写,这是一些代码约定)。不容易,因为 A 可以具有多值。 使用 LinkedList 而不是 ArrayList 作为 Deck.cards。发牌者每次轮询一张牌,更接近真实游戏。

this.cards.add(cards); 代替 'deck[Suits.length * i + j] = Ranks[i] + " of " + Suits[j];' 你根本不需要这个字符串数组。最重要的部分是你可以把骨架放在你指的地方,那里是最需要帮助的地方:


    private static void playGame(Scanner scan) {
        Deck deck = new Deck();
        String playGame;
        Integer yourScore = 0;
        Random random = new Random();
        Boolean playerEnough = false;
        Boolean dealerEnough = false;
        Integer dealerScore = 0;
        deck.shuffle();
        while ((yourScore <= 21 && dealerScore <= 21) && (!dealerEnough || !playerEnough)) {
            if (yourScore == 0) {
                yourScore += getCardScore(deck, "Player");
                yourScore += getCardScore(deck, "Player");
            }
            if (dealerScore == 0) {
                dealerScore += getCardScore(deck, "Dealer");
                dealerScore += getCardScore(deck, "Dealer");
            }
            if (!playerEnough) {
                System.out.println("Want a card?");
                playGame = scan.nextLine();
                if (playGame.equals("yes")) {
                    yourScore += getCardScore(deck, "Player");
                } else {
                    System.out.println("PlayerDone");
                    playerEnough = true;
                }
            }
            if (!dealerEnough) {
                if (random.nextBoolean()) {
                    dealerScore += getCardScore(deck, "Dealer");
                } else {
                    System.out.println("DealerDone");
                    dealerEnough = true;
                }
            }
            System.out.println(yourScore + " [p] : [d] " + dealerScore);    
        }
        // decide who is a winner
    }


    private static Integer getCardScore(Deck deck, String who) {
        System.out.print(who + " given: ");
        Cards cards = deck.cards.pollFirst();
        System.out.println(cards);
        return cards.getScore();
    }

这可以帮助你更进一步,或者没有,那么我建议你解决一些较小的练习。

一种有点过时但仍然非常有效的软件设计方法称为 瀑布 过程:从最顶层开始,逐步向下到细节而不是相反。比如先从游戏的整体流程说起:

public static class Game {
    public static void main(String args[]) {
        while (/* player wishes to continue */) {
            /* play a hand */
        }
        System.out.println('Thank you for playing.');
    }
}

现在,考虑需要为上面的评论做些什么。你希望如何与玩家沟通以获得他的意图?键盘?图形用户界面?这将决定您如何充实该部分。然后决定"play a hand"部分的整体策略,你会得到下一个关卡:

public class Game {
    . . .
     public void playOneHand(Player p) {
         /* create new shuffled deck */
         /* deal initial cards */
         while (/* player has choices */) {
             /* get player choices */
         }
         /* play dealer hand */
         /* display result */
     }
}
public class Player {
    /* Maybe actions should be numbers from a menu? An enum class? */
    public String getPlayerAction(String prompt) {
        Scanner sc = new Scanner(System.in);
        System.out.println(prompt);
        return sc.nextLine();
    }
}

这是第 2 级。现在开始填写每条评论,这将带您进入第 3 级,依此类推。最终你会深入了解卡片和数字的细节,但直到你准备好将它们放在某个地方。