Search code examples
javaarraylistpoker

Making Multiple Decks of Cards Java


Edit: I'm getting an error while trying to add the card to my originalDecks and dealDecks.

I think I have figured out how to create a single deck of cards [shown in the method Decks()], but my problem is that I have to create multiple decks of cards [n number of decks = n*52 number of cards]. I can't figure out how to utilize the n to make multiples of the same card. After creating the multiple decks, I think I can figure out the shuffle() and reset(), but getting multiple decks is giving me problems right now.

Note: I was thinking of creating an array of n decks, but I don't even know how to start this.

Card Class

/** class Card : for creating playing card objects
 *  it is an immutable class.
 *  Rank - valid values are 1 to 13
 *  Suit - valid values are 0 to 3
 *  Do not modify this class!
 */
class Card {

/* constant suits and ranks */
static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades" };
static final String[] Rank = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};

/* Data field of a card: rank and suit */
private int cardRank;  /* values: 1-13 (see Rank[] above) */
private int cardSuit;  /* values: 0-3  (see Suit[] above) */

/* Constructor to create a card */
/* throw PlayingCardException if rank or suit is invalid */
public Card(int rank, int suit) throws PlayingCardException { 
if ((rank < 1) || (rank > 13))
    throw new PlayingCardException("Invalid rank:"+rank);
else
        cardRank = rank;
if ((suit < 0) || (suit > 3))
    throw new PlayingCardException("Invalid suit:"+suit);
else
        cardSuit = suit;
}

/* Accessor and toString */
/* You may impelemnt equals(), but it will not be used */
public int getRank() { return cardRank; }
public int getSuit() { return cardSuit; }
public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; }


/* Few quick tests here */
public static void main(String args[])
{
try 
        {
        Card c1 = new Card(1,3);    // A Spades
        System.out.println(c1);
        c1 = new Card(10,0);    // 10 Clubs
        System.out.println(c1);
        //c1 = new Card(10,5);        // generate exception here
    }
catch (PlayingCardException e)
    {
        System.out.println("PlayingCardException: "+e.getMessage());
    }
    }
}

Decks Class

/** class Decks represents : n decks of playing cards
 *  Use class Card to construct n * 52 playing cards!
 *
 *  Do not add new data fields!
 *  Do not modify any methods
 *  You may add private methods 
 */

class Decks {

/* this is used to keep track of original n*52 cards */
private List<Card> originalDecks;   

/* this starts with n*52 cards deck from original deck   */
/* it is used to keep track of remaining cards to deal */
/* see reset(): it resets dealDecks to a full deck      */
private List<Card> dealDecks;

/* number of decks in this object */
private int numberDecks;


/**
 * Constructor: Creates default one deck of 52 playing cards in originalDecks and
 *          copy them to dealDecks.
 *              initialize numberDecks=n
 * Note: You need to catch PlayingCardException from Card constructor
 *       Use ArrayList for both originalDecks & dealDecks
 * @throws PlayingCardException 
 */
public Decks() throws PlayingCardException
{
    // implement this method!
    int i, j;
    for (i=0;i<4;i++) 
    {
        for(j=1;j<14;j++) 
        {
            Card orcard = new Card(i,j);
            originalDecks.add(orcard);
            dealDecks.add(orcard);
        }
    }

}


/**
 * Constructor: Creates n decks (52 cards each deck) of playing cards in
 *              originalDecks and copy them to dealDecks.
 *              initialize numberDecks=n
 * Note: You need to catch PlayingCardException from Card constructor
 *       Use ArrayList for both originalDecks & dealDecks
 * @throws PlayingCardException 
 */
public Decks(int n) throws PlayingCardException
{
    // implement this method!
    int i, j;
    for (i=0;i<4;i++) 
    {
        for(j=1;j<14;j++) 
        {
            Card orcard = new Card(i,j);
            originalDecks.add(orcard);
            dealDecks.add(orcard);
        }
    }
}

Solution

  • You should be able to just loop through the step of creating decks of cards multiple times. Either add another loop through n before or in the for loop.

    public Decks(int n) throws PlayingCardException
    {
        // implement this method!
        int i, j;
        for (i=0;i<4;i++) 
        {
            for(j=1;j<14;j++) 
            {
                for(k=0;k<n;k++){
                    Card orcard = new Card(i,j);
                    originalDecks.add(orcard);
                    dealDecks.add(orcard);
                 }
            }
        }
    }
    

    OR

    public Decks(int n) throws PlayingCardException
    {
        // implement this method!
        int i, j;
        for(int k=0;k<n;k++){
            for (i=0;i<4;i++) 
            {
                for(j=1;j<14;j++) 
                {
                    Card orcard = new Card(i,j);
                    originalDecks.add(orcard);
                    dealDecks.add(orcard);
                }
            }
        }
    }