-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeckOfCards.java
74 lines (73 loc) · 2.42 KB
/
DeckOfCards.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//8 Homework Part-1
//Zawaad M Shah
//DeckOfCards.java
//A DeckOfCards object represents a deck of ordinary playing cards. The top card is dealt
//each time the method deal is called. A dealt card will not be reused until the DeckOfCards
//is shuffled.
import java.util.Random;
public class DeckOfCards
{
private String[] rank;
private String[] suit;
private int top;
//Post:This DeckOfCards initialized to a new deck; the cards are ordered from AceSpades
// to KingDiamond as in a new deck; top initialized to 0.
public DeckOfCards()
{
rank=new String[52];
suit=new String[52];
for(int i=0;i<4;++i)
{
rank[0+13*i]="Ace";
rank[1+13*i]="Two";
rank[2+13*i]="Three";
rank[3+13*i]="Four";
rank[4+13*i]="Five";
rank[5+13*i]="Six";
rank[6+13*i]="Seven";
rank[7+13*i]="Eight";
rank[8+13*i]="Nine";
rank[9+13*i]="Ten";
rank[10+13*i]="Jack";
rank[11+13*i]="Queen";
rank[12+13*i]="King";
}
for(int i=0;i<13;++i)
suit[i]="Spade";
for(int i=13;i<26;++i)
suit[i]="Heart";
for(int i=26;i<39;++i)
suit[i]="Club";
for(int i=39;i<52;++i)
suit[i]="Diamond";
top=0;
}
//Post:This DeckOfCards thoroughly shuffled; top set to 0.
public void shuffle()
{
Random r=new Random();
for(int i=0;i<1000;++i)
{
int x=r.nextInt(52);
int y=r.nextInt(52);
String temp=rank[x];
rank[x]=rank[y];
rank[y]=temp;
temp=suit[x];
suit[x]=suit[y];
suit[y]=temp;
}
}
//Post: Top incremented by 1.
//Return:The top card of this DeckOfCards as a String such as "AceHeart", "TwoSpade",
// "TenDiamond", "KingClub", If top>=52, "NoMoreCard" is returned.
public String deal()
{
String result="";
if(top<52) result=rank[top]+suit[top];
else result="No More Card";
top++;
return result;
}
}