-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameFunctions.java
More file actions
613 lines (552 loc) · 20.6 KB
/
GameFunctions.java
File metadata and controls
613 lines (552 loc) · 20.6 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class GameFunctions{
private Random random = new Random();
private Cards[] playerBoardCards = new Cards[9];
private Cards[] computerBoardCards = new Cards[9];
private int playerTotalPlayed = 0;
private int[] playerHandPlayed = new int[4];
private int playerDeckPlayed = 0;
private int computerTotalPlayed = 0;
private int[] computerHandPlayed = new int[4];
private int totalPlayed;
private int computerTotal;
private Cards[] shuffledCards = new Cards[40];
private Cards[] computerHand = new Cards[4];
private Cards[] playerHand = new Cards[4];
private Scanner sc = new Scanner(System.in);
private int playerTotal;
public Cards[] getShuffledCards() {
return shuffledCards;
}
public void setShuffledCards(Cards[] shuffledCards) {
this.shuffledCards = shuffledCards;
}
public int[] getPlayerHandPlayed() {
return playerHandPlayed;
}
public void setPlayerHandPlayed(int[] playerHandPlayed) {
this.playerHandPlayed = playerHandPlayed;
}
public int[] getComputerHandPlayed() {
return computerHandPlayed;
}
public void setComputerHandPlayed(int[] computerHandPlayed) {
this.computerHandPlayed = computerHandPlayed;
}
boolean stand = true;
boolean computerstand = true;
public GameFunctions(int totalPlayed,int[] computerHandPlayed,int[]playerHandPlayed,Cards[] shuff,Cards[] playerHand,Cards[] computerHand) {
this.totalPlayed = totalPlayed;
this.computerHandPlayed = computerHandPlayed;
this.playerHandPlayed = playerHandPlayed;
this.shuffledCards = shuff;
this.playerHand = playerHand;
this.computerHand= computerHand;
}
public GameFunctions(int totalPlayed ) {
this.totalPlayed = totalPlayed;
}
public Cards[] getPlayerBoardCards() {
return playerBoardCards;
}
public Cards[] getComputerBoardCards() {
return computerBoardCards;
}
public int getComputerTotal() {
return computerTotal;
}
public int getPlayerTotalPlayed() {
return playerTotalPlayed;
}
public int getComputerTotalPlayed() {
return computerTotalPlayed;
}
public int getPlayerTotal() {
return playerTotal;
}
public int getTotalPlayed() {
return totalPlayed;
}
public void setTotalPlayed(int totalPlayed) {
this.totalPlayed = totalPlayed;
}
public void setComputerstand(boolean computerstand) {
this.computerstand = computerstand;
}
public boolean isComputerstand() {
return computerstand;
}
public Cards[] startCreateCards()
{
Cards[][] gameDeck = new Cards[4][10];
return gamedeckCreator(gameDeck);
}
public void startText(){
System.out.println("");
System.out.println("*************************");
System.out.println("* BLUEJACK *");
System.out.println("*************************");
System.out.println("ENTER 1 TO PLAY THE GAME");
System.out.println("ENTER 2 TO END THIS GAME");
System.out.println("*************************");
System.out.print("Enter your choice: ");
}
public Cards[] gamedeckCreator(Cards[][] gameDeck){
for (int i = 0; i < 4; i++) {
for (int j = 0; j <10 ; j++) {
if(i==0) gameDeck[i][j] = new Cards( j+1, "B");
if(i==1) gameDeck[i][j] = new Cards( j+1, "Y");
if(i==2) gameDeck[i][j] = new Cards( j+1, "R");
if(i==3) gameDeck[i][j] = new Cards( j+1, "G");
}
}
return shuffleTheCards(gameDeck);
}
public Cards[] shuffleTheCards(Cards[][]gameDeck)
{
Cards[] shuffledCards = new Cards[40];
int color = 0;
int number = 0;
for (int i = 0; i < 40; i++) {
color = random.nextInt(4);
number = random.nextInt(10);
boolean toNextNumber = true;
while (toNextNumber) {
for (int j = 0; j < i+1; j++) {
if(shuffledCards[j]==gameDeck[color][number])
{
color = random.nextInt(4);
number = random.nextInt(10);
toNextNumber = true;
break;
}
else
{
toNextNumber = false;
}
}
}
shuffledCards[i] = gameDeck[color][number];
}
return shuffledCards;
}
public Cards[] secondStepDeckGenerator(Cards[] deck){
for (int i = 5; i < 8; i++) {
String color = "";
int colorI = random.nextInt(4);//0 B 1 Y 2 R 3 G
int numberI = random.nextInt(6)+1;
int signI = random.nextInt(2);
if(colorI ==0) color = "B";
if(colorI ==1) color = "Y";
if(colorI ==2) color = "R";
if(colorI ==3) color = "G";
if(signI==0)deck[i] = new Cards( +numberI, color);
if(signI==1)deck[i] = new Cards( -numberI, color);
}
return thirdStepDeckGenerator(deck);
}
public Cards[] thirdStepDeckGenerator(Cards[] deck){
boolean alreadyJokerSelected = false;
int jokerCard = random.nextInt(2);
for (int i = 8; i < 10; i++) {
int chance = random.nextInt(100);
if(chance<80){
String color = "";
int colorI = random.nextInt(4);//0 B 1 Y 2 R 3 G
int numberI = random.nextInt(6)+1;
int signI = random.nextInt(2);
if(colorI ==0) color = "B";
if(colorI ==1) color = "Y";
if(colorI ==2) color = "R";
if(colorI ==3) color = "G";
if(signI==0)deck[i] = new Cards( numberI, color);
if(signI==1)deck[i] = new Cards( -numberI, color);
}else{
if(alreadyJokerSelected==false)
{
if(jokerCard==0) deck[i] = new Cards(false);
if(jokerCard==1) deck[i] = new Cards(true);
alreadyJokerSelected = true;
}else
{
if(jokerCard==0) deck[i] = new Cards(true);
if(jokerCard==1) deck[i] = new Cards(false);
}
}
}
return handDeckGenerator(deck);
}
public Cards[] handDeckGenerator(Cards[]Deck){
Cards[] handDeck = new Cards[4];
int number = 0;
for (int i = 0; i < 4; i++) {
number = random.nextInt(10);
boolean toNextNumber = true;
while (toNextNumber) {
for (int j = 0; j < i+1; j++) {
if(handDeck[j]==Deck[number])
{
number = random.nextInt(10);
toNextNumber = true;
break;
}else
{
toNextNumber = false;
}
}
}
handDeck[i] = Deck[number];
Deck[number] = null;
}
return handDeck;
}
public Cards[] firstStepDeckGenerator(Cards[]shuffledDeck,boolean playerDeck)
{
Cards[] deck = new Cards[10];
if(playerDeck)
{
for (int i = 0; i < 5; i++) {
deck[i] = shuffledDeck[39-i];
shuffledDeck[39-i] = null;
}
}else
{
for (int i = 0; i < 5; i++) {
deck[i] = shuffledDeck[i];
shuffledDeck[i] = null;
}
}
return secondStepDeckGenerator(deck);
}
public void playText(Cards[] shuffledCards,Cards[] playerHand,Cards[] computerHand,boolean show){
this.shuffledCards = shuffledCards;
this.computerHand = computerHand;
this.playerHand = playerHand;
playerTotal = 0;
computerTotal = 0;
String[] playerHandStrings = new String[4];
String[] computerHandStrings = new String[4];
String[] computerBoard = new String[9];
String[] playerBoard = new String[9];
for (int i = 0; i < 4; i++) {
if(1==playerHandPlayed[i])
{
playerHandStrings[i] = " ";
}
else
{
playerHandStrings[i] = playerHand[i].getCard();
}
}
for (int i = 0; i < 4; i++) {
if(1==computerHandPlayed[i])
{
computerHandStrings[i] = " ";
}
else
{
computerHandStrings[i] = "***";
}
}
for (int i = 0; i < 9; i++) {
if(i<computerTotalPlayed)
{
computerBoard[i] = computerBoardCards[i].getCard();
computerTotal += computerBoardCards[i].getCardNumber();
}
else
{
computerBoard[i] = " ";
}
}
for (int i = 0; i < 9; i++) {
if(i<playerTotalPlayed)
{
playerBoard[i] = playerBoardCards[i].getCard();
playerTotal += playerBoardCards[i].getCardNumber();
}
else
{
playerBoard[i] = " ";
}
}
if(show)
{
System.out.println("");
System.out.println(" -------------------------------");
System.out.println("Computer's hand-->"+" "+computerHandStrings[0]+" "+computerHandStrings[1]+" "+computerHandStrings[2]+" "+computerHandStrings[3]);
System.out.println(" -------------------------------");
System.out.println("Computer's Board->"+" "+computerBoard[0]+" "+computerBoard[1]+" "+computerBoard[2]+" "+computerBoard[3]+" "+computerBoard[4]+" "+computerBoard[5]+" "+computerBoard[6]+" "+computerBoard[7]+" "+computerBoard[8]);
System.out.println(" -------------------------------");
System.out.println("Player's Board--->"+" "+playerBoard[0]+" "+playerBoard[1]+" "+playerBoard[2]+" "+playerBoard[3]+" "+playerBoard[4]+" "+playerBoard[5]+" "+playerBoard[6]+" "+playerBoard[7]+" "+playerBoard[8]);
System.out.println(" -------------------------------");
System.out.println("Player's hand---->"+" "+playerHandStrings[0]+" "+playerHandStrings[1]+" "+playerHandStrings[2]+" "+playerHandStrings[3]);
System.out.println(" -------------------------------");
System.out.println("");
System.out.println("your deck: "+playerTotal);
System.out.println("Computer deck: "+ computerTotal);
}
}
public void startTurn()
{
int choice;
while (true) {
try {
System.out.println("");
System.out.println("1) Type 1 for pick card from game deck");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice != 1) {
System.out.println("You entered a value other than 1. Try again.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("");
System.out.println("You have entered unvalid value. Please enter an integer value.");
sc.nextLine();
}
}
if(choice==1)
{
System.out.println("");
System.out.println("You picked card from game deck");
System.out.println("");
playerBoardCards[playerTotalPlayed] = shuffledCards[totalPlayed];
shuffledCards[totalPlayed] = null;
totalPlayed++;
playerDeckPlayed++;
playerTotalPlayed++;
playText(shuffledCards, playerHand, computerHand,true);
thirdStep();
}
}
public void thirdStep()
{
int choice;
while (true) {
try {
System.out.println("");
System.out.println("1) Type 1 for end turn");
System.out.println("2) Type 2 for pick cards from your hand");
System.out.println("3) Type 3 for stand");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice != 1 && choice != 2&&choice != 3) {
System.out.println("You entered a value other than 1,2 or 3. Try again.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("");
System.out.println("You have entered unvalid value. Please enter an integer value.");
sc.nextLine();
}
}
if(choice==1)
{
System.out.println("");
System.out.println("You chosed to end the turn");
System.out.println("");
playText(shuffledCards, playerHand, computerHand,true);
}
if(choice==2)
{
pickAgain();
}
if(choice==3)
{
if(playerTotalPlayed==0)
{
System.out.println("");
System.out.println("You can not stand in first turn");
System.out.println("");
thirdStep();
}else
{
System.out.println("");
System.out.println("You chosed to stand");
System.out.println("");
stand = false;
}
}
}
public void pickAgain()
{
int choice;
while (true) {
try {
System.out.println("");
System.out.println("1)Please enter which row of cards you would like to choose card from hand");
System.out.println("2)press 0 to go back to previous selection menu");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice<0||choice>4) {
System.out.println("You entered unvalid number or you selected card which already selected pls try again");
continue;
}
if(choice==0)
{
thirdStep();
return;
}
if(playerHandPlayed[choice-1]==1)
{
System.out.println("");
System.out.println("You entered unvalid number or you selected card which already selected pls try again");
System.out.println("");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("");
System.out.println("You have entered unvalid value. Please enter an integer value.");
sc.nextLine();
}
}
if(playerHand[choice-1].isJoker()==true)
{
if(playerHand[choice-1].isX2()==true)
{
System.out.println("");
System.out.println("YOU USED X2");
System.out.println("");
playerBoardCards[playerTotalPlayed-1].setCardNumber(playerBoardCards[playerTotalPlayed-1].getCardNumber()*2);
playerHand[choice-1]=null;
playerHandPlayed[choice-1] = 1;
playText(shuffledCards, playerHand, computerHand,true);
}else
{
System.out.println("");
System.out.println("YOU USED +/-");
System.out.println("");
playerBoardCards[playerTotalPlayed-1].setCardNumber(playerBoardCards[playerTotalPlayed-1].getCardNumber()*-1);
playerHandPlayed[choice-1] = 1;
playerHand[choice-1]=null;
playText(shuffledCards, playerHand, computerHand,true);
}
}
else
{
System.out.println("");
System.out.println("You picked card from your hand");
System.out.println("");
playerBoardCards[playerTotalPlayed] = playerHand[choice-1];
playerHandPlayed[choice-1] = 1;
playerDeckPlayed++;
playerHand[choice-1]=null;
playerTotalPlayed++;
playText(shuffledCards, playerHand, computerHand,true);
}
while (true) {
try {
System.out.println("");
System.out.println("1) Type 1 for end turn");
System.out.println("2) Type 2 for stand");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
if (choice != 1 && choice != 2) {
System.out.println("You entered a value other than 1,2 Try again.");
continue;
}
break;
} catch (InputMismatchException e) {
System.out.println("");
System.out.println("You have entered unvalid value. Please enter an integer value.");
sc.nextLine();
}
}
if(choice==1)
{
System.out.println("");
System.out.println("You chosed to end the turn");
System.out.println("");
playText(shuffledCards, playerHand, computerHand,true);
}
if(choice==2)
{
System.out.println("");
System.out.println("You chosed to stand");
System.out.println("");
stand = false;
}
}
public void setstand(boolean stand)
{
this.stand = stand;
}
public boolean getstand()
{
return this.stand;
}
public void startToThink()
{
playText(shuffledCards, playerHand, computerHand,false);
computerBoardCards[computerTotalPlayed] = shuffledCards[totalPlayed];
shuffledCards[totalPlayed] = null;
totalPlayed++;
computerTotalPlayed++;
playText(shuffledCards, playerHand, computerHand,false);
if(computerTotal<=20&&computerTotal>=17)
{
computerstand = false;
}
if(computerTotal>20)
{
int min= 999;
int whichHandCard = 999;
for (int i = 0; i < 4; i++) {
if(computerHand[i]!=null)
{
if((computerTotal+computerHand[i].getCardNumber())<=20&&computerHandPlayed[i]==0&&computerHand[i].isJoker()==false&&computerHand[i]!=null)
{
if(computerHand[i].getCardNumber()<min&&computerHand[i]!=null)
min = computerHand[i].getCardNumber();
whichHandCard = i;
}
}
}
if(whichHandCard!=999&&computerHand[whichHandCard]!=null)
{
if(computerHand[whichHandCard]!=null)
{
computerBoardCards[computerTotalPlayed] = computerHand[whichHandCard];
computerHand[whichHandCard] = null;
computerHandPlayed[whichHandCard] = 1;
computerTotalPlayed++;
}
}
else
{
computerstand = false;
}
}else
{
for (int i = 0; i < 4; i++) {
if(computerHand[i]!=null)
{
if((20-computerTotal)==computerHand[i].getCardNumber()&&computerHand[i].isJoker()==false&&computerHand[i].getCardNumber()>0&&computerHand[i]!=null)
{
computerBoardCards[computerTotalPlayed] = computerHand[i];
computerHandPlayed[i] = 1;
computerTotalPlayed++;
computerstand = false;
computerHand[i] = null;
break;
}
}
}
}
playText(shuffledCards, playerHand, computerHand,false);
if(computerTotal<=20&&computerTotal>=17)
{
computerstand = false;
}
if (computerTotal>20) {
computerstand = false;
}
if(stand==false&&computerTotal>playerTotal) computerstand= false;
}
}