java数组价值_关于数组:Java问题计算BlackJack程序中玩家手的价值

我想用Java制作一个BlackJack游戏。 我正在使用数组代表卡片。 我有一个问题是获得卡值并使用卡值,每个玩家必须计算每个玩家的牌。 我有四个课程,卡片,播放器,经销商和BlackJackGame - 驱动程序。 我将使用其相关的Value方法,Player和BlackJackGame发布卡,因为经销商与玩家完全相同。

// 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

{

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的deck[]添加了两张牌,所以当你调用public int getCardValue(2)时它会进入player3牌组的第3个索引,这是空的。

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而不是array,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.

}

它应该如下所示:

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方法计算手数做些什么?

使用cCount所拥有的数组,您可以使用一种方法来计算总手数:

public int calcHandTotal() {

int total = 0;

for(int i = 0; i < cCount; i++) {

total += deck[i].Value();

}

return total;

}

我知道了。 谢谢你的澄清! 并且switch语句会清理value方法很多,我认为肯定会实现它。 我将查看每个玩家的手而不是阵列的阵列列表并从那里开始! 谢谢! 编辑:还有一件事,我可以用getValue方法来计算手,我还可以使用我必须做的事情 - 在每个玩家的阵列中使用卡片吗? 或者数组列表会让这更容易吗?

当然,我还添加了一些关于你的改组算法的内容。

你是最好的。 非常感谢。 我想我看到我的错误,我试图使用cCount而不是在我的数组上使用i,并想知道为什么我得到0并且无处不在。 我通过你的回答给你检查,你一直非常有帮助!

谢谢伙计,如果您需要更多帮助,请与我们联系

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Blackjack是一种流行的纸牌游戏,也称为21点。在Java,可以使用图形用户界面(GUI)创建一个Blackjack桌面应用程序。以下是一个简单的Blackjack Java桌面应用程序的示例,其包括一个平台和三个玩家:“Curly”(您),“Mo”和“Larry”: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Blackjack extends JFrame implements ActionListener { private Deck deck; private Player[] players; private JButton hitButton, stayButton, newButton; private JLabel[] playerLabels, scoreLabels; public Blackjack() { // 初始化牌堆和玩家 deck = new Deck(); players = new Player[3]; players[0] = new Player("Curly"); players[1] = new Player("Mo"); players[2] = new Player("Larry"); // 创建GUI setTitle("Blackjack"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); hitButton = new JButton("Hit"); hitButton.addActionListener(this); buttonPanel.add(hitButton); stayButton = new JButton("Stay"); stayButton.addActionListener(this); buttonPanel.add(stayButton); newButton = new JButton("New Game"); newButton.addActionListener(this); buttonPanel.add(newButton); add(buttonPanel, BorderLayout.SOUTH); JPanel playerPanel = new JPanel(); playerPanel.setLayout(new GridLayout(3, 2)); playerLabels = new JLabel[3]; scoreLabels = new JLabel[3]; for (int i = 0; i < 3; i++) { playerLabels[i] = new JLabel(players[i].getName() + ": "); playerPanel.add(playerLabels[i]); scoreLabels[i] = new JLabel("0"); playerPanel.add(scoreLabels[i]); } add(playerPanel, BorderLayout.CENTER); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == hitButton) { // 玩家点击“Hit”按钮 players[0].addCard(deck.dealCard()); updateGUI(); if (players[0].getScore() > 21) { // 玩家破产 JOptionPane.showMessageDialog(this, "You busted!"); newGame(); } } else if (e.getSource() == stayButton) { // 玩家点击“Stay”按钮 int dealerScore = playDealer(); if (dealerScore > 21 || players[0].getScore() > dealerScore) { // 玩家获胜 JOptionPane.showMessageDialog(this, "You win!"); } else if (players[0].getScore() == dealerScore) { // 平局 JOptionPane.showMessageDialog(this, "Push!"); } else { // 玩家失败 JOptionPane.showMessageDialog(this, "You lose!"); } newGame(); } else if (e.getSource() == newButton) { // 玩家点击“New Game”按钮 newGame(); } } private void newGame() { // 开始新游戏 deck.shuffle(); for (int i = 0; i < 3; i++) { players[i].clearHand(); players[i].addCard(deck.dealCard()); players[i].addCard(deck.dealCard()); } updateGUI(); } private int playDealer() { // 庄家的回合 while (players[1].getScore() < 17) { players[1].addCard(deck.dealCard()); } while (players[2].getScore() < 17) { players[2].addCard(deck.dealCard()); } return players[1].getScore(); } private void updateGUI() { // 更新GUI for (int i = 0; i < 3; i++) { playerLabels[i].setText(players[i].getName() + ": "); scoreLabels[i].setText(Integer.toString(players[i].getScore())); } } public static void main(String[] args) { new Blackjack(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值