我想用Java制作一个21点游戏。我使用数组来表示卡片。我在获取牌值和使用每个玩家必须计算每个玩家的牌值时遇到了问题。我有四个类,卡牌,玩家,荷官和21点游戏 - 司机。我将发布卡片及其相关的价值方法,玩家和21点游戏作为经销商与玩家完全相同。
//Card.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;
}
}
//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[] 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
{
int count= (int)(Math.random()* deck.length);
deck[i] = deck[count];
}
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("Player 1 has: " +deck[0]);
// System.out.println("Player 2 has: " +deck[1]);
// System.out.println("Player 3 has : " +deck[2]);
System.out.println("And now the Dealer gets his card...");
dealer.addCard(deck[3]);
// System.out.println("The Dealer has: " +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]);
System.out.println("These are the cards player1 has: " +deck[0]+ " "+deck[4]);
System.out.println("These are the cards player2 has: " +deck[1]+ " "+deck[5]);
System.out.println("These are the cards player3 has: " +deck[2]+ " "+deck[6]);
int p1 = player1.getCardValue(0);
int p2 = player2.getCardValue(1);
int p3 = player3.getCardValue(2); // This points to null, why?!
System.out.println(p1);
System.out.println(p2);
输出
Shuffling...
*Some print lines of stuff I wrote*
These are the cards this player has: ... ...
Exception in thread "main" java.lang.NullPointerException
at Player.getCardValue(Player:java:39)
at BlackJackGame.main(BlackJackGame.java:85)
您只向player3的<code>牌组〔〕时,它将转到Player2牌组的第三个索引,该索引为空。
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;
}
其他事情。。。
不是每个玩家都有一个名为的数组
或者将其重命名为hand
,并使用ArrayList
而不是数组
,arrayllist
将允许您动态添加元素。
我会在您的< code >卡中使用< code>switch语句。Value()方法。像这样:
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;
}
我能为getValue方法做什么来计算手牌?使用带有< code > account 的数组,您可以有一种方法来计算手牌总数:
public int calcHandTotal() {
int total = 0;
for(int i = 0; i < cCount; i++) {
total += deck[i].Value();
}
return total;
}
所有其他的工作都很好,除了我遇到一个问题,打印出牌的球员(如果他们‘击中’),因为我不知道什么牌的球员抽了什么牌,谁抽了什么牌,它从8开始,因为前7张牌被抽了,但我不知道谁得到了什么和多少。所以我试着用一种方法打印出每个球员的手牌,但我有很多麻烦,这可能需要一些帮助。我想在我的输出中打印玩家得到的卡片。我将发布我的卡片、玩家和BlackJackGame类,因为我的Dealer类与Player非常相
我有一副牌洗牌和返回结果,但现在我想根据用户输入更改输出:玩家数量和每个玩家的纸牌数量。 原始代码: CardRun-main 甲板级 卡片类 纸牌输出甲板:54 玩家数量:4(由用户输入) 每个用户的纸牌数量:5(由用户输入) 玩家1:黑桃A,红心2,红小丑 玩家2:红心8,梅花10,红心9 玩家1手牌更好。
问题内容: 作为Web开发人员,您经常会遇到一些问题,如果有类似价值计算之类的问题,很容易解决。 我经常想知道为什么无法在CSS中执行以下操作: 例如,这将解决您想要垂直对齐元素时遇到的一些问题。现在很难使用CSS垂直对齐元素,并且会产生相当大的开销。 在您知道元素的固定高度的情况下,您不需要这样做。但是,一旦高度发生变化(较长的文字等),您就会迷上了一个元素的总高度。 使用附加的JS可以轻松解决
我的程序要求用户输入牌数和玩家数。我只是想打印出每个玩家的卡片。ex.(球员1:黑桃王牌,红桃两枚。球员2:梅花两枚等)我已经把它打印出来了,但它似乎只打印出一套卡片,尽管玩家的数量中输入了2张。这是我的密码。 手类: 我的司机: 我得到的输出: 我想让它打印出两套卡片,而不是一套,但从逻辑上讲,我不知道该怎么做。任何帮助都会很好,谢谢。
本文向大家介绍java用扑克牌计算24点,包括了java用扑克牌计算24点的使用技巧和注意事项,需要的朋友参考一下 一副扑克牌的每张牌表示一个数(J、Q、K 分别表示 11、12、13,两个司令都表示 6)。任取4 张牌,即得到 4 个 1~13 的数,请添加运算符(规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式。每个数只能参与一次运算,4 个数顺序可以任意组合,4 个运算符任意取 3 个
我有一副洗牌牌,我应该把它做成这样,它可以把5张牌分给4个玩家。我像个白痴一样坐在这里好几个小时了,我被卡住了。 我被最后一部分难住了。我将为四名玩家获得五张类似的牌。看起来是这样的: 红心皇后 红心皇后 红心皇后 红心皇后 红心皇后 10的钻石 10的钻石 10的钻石 10的钻石 10的钻石 红心6号 红心6号 红心6号 红心6号 红心6号 黑桃10 黑桃10 黑桃10 黑桃10 黑桃10 如果