【问题标题】:Java Issue Printing hands of Players and Dealer in BlackJack programJava问题在BlackJack程序中打印玩家和经销商的手
【发布时间】:2015-01-13 01:00:46
【问题描述】:

除了我遇到打印玩家牌的问题(如果他们“击中”),其他一切都很好,因为我不知道玩家抽什么牌,哪个玩家抽什么牌,它从 8 开始前 7 张牌已经抽到了,但我不知道哪个玩家得到了什么以及有多少。所以我尝试了一种方法来打印每个玩家的手牌,但我遇到了很多麻烦,可以使用一些帮助。我想打印玩家在我的输出中获得的卡片。我将发布我的 Card、Player 和 BlackJackGame 类,因为我的 Dealer 类与玩家非常相似。

Card.java

import java.util.Random;

public class Card
{
    private String suit, rank; 
    private int value;


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

    public String getRank()
    {
        return rank;
    }

    public int Value()
    {
        if(rank.equals("2"))
        {
            value=2;
        }
        else if(rank.equals("3"))
        {
            value=3;
        }
        else if(rank.equals("4"))
        {
            value=4;
        }
        else if(rank.equals("5"))
        {
            value=5;
        }
        else if(rank.equals("6"))
        {
            value=6;
        }
        else if(rank.equals("7"))
        {
            value=7;
        }
        else if(rank.equals("8"))
        {
            value=8;
        }
        else if(rank.equals("9"))
        {
            value=9;
        }
        else if(rank.equals("10"))
        {
            value=10;
        }
        else if(rank.equals("A"))
        {
            Random rand = new Random();
            int count = rand.nextInt(1) +1;
            if(count == 1)
            {
                value=11;
            }
            else
                value= 1;
        }
        else if(rank.equals("Q"))
        {
            value=10;
        }
        else if(rank.equals("J"))
        {
            value=10;
        }
        else if(rank.equals("K"))
        {
            value=10;
        }

        return value;
    }


    public String toString()
    {
    return(rank + " of " + suit);
    }

}

Player.java

public class Player
{
    private int cValue;
    private int cCount; //Card count used to count how many 'cards' added
    Card[] deck= new Card[52];
    private int sum;

    public Player()
    {
        cCount=0;

    }

    public Card addCard(Card a)
    {
        deck[cCount] = a;
        cCount++;
        return a;

    }


    public int getcCount()
    {
        return cCount;
    }


   public int getValue()
   {
        int total=0;

        for(int i=0; i < cCount; i++)
        {
            total += deck[i].Value();
        }

        return total;


   }

BlackJackGame.java

public class BlackJackGame
{



    public static void main(String []   args)
    {
        Card[] deck = new Card[52];
        Player[] player = new Player[3];
        int loopcount=0; 
        String p1result = " ", p2result = " ", p3result = " ", p4result = " ", dresult = " "; 

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

        for(int i=0; i<13; i++)
        {
            for(int x=0; x<4;x++)
            {
                deck[loopcount] = new Card(suit[x], rank[i]);
                loopcount++;
            }
        }

        System.out.println("Shuffling...");

        for(int i=0; i< deck.length; i++) //Shuffle
        {
            Card tmp = deck[i];
            int count= (int)(Math.random()* deck.length);
            deck[i] = deck[count];
            deck[count] = tmp;

        }

        Player player1 = new Player();
        Player player2 = new Player();
        Player player3 = new Player();

        System.out.println("Welcome to our BlackJackGame!");

        System.out.println("Welcome Dealer!");

        Dealer dealer = new Dealer();

        System.out.println("Let's deal the cards!");

        player1.addCard(deck[0]);

        player2.addCard(deck[1]);

        player3.addCard(deck[2]);


    System.out.println("And now the Dealer gets his card...");

        dealer.addCard(deck[3]);

    System.out.println("Now we get our second cards!");

    System.out.println("Okay Dealer, deal out the cards!");

        player1.addCard(deck[4]);

        player2.addCard(deck[5]);

        player3.addCard(deck[6]);

        dealer.addCard(deck[7]);

        int count =8;
        int i=0;

        do
        {
            p1result = "";
            p2result = "";
            p3result = "";
            dresult = "";
        int dvalue = dealer.getValue();
        int p1value = player1.getValue();
        int p2value = player2.getValue();
        int p3value = player3.getValue();
        while(p1value < 17) //hit
        {
            player1.addCard(deck[count]);
            count++;
            p1value = player1.getValue();
        }
        if(p1value > 21)
        {
            p1result = "Bust!";
        }
        if(p1value <21 && p1value >17)//stand
        {
        }
        while(p2value < 17)//hit
        {
            player2.addCard(deck[count]);
            count++;
            p2value = player2.getValue();
        }
        if(p2value > 21) //bust
        {
            p2result = "Bust!";
        }
        if(p2value <21 && p2value >17) //stand
        {
        }
        while(p3value < 17) //hit
        {
            player3.addCard(deck[count]);
            count++;
            p3value = player3.getValue();
        }
        if( p3value > 21)
        {
            p3result = "Bust!";
        }
        if(p3value <21 && p3value >21) //stand 
        {
        }

        while(dvalue < 17)
        {
            dealer.addCard(deck[count]);
            count++;
            dvalue = dealer.getValue();
        }
        if(dvalue > 21) //Bust
        {
            p1value = player1.getValue();
            p2value = player2.getValue();
            p3value = player3.getValue();
            if(p1value == 21 || p1value <21) 
            {
                p1result = "Win!";
            }
            if(p2value == 21 || p2value <21)
            {
                p2result = "Win!";
            }
            if(p3value == 21 || p3value <21 )
            {
                p3result = "Win!";
            }
        }

        if(dvalue < 21 && dvalue >= 17) //For Dealer values in between
        {
            p1value = player1.getValue();
            p2value = player2.getValue();
            p3value = player3.getValue();
            dvalue = dealer.getValue();

            if(p1value == dvalue)
            {
                p1result = "Push!";
            }
            if(p1value > dvalue)
            {
                p1result = "Win!";
            }
            if(p1value < dvalue)
            {
                p1result = "Lose!";
            }
            if(p2value == dvalue)
            {
                p2result = "Push!";
            }
            if(p2value > dvalue)
            {
                p2result = "Win!";
            }
            if(p2value < dvalue)
            {
                p2result = "Lose!";
            }
            if(p3value == dvalue)
            {
                p3result = "Push!";
            }
            if(p3value > dvalue)
            {
                p3result = "Win!";
            }
            if(p3value < dvalue)
            {
                p3result = "Lose!";
            }

        }

        if(dvalue == 21 )
        {
            p1value = player1.getValue();
            p2value = player2.getValue();
            p3value = player3.getValue();
            dvalue = dealer.getValue();
            if(p1value == dvalue)
            {
                 p1result = "Push!";
            }
            if(p1value < dvalue || p1value > dvalue)
            {
                 p1result = "Lose!";
            }
            if(p2value == dvalue)
            {
                p2result = "Push!";
            }
            if(p2value < dvalue || p2value > dvalue)
            {
                p2result = "Lose!";
            }
            if(p3value == dvalue)
            {
                p3result = "Push!";
            }
            if(p3value < dvalue || p3value > dvalue)
            {
                p3result = "Lose!";
            }

        } 
        System.out.println("The BlackJack Game is Complete: ");
        System.out.println("Results: ");
        System.out.println("Dealer: " +deck[3] + " " + deck[7] + " " +("total of " +dealer.getValue() ));
        System.out.println("Player1: " +deck[0] + " " + deck[4] + " "+("total of " +player1.getValue() )+ ": " +p1result);
        System.out.println("Player2: " +deck[1] + " " + deck[5] + " "+("total of " +player2.getValue() )+ ": " +p2result);
        System.out.println("Player3: " +deck[2] + " " + deck[6] + " "+("total of " +player3.getValue() )+ ": " +p3result);
        i++;
    }
    while(i <1);

    }

}

示例输出:

BlackJack Game is Complete!
Results!
Dealer : 6 of Clubs 8 of Spades total 17
Player1: 2 of Clubs 2 of Spades total 16 Lose!
Player2: Q of Hearts Q of Spades total 20 Win!
Player3: A of Spades 6 of Hearts total 17 Push!

// 问题是在打印牌时,我打印出每个玩家拥有的前两张牌和已知的庄家。我遇到的问题是玩家或庄家是否“击中”玩家 1打印每个玩家的手牌,包括庄家和他们为我输出的牌。这就是我需要帮助的。其他一切正常。

【问题讨论】:

标签: java arrays blackjack


【解决方案1】:

首先,我建议使用 switch 语句而不是所有其他 if 来进行排名,这样会更整洁:)

其次,在二十一点中,一张 A 的“随机”值既不是 1 也不是 11。它是 11,除非它是 11 会使玩家破产。

第三,我会考虑为套牌使用列表。然后你需要做的就是“洗牌”列表,当你发牌时,从列表中移除最上面的牌并将其添加到玩家的手上。 (见Collections.Shuffle

现在要解决最初的问题,您应该在播放器类中有一个 List 手。然后在播放器类中添加一个扩展方法来打印手牌;它应该看起来像这样:

   public void printHand() {
      ListIterator<Card> it = hand.listIterator();
      if(it.hasNext())
         System.out.print(it.next());
      while(it.hasNext())
         System.out.print(", " + it.next());
      System.out.println();
   }

【讨论】:

  • 等等,我有点困惑。关于 Ace,我想我可以给 Ace 一个 1 或 11 的值来处理它,或者不知道该规则。这就解释了随机位。我会考虑将它合并到我的代码中的某个地方。是否可以使用我必须打印出每个玩家拥有的每张牌的东西,或者如果不使用列表,那是不可能的——如果可能的话,那会是什么样子?我尝试通过在上一个方法中返回 a 并以这种方式执行来强制数组为字符串,但是没有用。
  • 当然你可以使用你所拥有的,但是在 Player 类中添加一个扩展方法可能会更整洁,更不容易出错。如果您不想将所有内容都更改为列表,只需在您的打印方法中使用播放器类中的数组即可。
  • 使用播放器类中的数组会是什么样子 - 即使用我所拥有的?我尝试使用 addCard 但这会重现添加更多卡片并增加每个玩家价值的问题。我还没有接触过列表,所以我宁愿不使用它们,而是在处理它之前学习如何这样做。
  • 现在是学习的好时机 :) docs.oracle.com/javase/tutorial/collections/interfaces/… 但无论如何,如果你要展示手中的所有牌,而不仅仅是前两张,你会需要循环手
猜你喜欢
  • 1970-01-01
  • 2015-04-27
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多