-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
211 lines (193 loc) · 5.2 KB
/
Game.java
File metadata and controls
211 lines (193 loc) · 5.2 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.Random;
class Game {
public static void main (String[] args) {
Tableau g1 = new Tableau();
g1.play();
}
}
final class Card
{
// RANK NAME. Printable names of card ranks.
private static final String [] rankName =
{
"ace", // 0
"two", // 1
"three", // 2
"four", // 3
"five", // 4
"six", // 5
"seven", // 6
"eight", // 7
"nine", // 8
"ten", // 9
"jack", // 10
"queen", // 11
"king" // 12
};
// SUIT NAME. Printable names of card suits.
private static final String [] suitName =
{
"clubs", // 0
"diamonds", // 1
"hearts", // 2
"spades" // 3
};
private int rank; // Rank of this CARD, between 0 and 12.
private int suit; // Suit of this CARD, between 0 and 3.
// CARD. Constructor. Make a new CARD, with a given RANK and SUIT.
public Card(int rank, int suit)
{
if (0 <= rank && rank <= 12 && 0 <= suit && suit <= 3)
{
this.rank = rank;
this.suit = suit;
}
else
{
throw new IllegalArgumentException("Illegal rank or suit.");
}
}
// GET RANK. Return the RANK of this CARD.
public int getRank()
{
return rank;
}
// GET SUIT. Return the SUIT of this CARD.
public int getSuit()
{
return suit;
}
// TO STRING. Return a STRING that describes this CARD. For printing only!
public String toString()
{
return rankName[rank] + " (" + rank + ") of " + suitName[suit];
}
}
class Deck {
Card deck[];
int count;
Random r = new Random();
public Deck() {
int suit = 0;
int rank = 0;
deck = new Card[52];
for (int i = 0; i < 52; i++) {
if (i % 13 == 0 && i != 0) {
suit++;
rank = 0;
}
deck[i] = new Card(rank, suit);
rank++;
}
count = 52;
}
public Card deal() {
if (isEmpty()) {
throw new IllegalStateException("There are no cards left in the deck.");
}
else {
Card last;
last = deck[count - 1];
pop();
return last;
}
}
public void shuffle() {
if (count < 52) {
throw new IllegalStateException("Cannot shuffle the deck after dealing has started.");
}
else {
int j;
Card temp;
for (int i = 51; i >= 0; i--) {
j = r.nextInt(i + 1);
if (j != i) {
temp = deck[j];
deck[j] = deck[i];
deck[i] = temp;
}
}
}
}
public void pop() {
if (isEmpty()) {
throw new IllegalStateException("Cannot pop from an empty array");
}
else {
count -= 1;
deck[count] = null;
}
}
public boolean isEmpty() {
return count == 0;
}
}
class Pile {
private class Layer {
Card card;
Layer next;
public Layer(Card card, Layer next) {
this.card = card;
this.next = next;
}
}
private Layer head;
public Pile() {
head = null;
}
public void add(Card card) {
head = new Layer(card, head);
}
public Card draw() {
if (isEmpty()) {
throw new IllegalStateException("Cannot draw from an empty pile");
}
else {
Card temp = head.card;
head = head.next;
return temp;
}
}
public boolean isEmpty() {
return head == null;
}
}
class Tableau {
Pile[] pileArray = new Pile[13];
Deck gameDeck = new Deck();
public Tableau() {
Deck gameDeck = new Deck();
gameDeck.shuffle();
for (int i = 0; i < pileArray.length; i++) {
pileArray[i] = new Pile();
for (int j = 0; j < 4; j++) {
pileArray[i].add(gameDeck.deal());
}
}
}
public void play() {
int p = 0;
Card c1 = pileArray[p].draw();
System.out.println("Got " + c1 + " from pile " + p);
Card c2;
while (!pileArray[p].isEmpty()) {
c2 = pileArray[p].draw();
System.out.println("Got " + c2 + " from pile " + p);
if (c1.getSuit() == c2.getSuit()) {
p = c1.getRank();
c1 = c2;
}
else {
p = c2.getRank();
c1 = c2;
}
}
for (int i = 0; i < pileArray.length; i++) {
if (!pileArray[i].isEmpty()) {
System.out.println("Pile " + p + " is empty. We lost!");
return;
}
}
System.out.println("We won!");
}
}