-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPileOfCards.java
277 lines (245 loc) · 7.82 KB
/
PileOfCards.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Class PileOfCards for FinalProject
// Program PlayingCards by Ray Arias
class PileOfCards extends Shuffleable {
// Default Constructor for Class PileOfCards
PileOfCards() {
super();
}
// Deep Copy Constructor for Class PileOfCards
// Calls deep copy constructor of Class Shuffleable
// which calls its deepClone(Shuffleable) method
PileOfCards(PileOfCards copy) {
super(copy);
}
// Takes PileOfCards instance and removes a random
// number of cards from it and places them into
// a new Shuffleable object that is returned
@Override
public Shuffleable cut() {
Shuffleable result = new PileOfCards();
// int rand is generated by Class Shuffleable's
// static getRandomInt(int) : int method
int rand = Shuffleable.getRandomInt(this.quantity);
try {
// Use rand to call this.cut(int) method
result = this.cut(rand);
} catch (NumberOutOfRangeException noor) {
noor.printStackTrace();
}
return result;
}
// Takes PileOfCard instance and removes a given
// number of cards (given by int location) and
// places them into a new Shuffleable object
// that is returned
@Override
public Shuffleable cut(int location) throws NumberOutOfRangeException {
Shuffleable poc = new PileOfCards();
if (location < 1) {
throw new NumberOutOfRangeException("Value for int location (" + location
+ ") is nonpositive!");
} else {
// Beginning at given location take the card
// from this location of the current PileOfCards
// instance and make it the top card of the new
// Shuffleable object and do this while i > 0
// Note: First card taken from current instance
// is at position N, second card taken is at
// position N - 1, next one is at N - 2, and so
// on, until the last one at position 1.
for (int i = location; i > 0; i--) {
try {
poc.addCardToTop(this.takeNthCard(i));
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
} catch (ReachingBeyondBottomOfPileException bbp) {
bbp.printStackTrace();
}
}
}
return poc;
}
// This method takes the Cards from a
// Shuffleable object places them in
// bottom of a new Shuffleable object
// object called uncut. Then all the
// Cards in the current instance of
// PileOfCards are added on top of
// the uncut object. The current in-
// stance is deepCloned from the
// uncut object.
@Override
public void uncut(Shuffleable poc) {
Shuffleable uncut = new PileOfCards();
int q1 = poc.getQuantity();
int q2 = this.getQuantity();
for (int i = 0; i < q1; i++) {
try {
uncut.addCardToTop(poc.takeBottomCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
for (int i = 0; i < q2; i++) {
try {
uncut.addCardToTop(this.takeBottomCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
this.dispose();
try {
this.deepClone(uncut);
} catch (NumberOutOfRangeException noor) {
noor.printStackTrace();
}
}
// This method takes a card from the bottom
// of the current instance of PileOfCards,
// places it at the top of a new Shuffleable
// instance, then takes a card from the
// bottom of the Shuffleable instance passed
// in as a parameter, and places it at the
// top of the new instance until the object
// with fewer cards runs out. At this point,
// the method just takes the remaining cards
// from the bottom of the old instance with
// the greater number of cards and places
// one on the top of the new instance until
// no more cards remain in the remaining old
// instance. Thus, this method reproduces
// virtually what occurs when two piles of
// cards are physically shuffled downward
// into one new pile of cards. The new
// instance is then copied into the current
// instance, using the deepClone(Shuffleable)
// method in the Shuffleable superclass.
@Override
public void shuffleDown(Shuffleable poc) {
Shuffleable shuffled = new PileOfCards();
int q1 = this.quantity;
int q2 = poc.quantity;
int greatr, lesser;
if (q1 > q2) {
greatr = q1;
lesser = q2;
} else {
greatr = q2;
lesser = q1;
}
for (int i = 0; i < lesser; i++) {
try {
shuffled.addCardToTop(this.takeBottomCard());
shuffled.addCardToTop(poc.takeBottomCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
for (int i = lesser; i < greatr; i++) {
if (greatr == q1) {
try {
shuffled.addCardToTop(this.takeBottomCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
} else { // (greatr == q2)
try {
shuffled.addCardToTop(poc.takeBottomCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
}
this.dispose();
try {
this.deepClone(shuffled);
} catch (NumberOutOfRangeException noor) {
noor.printStackTrace();
}
}
// This method works like
// shuffleDown(Shuffleable) except that the
// Card objects from both old instances are
// placed into the new instance in exactly
// the reverse order. Specifically, a card
// from the top of the current instance of
// PileOfCards is taken and placed at the
// bottom of a new Shuffleable instance, then
// another card is taken from the top of the
// Shuffleable instance passed in as a
// parameter, and placed at the bottom of
// the new instance until the object with
// fewer cards runs out. At this point, the
// remaining cards are taken from the top of
// the old instance with the greater number
// of cards and placed at the bottom of the
// new instance until no more cards remain in
// the remaining old instance. This reproduces
// virtually what occurs when two piles of
// are physically shuffled upward into one new
// pile. The new instance is then copied into
// the current instance, using the
// deepClone(Shuffleable) method in the
// Shuffleable superclass.
@Override
public void shuffleUp(Shuffleable poc) {
Shuffleable shuffled = new PileOfCards();
int q1 = this.quantity;
int q2 = poc.quantity;
int greatr, lesser;
if (q1 >= q2) {
greatr = q1;
lesser = q2;
} else {
greatr = q2;
lesser = q1;
}
for (int i = 0; i < lesser; i++) {
try {
shuffled.addCardToBottom(this.takeTopCard());
shuffled.addCardToBottom(poc.takeTopCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
for (int i = lesser; i < greatr; i++) {
if (greatr == q1) {
try {
shuffled.addCardToBottom(this.takeTopCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
} else { // (greatr == q2)
try {
shuffled.addCardToBottom(poc.takeTopCard());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
}
this.dispose();
try {
this.deepClone(shuffled);
} catch (NumberOutOfRangeException noor) {
noor.printStackTrace();
}
}
// This method iterates through the entire
// current instance of PileOfCards and displays
// each card in it.
@Override
public void show() {
int p;
int q = this.quantity;
String format = "%2d: %s \n";
for (int i = 0; i < q; i++) {
p = i + 1;
try {
TextUI.dispf(format, p, takeTopCard().toString());
} catch (TakingFromEmptyPileException ep) {
ep.printStackTrace();
}
}
TextUI.newln();
}
}