Using this approach with independent enumeration lists does not work. This may be enough for the application you created, but if you create some kind of card game, you will have double cards, such as two aces of hearts. I suggest you create a class map that contains the rank and set as enumerations. Create a Deck class that initializes the creation of all cards for the deck. Then you can have the Card drawNextRandom () method, which returns the next random card from the deck and removes it from the deck.
public class Card{
public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
private Rank rank;
private Suite suite;
public Card(Rank rank, Suite suite){
this.rank = rank;
this.suite = suite;
}
}
public class Deck{
private RandomGenerator rgen = new RandomGenerator();
private List<Card> cards = new ArrayList<Card>();
public Deck(){
for(Rank rank : Rank.values()){
for(Suite suite : Suite.values()){
cards.add(new Card(rank, suite));
}
}
}
public Card nextRandomCard(){
int nextCard = rgen.nextInt(0, cards.size());
Card card = cards.remove(nextCard);
return card;
}
}
This will be the starting point for a card game.
Cheers, Sven
source
share