-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPresident.java
447 lines (415 loc) · 14.6 KB
/
President.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import java.util.Random;
/*
* Removed redundant methods and variables to change how program interacts with GUI
* -Removed updatePlayerHand()
* -Removed gameFlag
* -Changed method checkPlayerRank
* -Changed way ai players pick cards, instead they now pick a random index and the program checks if the card at that index was already picked if so then it loops through until a valid card is chosen. If the choice is skipped then the card stays and is not picked. 0 = card was not picked, 1 = card was picked
* -Changed method checkforNewPile() method now instead creates a new pile after 4 rounds, but still creates a new pile if the top card is an ace (ace = 14)
*/
public class President
{
//variables initialized in the constructor
DeckofCards deck;
Player player;
Player aiPlayer1;
Player aiPlayer2;
Player aiPlayer3;
Card[] pileDeck;
int cardsDrawn;
//needed variables
int playerChoice;
String[] socialRank = {"player", "aiPlayer1", "aiPlayer2", "aiPlayer3"};
int socialRankIndex = 0;
int aiPlayerChoice;
int newPileIndex = 0;
int[] cardsPicked = new int[13];
President() //call to constructor starts the game
{
this.deck = new DeckofCards();
Card tempCard = new Card(2, "Hearts");
this.pileDeck = new Card[52];
this.pileDeck[0] = tempCard;
deck.ShuffleDeck(deck.Deck);
this.player = new Player(13);
this.aiPlayer1 = new Player(13);
this.aiPlayer2 = new Player(13);
this.aiPlayer3 = new Player(13);
this.cardsDrawn = 0;
for(int i = 0; i < cardsPicked.length; i++)
cardsPicked[i] = 0;
dealCards(this.cardsDrawn, this.deck, this.player.playerHand); //deals the cards to all the players
dealCards(this.cardsDrawn, this.deck, this.aiPlayer1.playerHand);
dealCards(this.cardsDrawn, this.deck, this.aiPlayer2.playerHand);
dealCards(this.cardsDrawn, this.deck, this.aiPlayer3.playerHand);
switch2Cards(); //call to methods needed to switch necessary cards
switch1Card();
socialRank = new String[4]; //resets the social rank between players
}
public void gameSession()
{
if(checkforNewPile()) //creates a new pile of cards if current one cannot be used anymore
{
Card tempCard = new Card(2, "Hearts"); //sets the new top card to the lowest valued card
this.pileDeck = new Card[52];
this.pileDeck[0] = tempCard;
}
}
public void checkPlayerRank(String rank) //assigns player rank based on gameRounds. The later the game the lower the position the player gets
{
if(socialRankIndex == 0) //if the player name passed through is first they are the president
socialRank[socialRankIndex] = rank;
else
{
for(int i = 0; i < socialRankIndex; i++) //else first check to see if the player name passed through is already within the string array
{
if(rank == socialRank[i]) //if it's already in the array exit the method
return;
else
socialRank[socialRankIndex] = rank; //else add the player name at the next rank index
}
}
this.socialRankIndex++; //increment the index
}
public void dealCards(int cardsDrawn, DeckofCards deck, PlayerHand hand) //gives players 13 cards
{
for(int i = 0; i < 13; i++)
{
hand.Hand[i] = deck.Deck[cardsDrawn];
cardsDrawn++;
}
this.cardsDrawn = cardsDrawn;
}
public void currentPile(Card cardPlayed) //creates a pile of cards so we can keep track of the current card at the top
{
pileDeck[0] = cardPlayed;
}
public void playerTurn() //real players turn where they determine what card to play
{
Card tempCard = this.player.playerHand.Hand[playerChoice];
if(this.player.playerHand.Hand[playerChoice].cardNumber >= pileDeck[0].cardNumber)
currentPile(tempCard);
else
{
return;
}
}
public void aiPlayerTurn(Player aiPlayer) //this is where the AI player determines what their move will be. If they play an invalid card it skips their turn
{
Random rand = new Random();
Card tempCard;
while(true)
{
this.aiPlayerChoice = rand.nextInt(13);
if(cardsPicked[aiPlayerChoice] == 0)
if(aiPlayer.playerHand.Hand[aiPlayerChoice].cardNumber < this.pileDeck[0].cardNumber)
{
return;
}
else
{
cardsPicked[aiPlayerChoice] = 1;
tempCard = aiPlayer.playerHand.Hand[aiPlayerChoice];
currentPile(tempCard);
return;
}
else
continue;
}
}
public boolean checkforNewPile() //looks to see if any players have any playable cards and if not creates a new deck or if the top card is a 14 (ace) then it creates a new pile
{
if(this.pileDeck[0].cardNumber == 14) //if the top card is an ace create a new pile
{
newPileIndex = 0;
return true;
}
if(newPileIndex == 4) //if 4 rounds have passed create a new pile
{
newPileIndex = 0;
return true;
}
else //else keep the pile increment the index
{
newPileIndex++;
return false;
}
}
public void switch2Cards() //switches the 2 best and worst cards between the president and the Scum
{
Card bestCard = null;
Card worstCard = null;
Card tempCard;
int length = this.player.playerHand.Hand.length;
for(int count = 0; count < 2; count++)
{
if(socialRank[0] == "player")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.player.playerHand.Hand[j].cardNumber > this.player.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.player.playerHand.Hand[j];
this.player.playerHand.Hand[j] = this.player.playerHand.Hand[j + 1];
this.player.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.player.playerHand.Hand[count];
}
if(socialRank[0] == "aiPlayer1")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer1.playerHand.Hand[j].cardNumber > this.aiPlayer1.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer1.playerHand.Hand[j];
this.aiPlayer1.playerHand.Hand[j] = this.aiPlayer1.playerHand.Hand[j + 1];
this.aiPlayer1.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer1.playerHand.Hand[count];
}
if(socialRank[0] == "aiPlayer2")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer2.playerHand.Hand[j].cardNumber > this.aiPlayer2.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer2.playerHand.Hand[j];
this.aiPlayer2.playerHand.Hand[j] = this.aiPlayer2.playerHand.Hand[j + 1];
this.aiPlayer2.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer2.playerHand.Hand[count];
}
if(socialRank[0] == "aiPlayer3")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer3.playerHand.Hand[j].cardNumber > this.aiPlayer3.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer1.playerHand.Hand[j];
this.aiPlayer3.playerHand.Hand[j] = this.aiPlayer3.playerHand.Hand[j + 1];
this.aiPlayer3.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer3.playerHand.Hand[count];
}
if(socialRank[3] == "player")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.player.playerHand.Hand[j].cardNumber < this.player.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.player.playerHand.Hand[j + 1];
this.player.playerHand.Hand[j + 1] = this.player.playerHand.Hand[j];
this.player.playerHand.Hand[j] = tempCard;
}
bestCard = this.player.playerHand.Hand[count];
}
if(socialRank[3] == "aiPlayer1")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer1.playerHand.Hand[j].cardNumber < this.aiPlayer1.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer1.playerHand.Hand[j + 1];
this.aiPlayer1.playerHand.Hand[j + 1] = this.aiPlayer1.playerHand.Hand[j];
this.aiPlayer1.playerHand.Hand[j] = tempCard;
}
bestCard = this.aiPlayer1.playerHand.Hand[count];
}
if(socialRank[3] == "aiPlayer2")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer2.playerHand.Hand[j].cardNumber < this.aiPlayer2.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer2.playerHand.Hand[j + 1];
this.aiPlayer2.playerHand.Hand[j + 1] = this.aiPlayer2.playerHand.Hand[j];
this.aiPlayer2.playerHand.Hand[j] = tempCard;
}
bestCard = this.aiPlayer2.playerHand.Hand[count];
}
if(socialRank[3] == "aiPlayer3")
{
for(int i = 0; i < length - 1; i++)
{
for(int j = 0; j < length - i - 1; j++)
{
if(this.aiPlayer3.playerHand.Hand[j].cardNumber < this.aiPlayer3.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer3.playerHand.Hand[j + 1];
this.aiPlayer3.playerHand.Hand[j + 1] = this.aiPlayer3.playerHand.Hand[j];
this.aiPlayer3.playerHand.Hand[j] = tempCard;
}
}
}
bestCard = this.aiPlayer3.playerHand.Hand[count];
}
if(socialRank[0] == "player")
{
this.player.playerHand.Hand[0] = bestCard;
}
if(socialRank[0] == "aiPlayer1")
{
this.aiPlayer1.playerHand.Hand[0] = bestCard;
}
if(socialRank[0] == "aiPlayer2")
{
this.aiPlayer2.playerHand.Hand[0] = bestCard;
}
if(socialRank[0] == "aiPlayer3")
{
this.aiPlayer3.playerHand.Hand[0] = bestCard;
}
if(socialRank[3] == "player")
{
this.player.playerHand.Hand[0] = worstCard;
}
if(socialRank[3] == "aiPlayer1")
{
this.aiPlayer1.playerHand.Hand[0] = worstCard;
}
if(socialRank[3] == "aiPlayer2")
{
this.aiPlayer2.playerHand.Hand[0] = worstCard;
}
if(socialRank[3] == "aiPlayer3")
{
this.aiPlayer3.playerHand.Hand[0] = worstCard;
}
}
}
public void switch1Card() //switches the best and worst card between the Vice-President and the Civilian
{
Card bestCard = null;
Card worstCard = null;
Card tempCard;
int length = this.player.playerHand.Hand.length;
if(socialRank[1] == "player")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.player.playerHand.Hand[j].cardNumber > this.player.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.player.playerHand.Hand[j];
this.player.playerHand.Hand[j] = this.player.playerHand.Hand[j + 1];
this.player.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.player.playerHand.Hand[0];
}
if(socialRank[1] == "aiPlayer1")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer1.playerHand.Hand[j].cardNumber > this.aiPlayer1.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer1.playerHand.Hand[j];
this.aiPlayer1.playerHand.Hand[j] = this.aiPlayer1.playerHand.Hand[j + 1];
this.aiPlayer1.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer1.playerHand.Hand[0];
}
if(socialRank[1] == "aiPlayer2")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer2.playerHand.Hand[j].cardNumber > this.aiPlayer2.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer2.playerHand.Hand[j];
this.aiPlayer2.playerHand.Hand[j] = this.aiPlayer2.playerHand.Hand[j + 1];
this.aiPlayer2.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer2.playerHand.Hand[0];
}
if(socialRank[1] == "aiPlayer3")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer3.playerHand.Hand[j].cardNumber > this.aiPlayer3.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer3.playerHand.Hand[j];
this.aiPlayer3.playerHand.Hand[j] = this.aiPlayer3.playerHand.Hand[j + 1];
this.aiPlayer3.playerHand.Hand[j + 1] = tempCard;
}
worstCard = this.aiPlayer3.playerHand.Hand[0];
}
if(socialRank[2] == "player")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.player.playerHand.Hand[j].cardNumber < this.player.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.player.playerHand.Hand[j + 1];
this.player.playerHand.Hand[j + 1] = this.player.playerHand.Hand[j];
this.player.playerHand.Hand[j] = tempCard;
}
bestCard = this.player.playerHand.Hand[0];
}
if(socialRank[2] == "aiPlayer1")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j < length - i - 1; j++)
if(this.aiPlayer1.playerHand.Hand[j].cardNumber < this.aiPlayer1.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer1.playerHand.Hand[j + 1];
this.aiPlayer1.playerHand.Hand[j + 1] = this.aiPlayer1.playerHand.Hand[j];
this.aiPlayer1.playerHand.Hand[j] = tempCard;
}
bestCard = this.aiPlayer1.playerHand.Hand[0];
}
if(socialRank[2] == "aiPlayer2")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j > length - i - 1; j++)
if(this.aiPlayer2.playerHand.Hand[j].cardNumber < this.aiPlayer2.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer2.playerHand.Hand[j + 1];
this.aiPlayer2.playerHand.Hand[j + 1] = this.aiPlayer2.playerHand.Hand[j];
this.aiPlayer2.playerHand.Hand[j] = tempCard;
}
bestCard = this.aiPlayer2.playerHand.Hand[0];
}
if(socialRank[2] == "aiPlayer3")
{
for(int i = 0; i < length - 1; i++)
for(int j = 0; j > length - i - 1; j++)
if(this.aiPlayer3.playerHand.Hand[j].cardNumber < this.aiPlayer3.playerHand.Hand[j + 1].cardNumber)
{
tempCard = this.aiPlayer3.playerHand.Hand[j + 1];
this.aiPlayer3.playerHand.Hand[j + 1] = this.aiPlayer3.playerHand.Hand[j];
this.aiPlayer3.playerHand.Hand[j] = tempCard;
}
bestCard = this.aiPlayer3.playerHand.Hand[0];
}
if(socialRank[1] == "player")
{
this.player.playerHand.Hand[0] = bestCard;
}
if(socialRank[1] == "aiPlayer1")
{
this.aiPlayer1.playerHand.Hand[0] = bestCard;
}
if(socialRank[1] == "aiPlayer2")
{
this.aiPlayer2.playerHand.Hand[0] = bestCard;
}
if(socialRank[1] == "aiPlayer3")
{
this.aiPlayer3.playerHand.Hand[0] = bestCard;
}
if(socialRank[2] == "player")
{
this.player.playerHand.Hand[0] = worstCard;
}
if(socialRank[2] == "aiPlayer1")
{
this.aiPlayer1.playerHand.Hand[0] = worstCard;
}
if(socialRank[2] == "aiPlayer2")
{
this.aiPlayer2.playerHand.Hand[0] = worstCard;
}
if(socialRank[2] == "aiPlayer3")
{
this.aiPlayer3.playerHand.Hand[0] = worstCard;
}
}
}