【问题标题】:Understanding method / class calls in Java理解 Java 中的方法/类调用
【发布时间】:2019-12-13 10:54:36
【问题描述】:

我正在尝试制作一个非常基本的 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);
    }
}

卡片.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);
    }
}

【问题讨论】:

  • 此类游戏通常有一个while 循环,其条件表明游戏仍在进行中。放开逻辑,找出一个简单的 while 循环,它将继续。对于初学者来说,它可能是 while user does not hit q, get input.. 旁注,你不需要多个扫描仪,永远不要忘记 scanner.close
  • 你可以用System.out.println方法代替System.out.print,这样你就可以不用在每一行后面加上\n

标签: java string arraylist blackjack


【解决方案1】:

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

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


    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();
    }

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

【讨论】:

    【解决方案2】:

    现在我对 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;
       }
    }
    

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

    希望我能帮上忙!

    【讨论】:

      【解决方案3】:

      一种有点过时但仍然非常有效的软件设计方法称为瀑布过程:从最顶层开始,逐步深入细节,而不是其他方式。例如,从游戏的整体流程开始:

      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.');
          }
      }
      

      现在,考虑一下上面的 cmets 需要做什么。你希望如何与玩家沟通以了解他的意图?键盘?图形用户界面?这将决定你如何充实那部分。然后决定“玩一手”部分的整体策略,你就会进入下一个关卡:

      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 级。现在开始填写每个 cmets,这将带您进入第 3 级,依此类推。最终,您会深入了解卡片和数字的详细信息,但要等到您准备好将它们放在某个地方之后。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 2015-10-06
        • 2016-08-01
        • 1970-01-01
        • 2020-07-31
        • 2020-07-01
        相关资源
        最近更新 更多