编辑:我的第一个问题已经解决,但是,函数 getValue() 总是返回 11。有什么想法吗?
编辑:找到了,
Collections.shuffle(Arrays.asList(deck));
没有正确洗牌。我不知道为什么。
我正在做一个任务,我必须编写/填写一些代码来编写一个简单的二十一点程序。
我已经定义了三个类,一个 Card 类、一个 Hand 类(它是一个 Cards 数组和一些其他值)和一个 Deck 类,它也是一个 Cards 数组。
在 Hand 类中,我有一个方法可以将卡片的所有值相加,每当我运行我的代码时,我都会得到一个 java.lang.NullPointerException,并且控制台指向我的这个“addHand”方法:
public int addHand(){
int j=0;
for(int i=0; i<=counter-1; i++){
j += Cards[i].getValue();
}
return j;
}
我认为这是因为 Cards[] 中的某些值是空的。但是“计数器”不应该让它们中的任何一个被添加。我将包括手牌类的其余部分,以及卡片类。
手类:
public class Hand {
private Card[] Cards;
private int counter;
//private int valueOfHand;
public Hand(){
Cards = new Card[10];
counter = 0;
}
public int numCards(){
return counter;
}
public Card getCard(int i){
return Cards[i];
}
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
public void addCard(Card card){
Cards[counter] = card;
setCounter(counter+1);
}
public int addHand(){
int j=0;
for(int i=0; i<=counter-1; i++){
j += Cards[i].getValue();
}
return j;
}
}
和卡片类:
public class Card {
private String value;
private String suit;
public Card(String number, String shape){
value = number;
suit = shape;
}
public int getValue() {
if (value.equals("King") || value.equals("Queen") || value.equals("Jack")){
return 10;
}
else if (value.equals("Ace")){
return 11;
}
else {
return Integer.parseInt(value);
}
}
public void setValue(String value) {
this.value = value;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
}
我不知道我是否需要发布其他内容,如果有必要我会很乐意。
确切的错误是:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Hand.addHand(Hand.java:39)
at BlackJackGUIPanel$CardPanel.hitMe(BlackJackGUIPanel.java:182)
at BlackJackGUIPanel$CardPanel.actionPerformed(BlackJackGUIPanel.java:161)
这是Hitme代码:
void hitMe() {
if (!gameInProgress) {
message = "Click \"New Game\" to start a new game!";
repaint();
return;
}
player.addCard(deck.giveCard());
if(player.addHand() > 21){
message = "Game over, you busted";
gameInProgress = false;
}
message = "You clicked Hit Me!";
repaint();
和 giveCard() 代码:
public Card giveCard(){
Card temp = Decks[0];
for(int i = 1; i<52; i++){
Decks[i-1] = Decks[i];
}
Decks[51] = temp;
return temp;
}
我找不到更好的方法来完成这部分。这里是我初始化我的牌组的地方:
public Deck(){
Decks = new Card[52];
//int cardNumber = 2;
for(int i = 0; i < 52; i += 4){
switch (i/4){
case 0: Decks[i] = new Card("2", "Spades");
Decks[i+1] = new Card("2", "Hearts");
Decks[i+2] = new Card("2", "Clubs");
Decks[i+3] = new Card("2", "Diamonds");
case 4: Decks[i] = new Card("3", "Spades");
Decks[i+1] = new Card("3", "Hearts");
Decks[i+2] = new Card("3", "Clubs");
Decks[i+3] = new Card("3", "Diamonds");
case 8: Decks[i] = new Card("4", "Spades");
Decks[i+1] = new Card("4", "Hearts");
Decks[i+2] = new Card("4", "Clubs");
Decks[i+3] = new Card("4", "Diamonds");
case 12: Decks[i] = new Card("5", "Spades");
Decks[i+1] = new Card("5", "Hearts");
Decks[i+2] = new Card("5", "Clubs");
Decks[i+3] = new Card("5", "Diamonds");
case 16: Decks[i] = new Card("6", "Spades");
Decks[i+1] = new Card("6", "Hearts");
Decks[i+2] = new Card("6", "Clubs");
Decks[i+3] = new Card("6", "Diamonds");
case 20: Decks[i] = new Card("7", "Spades");
Decks[i+1] = new Card("7", "Hearts");
Decks[i+2] = new Card("7", "Clubs");
Decks[i+3] = new Card("7", "Diamonds");
case 24: Decks[i] = new Card("8", "Spades");
Decks[i+1] = new Card("8", "Hearts");
Decks[i+2] = new Card("8", "Clubs");
Decks[i+3] = new Card("8", "Diamonds");
case 28: Decks[i] = new Card("9", "Spades");
Decks[i+1] = new Card("9", "Hearts");
Decks[i+2] = new Card("9", "Clubs");
Decks[i+3] = new Card("9", "Diamonds");
case 32: Decks[i] = new Card("10", "Spades");
Decks[i+1] = new Card("10", "Hearts");
Decks[i+2] = new Card("10", "Clubs");
Decks[i+3] = new Card("10", "Diamonds");
case 36: Decks[i] = new Card("Jack", "Spades");
Decks[i+1] = new Card("Jack", "Hearts");
Decks[i+2] = new Card("Jack", "Clubs");
Decks[i+3] = new Card("Jack", "Diamonds");
case 40: Decks[i] = new Card("Queen", "Spades");
Decks[i+1] = new Card("Queen", "Hearts");
Decks[i+2] = new Card("Queen", "Clubs");
Decks[i+3] = new Card("Queen", "Diamonds");
case 44: Decks[i] = new Card("King", "Spades");
Decks[i+1] = new Card("King", "Hearts");
Decks[i+2] = new Card("King", "Clubs");
Decks[i+3] = new Card("King", "Diamonds");
case 48: Decks[i] = new Card("Ace", "Spades");
Decks[i+1] = new Card("Ace", "Hearts");
Decks[i+2] = new Card("Ace", "Clubs");
Decks[i+3] = new Card("Ace", "Diamonds");
}
}
Collections.shuffle(Arrays.asList(Decks));
}