Java问题在BlackJack计划中打印玩家和经销商

时间:2014-11-15 20:18:04

标签: java arrays blackjack

其他一切正常,除了我遇到打印出卡片的问题(如果他们'击中')因为我不知道玩家绘制什么牌以及哪个牌手取出什么牌,它从8开始前七张牌都被抽出但我不知道哪位牌手得到了什么以及有多少牌。所以我试着用一种方法来打印每个玩家的手,但是我有很多麻烦,可以使用一些帮助。我想打印玩家在输出中获得的牌。我会发布我的卡片,播放器和BlackJackGame课程,因为我的经销商课程非常类似于播放器。

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我不知道哪些牌被击中/哪个玩家拥有它们并且想知道如何在Player类中使用一个方法打印每个玩家的手牌,包括经销商和他们输出的牌。这就是我需要帮助的地方。其他一切都很好。

1 个答案:

答案 0 :(得分:0)

首先,我建议使用switch语句而不是所有其他if if for rank,它会更加整洁:)

其次,在二十一点中,一张王牌并没有随机的&#34;价值1或11;这是一个11,除非它是11使球员破产。

第三,我会考虑使用甲板清单。那么你需要做的就是&#34; shuffle&#34;列表以及当您处理卡片时,从列表中删除顶部卡片并将其添加到播放器的手中。 (见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();
   }