-1
我想为卡片游戏制作一个基本轮廓,我创建了卡片,创建了一个处理随机卡片的方法,但是我很难将卡片添加到实际的手中。问题出在游戏类与getCard()方法。我不知道这样做的正确方法,因为我的想法没有奏效。卡片游戏将卡片添加到一组“手”--Java
类创建甲板:
import java.util.Random;
public class Deck<E> {
//create a new linked list for Deck
LinkedPositionalList deck = new LinkedPositionalList();
public Deck(){
for (int i = 0; i < 4; i++){
for(int j = 2; j< 14; j++){
Card card = new Card(i,j);
deck.addLast(card); //add to linked list
}
}
}
public Card card(){
Random rand = new Random();
int position = rand.nextInt(52);//create random number 0-52
int counter = 0;
Iterator<Card> iter = this.deck.iterator();
while(counter <= position){
Card card = iter.next();
if(counter == position){
iter.remove();
return card;
}
counter++;
}
return null;
}
}
类创建卡:
public class Card<E> {
public final static int CLUBS = 0,
SPADES = 1,
DIAMONDS = 2,
HEARTS = 3;
private final static String [] suitNames = {"CLUBS", "SPADES", "DIAMONDS", "HEARTS"};
// Special cards
private int JACK_VALUE = 11;
private int QUEEN_VALUE = 12;
private int KING_VALUE = 13;
private int ACE_VALUE = 14;
// The card
private int suit = 0; // Suit of the card
private int value = 2; // Value of the card
public int getSuit() {
return this.suit;
}
public int getValue() {
return this.value;
}
public Card(int suit, int value){
this.suit = suit;
this.value = value;
}
public String suitToString() {
return suitNames[ suit ];
}
public String valueToString() {
if (value == ACE_VALUE) {
return "ACE";
} else if (value == JACK_VALUE) {
return "JACK";
} else if (value == QUEEN_VALUE) {
return "QUEEN";
} else if (value == KING_VALUE) {
return "KING";
} else if (value > 0) {
return String.valueOf(value);
}
return "";
}
public String shortValueToString() {
if (value == ACE_VALUE) {
return " A";
} else if (value == JACK_VALUE) {
return " J";
} else if (value == QUEEN_VALUE) {
return " Q";
} else if (value == KING_VALUE) {
return " K";
} else if (value > 0) {
return String.format("%2d",value);
}
return "";
}
public String toString() {
return valueToString() + " of " + suitToString();
}
public String toShortString() {
return shortValueToString() + suitToString().substring(0,1);
}
public boolean equalTo(Card c) {
if (c.getSuit() != this.getSuit()) return false;
if (c.getValue() != this.getValue()) return false;
return true;
}
}
类创建的手:
public class CardHand<E> {
LinkedPositionalList<E> hand = new LinkedPositionalList();
//TODO: create method to order hand
}
类初始化游戏:
public class Game{
int players;
int maxCardsInHand;
int decks;
CardHand[] hand;
Card card;
Deck deck;
//constructor
public Game(int player, int max, int numDecks){
players = player;
maxCardsInHand = max;
decks = numDecks;
this.hand = new CardHand[player];
for(int index = 0; index < hand.length; index++){
this.hand[index] = new CardHand();
}
}
public void getCard(){
System.out.println("You got this far...");
card = deck.card();
for(int index = 0; index < hand.length; index++){ //to add to each players hands
hand[index] = card; //issues with this part
}
//TODO: ordered correctly by suit AND value
}
首先,'card'应该是'getCard()'方法中的局部变量。其次,你遇到了什么问题? – Stefan
@Stefan我得到一个空指针异常。我的输出打印出“你有这么多”。我需要打电话给deck.card();因为这实际上处理卡 – Travis
可能重复[什么是NullPointerException,以及如何解决它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do -i-fix-it) – GriffeyDog