Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
21点程序中玩家手的Java问题计算_Java_Arrays_Blackjack - Fatal编程技术网

21点程序中玩家手的Java问题计算

21点程序中玩家手的Java问题计算,java,arrays,blackjack,Java,Arrays,Blackjack,我想用Java做一个21点游戏。我用数组来表示卡片。我遇到了一个问题,就是如何获得牌的价值,并使用每个玩家必须计算出的牌的价值来计算每个玩家的牌数。我有四门课,纸牌、玩家、庄家和黑杰克游戏-司机。我会把这张卡和它的相关值方法、玩家和黑杰克游戏一起贴出来,因为庄家和玩家是完全一样的 //Java public class Card { private String suit, rank; private int value; public Card(String su

我想用Java做一个21点游戏。我用数组来表示卡片。我遇到了一个问题,就是如何获得牌的价值,并使用每个玩家必须计算出的牌的价值来计算每个玩家的牌数。我有四门课,纸牌、玩家、庄家和黑杰克游戏-司机。我会把这张卡和它的相关值方法、玩家和黑杰克游戏一起贴出来,因为庄家和玩家是完全一样的

//Java

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"))
        {
            value=11;
        }
        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

//Player.java

public class Player
{
    private int cValue; // [Card Value] I use this in a method to equal what deck[int] produces and try to use in the getValue method to no avail
    private int cCount; //Card count used to count how many 'cards' added
    Card[] deck= new Card[52]; // 52 card objects
    private int sum; A temporary int I add the cValues into and assign the value to cValue and return it

    public Player()
    {
        cCount=0;

    }

    public void addCard(Card a) //Everytime addCard is executed so I know how many cards are drawn at this point in the program everyone( 3 players and a dealer) has two cards
    {
        deck[cCount] = a;
        cCount++;

    }

    public int getcCount()  //Get the card count from the void method
    {
        return cCount;
    }

    public Card getCard(int a) //Return the deck integer each player has
    {
        return deck[a];
    }

    public int getCardValue(int a) // This works and it produces the value of the card I give the int of the method too however if I use more than two of these in succession, I get a null pointer exception, can't figure it out.  
    {
        cValue = deck[a].Value();
        return cValue;
    }

   public void getValue(int a) //The method I can't get to work, trying to calculate the hand of the player( 
   {
        for(int i =0; i<deck.length; i++)
        {
            sum += cValue;
        }


   }

   public int getValue() // I want to make this the method where the values are summed and return but for some reason no matter what I do I get 0 returned, tried everything.. I really need help with this method.
   {
        cValue = sum;
        return cValue;
   }



}

您只向player3的牌组[]添加了两张牌,因此当您调用public int getCardValue2时,它将转到player3牌组的第三个索引,该索引为空

    Player player3 = new Player();
    ...
    player3.addCard(deck[2]);
    // deck[0]
    ...
    player3.addCard(deck[6]);
    // deck[1]
    ...
    int p3 = player3.getCardValue(2);
    // get value at deck[2]

    // this goes to ->
    public int getCardValue(int a)  
    {
        // try to access deck[2], but deck only has 
        // 2 valid entries deck[0] and deck[1]
        cValue = deck[a].Value();
        return cValue;
    }
其他事情

不是每个玩家都有一个名为deck的数组,或者将其重命名为hand并使用ArrayList而不是数组,ArrayList将允许您动态添加元素

我会在你的Card.Value方法中使用switch语句。像这样:

public int Value()
{
    switch(rank) {
    case "A":
        return 11;
    case "K":
    case "Q":
    case "J":
        return 10;
    default:
        return Integer.parseInt(rank);
}
你的洗牌算法几乎是正确的

    for(int i=0; i< deck.length; i++) //Shuffle
    {
        // this line chooses a random card in the deck
        int count= (int)(Math.random()* deck.length); 
        // this line sets the card at index 'i' to the randomly chosen card
        deck[i] = deck[count];
        // however your creating multiple instance of one card in the deck
        // instead of switching the cards around, this will lead to your deck
        // having more than one of the same card.
    }

我懂了。谢谢你的澄清!一个switch语句会对value方法进行大量清理,我认为可以肯定地实现它。我将查看每个玩家手牌的阵法列表,而不是阵法,然后从那里开始!非常感谢。编辑:还有一件事,我可以用getValue方法来计算牌数,我还可以用我必须做的事情吗?让每个玩家的牌排成一排?或者一个数组列表会让这变得简单很多吗?当然,我还添加了一些关于你的洗牌算法的内容。你是最好的。非常感谢你。我想我看到了我的错误,我尝试在数组中使用cCount而不是I,我想知道为什么到处都是0和null。我根据你的答案给了你答案,你帮了我大忙!谢谢,伙计,如果你还需要帮助,请告诉我
public int Value()
{
    switch(rank) {
    case "A":
        return 11;
    case "K":
    case "Q":
    case "J":
        return 10;
    default:
        return Integer.parseInt(rank);
}
    for(int i=0; i< deck.length; i++) //Shuffle
    {
        // this line chooses a random card in the deck
        int count= (int)(Math.random()* deck.length); 
        // this line sets the card at index 'i' to the randomly chosen card
        deck[i] = deck[count];
        // however your creating multiple instance of one card in the deck
        // instead of switching the cards around, this will lead to your deck
        // having more than one of the same card.
    }
    for(int i=0; i< deck.length; i++) //Shuffle
    {
        // create a temporary card to hold the value of the card to switch
        Card tmp = deck[i];
        // now choose a random card in the deck
        int count= (int)(Math.random()* deck.length); 
        // now set the card at index 'i' to the randomly chosen card
        deck[i] = deck[count];
        // and set the randomly chosen card to deck[i]
        deck[count] = tmp;
    }
  public int calcHandTotal() {
      int total = 0;

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

      return total;
  }