Hi, I'm concentrating on getting my card and deck classes for a card game project working correctly. Both my card and deck classes are working technically correct but when I go though my nested for loop in my Deck class to create a deck of cards it ends up being in a reversed order than what we should have based on the project details. Any ideas how to easily switch the order that my for loops create a card deck so that it goes from the top down instead of bottom up?
Here are my card classes and output when I test in Deck class.
public class Card{
public Rank rank;
public Suit suit;
//constructor for a single card: rank and suit
Card( Rank rank, Suit suit){
this.suit = suit;
this.rank = rank;
}
public Rank getRank(){
return rank;
}
public Suit getSuit(){
return suit;
}
public String toString(){
String temp = "";
temp += rank.getName();
temp += " of ";
temp += suit.getName();
return temp;
}
public static void main(String[] args){
Card card1 = new Card(Rank.FOUR, Suit.CLUBS);
System.out.println(card1);
}
}
import java.util.Arrays;
public class Deck
{
private Card[] deck;
static final int numberOfCards = 52;
private static Rank[] rankValues = Rank.values();
private static Suit[] suitValues = Suit.values();
public Deck()
{
deck = new Card[52];
int x = 0;
for (int i = 0; i < suitValues.length; i++)
{
for (int j = 0; j < rankValues.length; j++)
{
deck[x] = new Card(rankValues[j], suitValues[i]);
x++;
}
}
}
public void shuffle()
{
// Shuffles the deck.
Card temp;
int a;
for (int x = 0; x < numberOfCards; x++)
{
a = (int)(Math.random() * numberOfCards);
temp = deck[x];
deck[x] = deck[a];
deck[a] = temp;
}
}
public String toString(){
return Arrays.toString(deck);
}
public static void main(String[] args)
{
Deck deck1 = new Deck();
System.out.println(Arrays.toString(rankValues));
System.out.println(Arrays.toString(suitValues));
System.out.println(deck1.toString());
//deck1.shuffle();
//System.out.println(deck1.toString());
}
}
Everything is working technically correct except the order which the cards are created needs to be completely reversed. When I run the test main in my Deck class the output is
[ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING]
[CLUBS, DIAMONDS, HEARTS, SPADES]
[Ace of Clubs, Two of Clubs, Three of Clubs etc... Ace of Diamonds, Two of Diamonds, Three of Diamonds etc... Ace of Hearts, Two of Hearts, Three of Hearts etc... Ace of Spades, Two of Spades, Three of Spades etc...]
I need this output to begin with the King of Spades and end with the ace of Clubs in reversed order from what its currently printing out.