简体   繁体   English

Java问题在BlackJack程序中打印玩家和经销商的手

[英]Java Issue Printing hands of Players and Dealer in BlackJack program

Everything else works fine except I run into an issue of printing out the card the player (if they 'hit' ) as I don't know what card the player draws and which player draws what card, it starts at 8 as the first 7 cards are drawn but I don't know what player gets what and how many. 除我遇到打印出玩家的纸牌(如果他们“打”)的问题外,其他所有方法都工作正常,因为我不知道该玩家抽出哪张纸牌以及哪个玩家抽出了哪张纸牌,它从第8张开始为7纸牌被抽出,但我不知道哪个玩家得到什么以及多少。 So I tried to have a method to print out the hand of each player but I'm having a LOT of trouble with that could use some help. 所以我试图找到一种方法来打印出每位玩家的手牌,但是我遇到了很多麻烦,可以使用一些帮助。 I want to print the cards the players get in my output. 我想打印玩家在我的输出中得到的卡片。 I'll post my Card, Player, and BlackJackGame class as my Dealer class is very similar to player. 我将发布Card,Player和BlackJackGame类,因为Dealer类与Player非常相似。

Card.java 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 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);

    }

}

Example Output: 示例输出:

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!

// The Problem is when the cards are printed , I print out the first two cards each player has and the dealer which are known. //问题是在打印纸牌时,我打印出每个玩家拥有的前两张纸牌以及已知的发牌人。 The problem I run into is if the player or dealer 'hits' as you can see with Player 1 I don't know which cards are hit/ and which player has them and was wondering how to have a method in the Player class that would print the hand of each player including the dealer and the cards they have for my output. 我遇到的问题是,如玩家1所示,玩家或发牌人是否“命中”,我不知道哪个牌被击中/哪个牌手拥有,并且想知道如何在Player类中使用一种方法打印每个玩家的手牌,包括发牌者以及他们用于输出的卡。 That is what I need help with. 这就是我需要帮助的地方。 Everything else works fine. 其他一切正常。

First, I would suggest using a switch statement instead of all those else ifs for rank, it'll be a lot neater :) 首先,我建议使用switch语句代替所有其他if排名,这样会更整洁:)

Second, in Blackjack an Ace doesn't have a "random" value of either 1 or 11; 其次,在二十一点中,“ A”的“随机”值不为1或11。 it is an 11 unless it being an 11 makes the player bust. 它是11,除非它是11会使玩家破产。

Third, I would consider using a list for the deck. 第三,我会考虑使用甲板清单。 Then all you need to do is "shuffle" the list and when you deal a card, remove the top card from the list and add it to the player's hand. 然后,您所需要做的就是“洗牌”列表,并且在发牌时,从列表中取出最上面的卡,并将其添加到玩家的手上。 (see Collections.Shuffle ) (请参阅Collections.Shuffle

Now to address the initial question, you should have a List hand in the player class. 现在要解决第一个问题,在播放器类中应该有一个List牌。 Then add an extension method to the player class to print the hand; 然后在玩家类中添加扩展方法以打印手牌; it should look something like this: 它应该看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM