import java.util.Random;
enum Suit { SPADES, CLUBS, HEARTS, DIAMONDS };
enum Face { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };
class Card {
Suit suit;
Face face;
public Card(Suit suit, Face face){
this.suit = suit;
this.face = face;
}
public Suit getSuit(){
return suit;
}
public Face getFace(){
return face;
}
}
public class DeckCardGame {
private static final int suits_per_deck = Suit.values().length;
private static final int cards_per_suit = Face.values().length;
private Card [] cards = null;
public DeckCardGame() {
cards = new Card[suits_per_deck * cards_per_suit];
for (Suit s : Suit.values()) {
for (Face f : Face.values()) {
int index = s.ordinal() * cards_per_suit + f.ordinal();
cards[index] = new Card(s, f);
}
}
}
public void shuffle() {
int total = suits_per_deck * cards_per_suit;
int j = total;
int index;
Random rand = new Random();
while (j > 0) {
index = rand.nextInt(total);
try{
Card tmp = cards[index];
cards[index] = cards[j];
cards[j] = tmp;
}catch(Exception e){
}
j--;
}
}
public void printCards() {
for(Card c : cards) {
System.out.println("["+c.getSuit().toString() + " " + c.getFace().toString() + "], ");
}
System.out.println("");
}
public static void main(String [] args) {
DeckCardGame dcg = new DeckCardGame();
dcg.printCards();
dcg.shuffle();
dcg.printCards();
}
};
No comments:
Post a Comment