rendered paste bodyimport java.util.Random;
import myJavaLib.io.Keyboard;
class DeckOfCards
{
enum Suit {Diamond, Club, Heart, Spade};
enum Rank {Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King};
private Rank[] rank=new Rank[52];
private Suit[] suit=new Suit[52];
private int top=0;
public static DeckOfCards d = null;
private DeckOfCards()
{
for (int i=0; i<4; i++)
{
rank[0+13*i] = Rank.Ace;
rank[1+13*i] = Rank.Two;
rank[2+13*i] = Rank.Three;
rank[3+13*i] = Rank.Four;
rank[4+13*i] = Rank.Five;
rank[5+13*i] = Rank.Six;
rank[6+13*i] = Rank.Seven;
rank[7+13*i] = Rank.Eight;
rank[8+13*i] = Rank.Nine;
rank[9+13*i] = Rank.Ten;
rank[10+13*i] = Rank.Jack;
rank[11+13*i] = Rank.Queen;
rank[12+13*i] = Rank.King;
}
for (int i=0; i<13; i++)
suit[i]=Suit.Spade;
for (int i=13; i<26; i++)
suit[i]= Suit.Heart;
for (int i=26; i<39; i++)
suit[i]= Suit.Club;
for (int i=39; i<52; i++)
suit[i]= Suit.Diamond;
top=0;
}
public static DeckOfCards getDeck()
{
if (d == null) d = new DeckOfCards();
return d;
}
public void shuffle()
{
int x, y;
Suit tempS;
Rank tempR;
Random rand=new Random();
for (int i=0; i<1000; ++i)
{
x=rand.nextInt(52);
y=rand.nextInt(52);
tempR=rank[x];
rank[x]=rank[y];
rank[y]=tempR;
tempS=suit[x];
suit[x]=suit[y];
suit[y]=tempS;
}
top=0;
}
public String deal()
{
String card="";
if (top>=52) card = "NoMoreCard";
else card = ""+rank[top]+suit[top];
top++;
return card;
}
}
class DeckOfCardsApp
{
public static void main(String[] args)
{
DeckOfCards d = DeckOfCards.getDeck();
System.out.print("New deck or shuffled deck? N/S: ");
char ch=Keyboard.nextChar();
if (ch=='S' || ch=='s') d.shuffle();
System.out.print("How many cards do you want?");
int n= Keyboard.nextInt();
for (int i=0; i<n; ++i)
System.out.println(d.deal());
}
}