Click to See Complete Forum and Search --> : cards


zombieJones
12-18-2007, 08:15 PM
I am trying to build a deck full of cards.
Here is the loop I am using to populate the deck:

Card[] deck = new Card[52];
int index = 0; // position of card
for (int suit = 0; suit < 4; suit++)
{
for (int rank = 0; rank < 13; rank++)
{
deck[index] = new Card (rank, suit);
index++;

}
}

I wrote original code to do this, but every time I added a new card into the deck, every position in the array that came before the current card was reset as this card.
So I searched the web and I found that code above. Tried it out and thought it would help me see what I was doing wrong, but I have the exact same problem as before. It is a deck full of Aces of Spades (12-3) by the end.
This is my first Java project and I have been stuck on this problem for hours.

zombieJones
12-18-2007, 08:56 PM
I put the deck creation into a separate class file and that solved all of my problems.

I am trying to build a deck full of cards.
Here is the loop I am using to populate the deck:

Card[] deck = new Card[52];
int index = 0; // position of card
for (int suit = 0; suit < 4; suit++)
{
for (int rank = 0; rank < 13; rank++)
{
deck[index] = new Card (rank, suit);
index++;

}
}

I wrote original code to do this, but every time I added a new card into the deck, every position in the array that came before the current card was reset as this card.
So I searched the web and I found that code above. Tried it out and thought it would help me see what I was doing wrong, but I have the exact same problem as before. It is a deck full of Aces of Spades (12-3) by the end.
This is my first Java project and I have been stuck on this problem for hours.