我正在尝试编写一个二十一点程序,用户可以对付CPU经销商。
我遇到的问题是我已经为卡片,套牌和经销商编写课程,但是当我尝试初始化时新经销商和新甲板我收到错误:
Exception in thread "main" java.lang.NullPointerException
at Blackjack.Deck.<init>(Deck.java:12)
at Blackjack.Blackjack.main(Blackjack.java:10)
以下是卡:
的课程 class Card {
private int rank;
private int suit;
private int value;
private static String[] ranks = {"Joker","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
private static String[] suits = {"Clubs","Diamonds","Hearts","Spades"};
Card(int suit, int values)
{
this.rank=values;
this.suit=suit;
if(rank>10)
{
value=10;
}
else
value=rank;
}
public String toString()
{
return ranks[rank]+" of "+suits[suit];
}
public int getRank()
{
return rank;
}
public int getSuit()
{
return suit;
}
public int getValue()
{
return value;
}
public void setValue(int set)
{
value = set;
}
}
我的课程甲板:
package Blackjack;
import java.util.ArrayList;
import java.util.Random;
class Deck {
private ArrayList<Card> deck;
Deck()
{
for(int i=0; i<4; i++)
{
for(int j=1; j<=13; j++)
{
deck.add(new Card(i,j));
}
}
}
public void shuffle()
{
Random random = new Random();
Card temp;
for(int i=0; i<200; i++)
{
int index1 = random.nextInt(deck.size()-1);
int index2 = random.nextInt(deck.size()-1);
temp = deck.get(index2);
deck.set(index2, deck.get(index1));
deck.set(index1, temp);
}
}
public Card drawCard()
{
return deck.remove(0);
}
}
经销商的课程:
package Blackjack;
import java.util.ArrayList;
import java.util.Arrays;
class Dealer {
ArrayList<Card> hand;
private int handvalue=0;
private Card[] aHand;
Dealer(Deck deck)
{
hand = new ArrayList<>();
aHand = new Card[]{};
for(int i=0; i<2; i++)
{
hand.add(deck.drawCard());
}
aHand = hand.toArray(aHand);
for(int i=0; i<aHand.length; i++)
{
handvalue += aHand[i].getValue();
if(aHand[i].getValue()==1 && handvalue<12)
{
handvalue=handvalue+10;
}
}
}
public String showFirstCard()
{
Card[] firstCard = new Card[]{};
firstCard = hand.toArray(firstCard);
return firstCard[0].toString();
}
public void Hit(Deck deck)
{
hand.add(deck.drawCard());
aHand = hand.toArray(aHand);
handvalue = 0;
for(int i=0; i<aHand.length; i++)
{
handvalue += aHand[i].getValue();
if(aHand[i].getValue()==1 && handvalue<12)
{
handvalue=handvalue+10;
}
}
}
public boolean wantsToHit()
{
if(handvalue<17)
{
return true;
}
return false;
}
public boolean hasBlackJack()
{
if(hand.size()==2 && handvalue==21)
{
return true;
}
return false;
}
public String showHand()
{
aHand = hand.toArray(aHand);
String hands="";
for(int i=0; i<aHand.length-1; i++)
{
hands = aHand[i].toString()+", ";
}
hands = hands + aHand[aHand.length-1].toString();
return hands;
}
public int getHandValue()
{
return handvalue;
}
}
我的主要方法:
public class Blackjack {
private static int cash;
public static void main(String[] args){
System.out.println("Hi! What is your name?");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.println("Hello, "+name+" lets plays some BlackJack!");
Deck deck = new Deck();
deck.shuffle();
Dealer dealer = new Dealer(deck);
System.out.println(dealer.showHand());
System.out.println(dealer.getHandValue());
}
}
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
在Deck
中,属性deck
永远不会被初始化,因此null
。由于deck
为null
,此行:deck.add(new Card(i,j));
将引发NullPointerException
。只需在for循环之前创建deck
即可解决此问题
Deck()
{
deck = new ArrayList<>();
for(int i=0; i<4; i++)
{
for(int j=1; j<=13; j++)
{
deck.add(new Card(i,j));
}
}
}
答案 1 :(得分:0)
在Deck()
内,您需要添加以下内容:
deck = new ArrayList<Card>(); // Same for hand in Dealer class
在这里,
aHand = hand.toArray(aHand);
for(int i=0; i<aHand.length; i++)
{
handvalue += aHand[i].getValue();
if(aHand[i].getValue()==1 && handvalue<12)
{
handvalue=handvalue+10;
}
}
可以更改为
for(Card c : hand) {
handvalue += c.getValue();
// rest of code
// And I think inside your if,
// It should be handvalue += 9? You already added the value '1' before.
// If I remember how to play blackjack correctly, counting Ace as 10?
}