了解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);
    }
}
卢多夫·德米特里(Ludov Dmitrii)

这类任务不是很轻松,这里有很多事情要做,也许您必须从​​一些更基本的东西开始。但是,如果您确定要遍历,那么这里有一些建议和代码:您必须score在Card类中添加字段(对于类,单名比复数更好,对于变量,则小写首字母,这是一些代码约定) 。这并不容易,因为Ace可以具有多值。使用LinkedList而不是ArrayList作为Deck.cards。经销商每次轮询一张牌时,它与真实游戏更相似。

this.cards.add(cards);'deck [Suits.length * i + j] =“ [suits [j]; 您根本不需要此String数组。最重要的部分是您可以将塞文放到您所指的位置,最需要帮助的地方:


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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章