了解 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);

    }

}


猛跑小猪
浏览 122回答 3
3回答

慕盖茨4494581

这种任务不是很容易的水平,这里有很多事情要做,也许你必须从一些更基本的东西开始。但是,如果您确定要通过,这里有一些建议和代码:您必须score向 Card 类添加字段(类的单个名称优于复数,变量的首字母小写,这是一些代码约定) . 不容易,因为 A 可以具有多值。使用 LinkedList 而不是 ArrayList 作为 Deck.cards。发牌者每次轮询一张牌,这与真实游戏更相似。把this.cards.add(cards);'deck [Suits.length * i + j] = Ranks [i] + " of " + Suits [j];' 你根本不需要这个字符串数组。最重要的部分是您可以将骨架放在您指向的地方,最需要帮助的地方:&nbsp; &nbsp; private static void playGame(Scanner scan) {&nbsp; &nbsp; &nbsp; &nbsp; Deck deck = new Deck();&nbsp; &nbsp; &nbsp; &nbsp; String playGame;&nbsp; &nbsp; &nbsp; &nbsp; Integer yourScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; Random random = new Random();&nbsp; &nbsp; &nbsp; &nbsp; Boolean playerEnough = false;&nbsp; &nbsp; &nbsp; &nbsp; Boolean dealerEnough = false;&nbsp; &nbsp; &nbsp; &nbsp; Integer dealerScore = 0;&nbsp; &nbsp; &nbsp; &nbsp; deck.shuffle();&nbsp; &nbsp; &nbsp; &nbsp; while ((yourScore <= 21 && dealerScore <= 21) && (!dealerEnough || !playerEnough)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (yourScore == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourScore += getCardScore(deck, "Player");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourScore += getCardScore(deck, "Player");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dealerScore == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dealerScore += getCardScore(deck, "Dealer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dealerScore += getCardScore(deck, "Dealer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!playerEnough) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Want a card?");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playGame = scan.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (playGame.equals("yes")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourScore += getCardScore(deck, "Player");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("PlayerDone");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; playerEnough = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!dealerEnough) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (random.nextBoolean()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dealerScore += getCardScore(deck, "Dealer");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("DealerDone");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dealerEnough = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(yourScore + " [p] : [d] " + dealerScore);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // decide who is a winner&nbsp; &nbsp; }&nbsp; &nbsp; private static Integer getCardScore(Deck deck, String who) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print(who + " given: ");&nbsp; &nbsp; &nbsp; &nbsp; Cards cards = deck.cards.pollFirst();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(cards);&nbsp; &nbsp; &nbsp; &nbsp; return cards.getScore();&nbsp; &nbsp; }这可以帮助你更进一步,或者没有,那么我建议你解决一些较小的练习。

墨色风雨

一种有点过时但仍然非常有效的软件设计方法称为瀑布过程:从最顶层开始,逐步深入到细节,而不是相反。比如先从游戏的整体流程说起:public static class Game {&nbsp; &nbsp; public static void main(String args[]) {&nbsp; &nbsp; &nbsp; &nbsp; while (/* player wishes to continue */) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* play a hand */&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println('Thank you for playing.');&nbsp; &nbsp; }}现在,考虑需要为上面的评论做些什么。你希望如何与玩家沟通以获得他的意图?键盘?图形用户界面?这将决定您如何充实该部分。然后决定“玩一手牌”部分的总体策略,你会得到一个新的水平:public class Game {&nbsp; &nbsp; . . .&nbsp; &nbsp; &nbsp;public void playOneHand(Player p) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* create new shuffled deck */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* deal initial cards */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (/* player has choices */) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* get player choices */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* play dealer hand */&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* display result */&nbsp; &nbsp; &nbsp;}}public class Player {&nbsp; &nbsp; /* Maybe actions should be numbers from a menu? An enum class? */&nbsp; &nbsp; public String getPlayerAction(String prompt) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(prompt);&nbsp; &nbsp; &nbsp; &nbsp; return sc.nextLine();&nbsp; &nbsp; }}那是第 2 级。现在开始填写每条评论,这将带您进入第 3 级,依此类推。最终你会深入了解卡片和数字的细节,但直到你准备好将它们放在某个地方。

holdtom

现在我对 Java 比较陌生,所以我假设会有更多知识的人出现并能够为您提供更多帮助,但这就是我现在所拥有的:1)我建议你删除,public Cards() {} constructor因为你可能会不小心创建一个无效的卡(没有设置任何属性)。2)如评论所述,我会做一个 while 循环来检查是否按下了一个键,然后执行相关操作。while (gameIsRunning) {&nbsp; &nbsp;if (key1 is pressed) {&nbsp; &nbsp;...&nbsp; &nbsp;}&nbsp; &nbsp;...&nbsp; &nbsp;if (key10 is pressed) {&nbsp; &nbsp;...&nbsp; &nbsp;System.out.println("You won! Game over.");&nbsp; &nbsp;gameIsRunning = false;&nbsp; &nbsp;}}我会在一个类或方法的某个地方定义一轮游戏的过程,或者为它编写所有的辅助方法,然后将它们放入这个循环结构中。或者,您可以用文本提示用户并等待文本输入以进行下一步操作。希望我能帮上忙!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java