Création d'un jeu de cartes à jouer à l'aide d'une LinkedList en java


Je ne sais pas ce que je fais ici, mais j'essaie de travailler avec la collection LinkedList en java pour créer un jeu de cartes à jouer. il semble y avoir une erreur dans ma logique. je continue à obtenir une NullPointerException dans le constructeur du deck.

J'essaie de retravailler le code qui utilisait un tableau pour utiliser une LikedList à la place. Ce qui me manque?

public class Deck {

/** 
 * A LinkedList of cards in the deck, where the top card is the 
 * first index.
 */
private LinkedList<Card> mCards;

/**
 * Number of cards currently in the deck
 */
private int numCards;


/**No args Constructor- if no arguments are used when creating a deck,
 * then we define the game deck to be one deck without shuffling.
 * 
 */
public Deck(){ this(1, false); }

/**Constructor that defines the number of decks (how many sets of 52
 *              cards are in the deck) and whether it should be shuffled.
 * 
 * @param numDecks the number of individual decks in this game deck
 * @param isShuffled whether to shuffle the cards
 */
public Deck(int numDecks, boolean isShuffled){

    this.numCards = numDecks * 52;

    //for each deck
    for (int i = 0; i < numDecks; i++){

        //for each suit
        for(int j = 0; j < 4; j++){

            //for each number
            for(int k = 1; k <= 13; k++){

                //add card to the deck
                this.mCards.add(new Card(Suit.values()[j], k));
            }
        }
    }
    if(isShuffled){
        this.shuffle();
}

public enum Suit {
   Clubs,
   Diamonds,
   Spades,
   Hearts,
}
Author: StillLearningToCode, 2014-07-30

1 answers

Vous n'initialisez jamais mCards.

Ajouter (au début de votre constructeur) :

mCards = new LinkedList<Card>();
 4
Author: Eran, 2014-07-29 22:45:51