java - 理解Java中的方法/类调用

标签 java string arraylist blackjack

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

最佳答案

这种任务不是很容易的关卡,这里有很多事情要做,也许你必须从一些更基础的东西开始。但如果你确定要通过,这里有一些建议和代码: 您必须将 score 字段添加到 Card 类(类的单个名称比复数名称更好,变量的首字母小写,这是一些代码约定)。这并不容易,因为 Ace 可以具有多值。 使用 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();
    }

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

关于java - 理解Java中的方法/类调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366680/

相关文章:

java - 如何使用 JSP 将表单数据解析到客户端 Java 类

java mysql 外键约束

java - 保存一些数据然后检索它的最佳方法

java - 使用流更改 ArrayList 中对象字段的值,使代码更高效

java - 问题打印由定义的类构造的数组列表

java - Android ListView OnScrollListener 清除旧数据

Java 打印服务 : PrintServiceLookup. lookupPrintServices 不返回网络打印机

c++ - C++中如何控制cin到cout的流程

每次运行时 Java 字符串到字节数组的变化

string - 用 JRE 库替换 StrSubstitutor