-
Notifications
You must be signed in to change notification settings - Fork 0
/
Games.java
753 lines (686 loc) · 24.5 KB
/
Games.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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import java.util.Scanner;
public class Games
{
static String s, str = "", z; char a, b; static long l;
static int i, box = 0; static char play = 'r';
static char wrong[] = new char [8]; int w = 0;
Scanner sc = new Scanner (System.in);
void display(String y)
{
int j;
for(i=0; i<20; i++)
for(l=0; l<10000000; l++);
System.out.print("\fWelcome ");
for(i=0; i<20; i++)
for(l=0; l<10000000; l++);
System.out.print("to ");
for(i=0; i<15; i++)
for(l=0; l<10000000; l++);
for(j=0; j<y.length(); j++)
{
for(i=0; i<5; i++)
for(l=0; l<10000000; l++);
System.out.print(y.charAt(j));
}
for(i=0; i<5; i++)
for(l=0; l<10000000; l++);
System.out.println("!!");
}
// HANGMAN
void hanger(int ch)
{
if(ch==8)
{
System.out.println("\t _______");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==7)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==6)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==5)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t | |");
System.out.println("\t | |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==4)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t | /|");
System.out.println("\t | |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==3)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t | /|\\");
System.out.println("\t | |");
System.out.println("\t |");
System.out.println("\t |___________|");
}
else if(ch==2)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t | /|\\");
System.out.println("\t | |");
System.out.println("\t | /");
System.out.println("\t |___________|");
}
else if(ch==1)
{
System.out.println("\t _______");
System.out.println("\t | |");
System.out.println("\t | O");
System.out.println("\t | /|\\");
System.out.println("\t | |");
System.out.println("\t | / \\");
System.out.println("\t |___________|");
}
else if(ch==0)
{
System.out.println("\f\t _______");
System.out.println("\t | |");
System.out.println("\t | " + (char)216);
System.out.println("\t | /|\\");
System.out.println("\t | |");
System.out.println("\t | / \\");
System.out.println("\t |___________|");
}
}
void Rules_H()
{
display("HANGMAN");
for(i=0; i<35; i++)
for(l=0; l<10000000; l++);
System.out.println("\n Rules:\n- One player enters the word(s).\n- All vowels will be shown.\n- Other player(s) will guess each consonant.\n- After his/her chance, another player will give the word.\n- A guessing player loses his chance after 8 wrong guesses.");
for(i=0; i<50; i++)
for(l=0; l<10000000; l++);
}
void input()
{
System.out.print("\fWord provided by opponent: "); String y;
do
{
y = sc.nextLine();
if(y.length()>=1)
s = y;
}
while(!(y.length()>=1));
s = s.toUpperCase();
System.out.println("\f");
}
void FW()
{
for(i=0; i<s.length(); i++)
{
a = s.charAt(i);
if(a=='A'||a=='E'||a=='I'||a=='O'||a=='U'||a==' ')
str = str + a;
else
str = str + "_";
}
z = str;
}
boolean word_n_chance()
{
String x = ""; boolean f = false;
for(i=0; i<s.length(); i++)
{
a = s.charAt(i);
if(a==b)
{
f = true; x = x + b;
}
else if(a=='A'||a=='E'||a=='I'||a=='O'||a=='U'||a==' '||a==z.charAt(i))
{
x = x + a;
}
else
x = x + "_";
}
if(b=='A'||b=='E'||b=='I'||b=='O'||b=='U'||b==' ')
f = true;
if(!f)
if(!Wrong())
wrong[w++] = b;
else
f = true;
str = x;
z = str;
return(f);
}
void Char()
{
System.out.println("\n\nEnter your guess: "); String y;
do
{
y = sc.next();
if(y.length()>=1)
b = y.charAt(0);
}
while(!(y.length()>=1));
b = Character.toUpperCase(b);
}
boolean Wrong()
{
boolean f = false;
for(i=0; i<8; i++)
if(b==wrong[i])
{ f = true; break; }
return f;
}
void hangman()
{
str = ""; z = ""; w = 0;
Rules_H();
wrong = new char [8];
System.out.println("\nPress 'P' & 'Enter' to begin game.");
String x = sc.next();
input(); FW();
boolean chance = false;
int ch = 8;
while(ch>0 && !(s.equalsIgnoreCase(str)))
{
System.out.println("\fHANGMAN");
hanger(ch);
System.out.print("\nWord: ");
for(i=0; i<str.length(); i++)
System.out.print(str.charAt(i) + " ");
System.out.println("\n\nChances left: " + ch);
System.out.print("\nWrong letters entered:");
for(i=0; i<7; i++)
System.out.print(" " + wrong[i]);
Char();
chance = word_n_chance();
if(!chance)
ch--;
}
System.out.println("\fHANGMAN");
hanger(ch);
System.out.println("\n\nChances left: " + ch);
if(ch>0)
System.out.println("Congratulations!!\nYou got the word right.\nWell played.");
else
System.out.println("Oops! You're dead.\nBetter luck next time :)");
System.out.println("The word was " + s);
}
// SNAKES AND LADDERS
void Rules_S()
{
display("SNAKES AND LADDERS");
for(i=0; i<20; i++)
for(l=0; l<10000000; l++);
System.out.println("\n Rules:\n- 1 to 4 player game. *1 player game means Player vs. Computer.\n- Each player must enter a name. * Names will not be saved after the game is over.\n- A player starts moving on the board only after rolling a 6.\n- Every time a player rolls a 6, he/she must roll again.\n- If a player encounters a snake bite, he will go back to its tail.\n- If a player encounters a ladder, he will go up the ladder.\n- The first player to reach box 100 wins.");
for(i=0; i<50; i++)
for(l=0; l<10000000; l++);
}
int dice()
{
int n = (int) Math.round(Math.random() * 5 + 1);
n = (int) Math.round(Math.random() * 5 + 1);
return((int) Math.round(Math.random() * 5 + 1));
}
void chance(int n, char p)
{
int x = box + n; String y;
if(p=='p')
y = "You ";
else
y = "Computer ";
if(n!=6)
{
switch(x)
{
case 4: System.out.println(y + "got a ladder at " + x + "!! "); x = 14; break;
case 9: System.out.println(y + "got a ladder at " + x + "!! "); x = 31; break;
case 17: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 7; break;
case 20: System.out.println(y + "got a ladder at " + x + "!! "); x = 38; break;
case 28: System.out.println(y + "got a ladder at " + x + "!! "); x = 84; break;
case 40: System.out.println(y + "got a ladder at " + x + "!! "); x = 59; break;
case 62: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 34; break;
case 71: System.out.println(y + "got a ladder at " + x + "!! "); x = 91; break;
case 87: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 24; break;
case 93: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 63; break;
case 94: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 70; break;
case 98: System.out.println(y + "got bit by a snake at " + x + "!! "); x = 81; break;
}
}
box = x;
}
void table()
{
System.out.println("\n\t LADDERS(#)\t\t\t\t\t SNAKES($)");
System.out.println("\t From:\t\tTo:\t\t\t\t From:\t\tTo:");
System.out.println("\t 4\t\t14\t\t\t\t 17\t\t 7");
System.out.println("\t 9\t\t31\t\t\t\t 62\t\t34");
System.out.println("\t 20\t\t38\t\t\t\t 87\t\t24");
System.out.println("\t 28\t\t84\t\t\t\t 93\t\t63");
System.out.println("\t 40\t\t59\t\t\t\t 94\t\t70");
System.out.println("\t 71\t\t91\t\t\t\t 98\t\t81");
}
char SorL(int x)
{
char SL = '\u0000';
switch(x)
{
case 4: case 9: case 20: case 28: case 40: case 71: SL = '#'; break;
case 17: case 62: case 87: case 93: case 94: case 98: SL = '$'; break;
}
return(SL);
}
void board(int p[], char bead[])
{
int j; int board[] = {91,92,93,94,95,96,97,98,99,100};
for(int k=10; k>0; k--)
{
System.out.print("\n\t");
if(k%2==1)
for(i=0; i<10; i++)
{
System.out.print(board[i]);
System.out.print(SorL(board[i]));
System.out.print(" ");
for(j=0; j<p.length; j++)
if(p[j]==board[i])
System.out.print(bead[j]);
System.out.print("\t");
}
else
for(i=9; i>=0; i--)
{
System.out.print(board[i]);
System.out.print(SorL(board[i]));
System.out.print(" ");
for(j=0; j<p.length; j++)
if(p[j]==board[i])
System.out.print(bead[j]);
System.out.print("\t");
}
for(i=0; i<10; i++)
board[i] -= 10;
}
}
void PvC()//Player vs Computer
{
int n = 0, b = 0, sixp = 0, sixc = 0;
int p[] = new int [2]; char bead[] = new char [2];
boolean ps = false, cs = false; bead[1] = (char)162;
System.out.print("\fEnter your name, Player : ");
String name, y;
do
{
name = sc.nextLine().trim().toUpperCase();
}while(name.length()<1);
do
{
System.out.print("\nChoose your bead: " );
System.out.println("1. " + (char)164 + " 2. " + (char)167 + " 3. " + (char)42 + " 4. " + (char)230);
b = sc.nextInt();
switch(b)
{
case 1: bead[0] = (char)164; break;
case 2: bead[0] = (char)167; break;
case 3: bead[0] = (char)169; break;
case 4: bead[0] = (char)230; break;
default: System.out.println("Invalid input.");
}
}while(b<1 || b>4);
y = sc.nextLine();
System.out.println("\nLoading....");
for(i=0; i<15; i++)
for(l=0; l<10000000; l++);
while(box<100)
{
//Player's chance
do
{
System.out.println("\f\t\t\t\tSNAKES AND LADDERS");
System.out.println("\t\tBeads: " + name + "- " + bead[0] + " Computer- " + bead[1]);
System.out.println("\t\tBoxes: " + name + ": " + p[0] + " Computer: " + p[1]);
table();
board(p,bead);
System.out.print("\n\n" + name + "'s chance:");
System.out.print("\nTo restart the game, press 'R' and 'Enter'.\nTo quit the game, press 'Q' and 'Enter'.\nPress 'D' and 'Enter' to play dice-- ");
do
{
y = sc.next();
if(y.length()>=1)
play = y.charAt(0);
}while(y.length()<1);
if(play=='q' || play=='Q' || play=='r' || play=='R')
{
box = 101;
break;
}
n = dice();
if(sixp==3)
{
n = 6;
sixp = 0;
}
if(p[0]>=90 && n==6)
n = (int) Math.round(Math.random() * 4 + 1);
for(i=0; i<n; i++)
{
for(int j=0; j<20; j++)
for(l=0; l<10000000; l++);
System.out.print(". ");
}
if(ps==true)
{
if(p[0]+n>100)
{
System.out.println("You have rolled a number greater than what you need!");
break;
}
if(n==6)
System.out.println("Congratulations! You rolled a 6! Play again.");
else
System.out.println("You rolled a " + n);
box = p[0];
chance(n,'p');
p[0] = box;
}
else
{
if(n==6)
{
ps = true;
System.out.println("Congratulations! You rolled a 6! You have started moving.");
}
else
{
System.out.println("You rolled a " + n + ". Not a 6! Not a start.");
sixp++;
}
}
for(i=0; i<100; i++)
for(l=0; l<10000000; l++);
}while(n==6);
if(p[0]==100)
{
System.out.println("Congratulations " + name + "!! You have won the Game :):):)");
break;
}
if(play=='q' || play=='Q' || play=='r' || play=='R')
break;
//Computer's chance
do
{
System.out.println("\f\t\t\t\tSNAKES AND LADDERS");
System.out.println("\t\tBeads: " + name + "- " + bead[0] + " Computer- " + bead[1]);
System.out.println("\t\tBoxes: " + name + ": " + p[0] + " Computer: " + p[1]);
table();
board(p,bead);
System.out.println("\n\nComputer's chance: ");
for(i=0; i<30; i++)
for(l=0; l<10000000; l++);
n = dice();
if(sixc==4)
{
n = 6;
sixc = 0;
}
if(p[1]>=90 && n==6)
n = (int) Math.round(Math.random() * 4 + 1);
for(i=0; i<n; i++)
{
for(int j=0; j<20; j++)
for(l=0; l<10000000; l++);
System.out.print(". ");
}
if(cs==true)
{
if(p[1]+n>100)
{
System.out.println("Computer has rolled a number greater than what it needs!");
break;
}
if(n==6)
System.out.println("Computer rolled a 6! Computer plays again.");
else
System.out.println("Computer rolled a " + n);
box = p[1];
chance(n,'c');
p[1] = box;
}
else
{
if(n==6)
{
cs = true;
System.out.println("Computer rolled a 6! Computer has started moving.");
}
else
{
System.out.println("Computer rolled a " + n + ". Not a 6! Not a start.");
sixc++;
}
}
for(i=0; i<100; i++)
for(l=0; l<10000000; l++);
}while(n==6);
if(p[1]==100)
System.out.println("Oops! Computer won the game. :(:(:(\nBetter luck next time.");
}
}
void PvP(int pl)//Player vs Player
{
int p[] = new int [pl]; int k = 3;
String name[] = new String [pl];
boolean start[] = new boolean [pl];
char beads[] = {(char)164, (char)167, (char)42, (char)230};
char bead[] = new char[pl];
int six[] = new int [pl];
String y;
box = 0;
for(i=0; i<pl; i++)
{
System.out.print("\fEnter your name, Player " + (i + 1) + ": ");
do
{
name[i] = sc.nextLine().trim().toUpperCase();
}while(name[i].length()<1);
if(i<3)
{
System.out.print("\nChoose your bead: ");
for(l=0; l<4; l++)
if(beads[(int)l]!='\u0000')
System.out.print(" " + (l+1) + ". " + beads[(int)l]);
System.out.println();
int b = sc.nextInt() - 1;
bead[i] = beads[b];
for(l=0; l<3; l++)
if(l >= b)
beads[(int)l] = beads[(int)l+1];
beads[k--] = '\u0000';
y = sc.nextLine();
}
}
if(pl==4)
bead[pl-1] = beads[0];
System.out.println("\nLoading...");
//Game begins.
for(i=0; i<15; i++)
for(l=0; l<10000000; l++);
int n = 0, t = 0;
while(box<100)
{
for(t=0; t<pl; t++)
{
System.out.println("\f\t\t\tSNAKES AND LADDERS");
System.out.print("\t\tBeads: ");
for(i=0; i<pl; i++)
System.out.print(name[i] + "- " + bead[i] + " ");
System.out.print("\n\t\tBoxes: ");
for(i=0; i<pl; i++)
System.out.print(name[i] + "- " + p[i] + " ");
table();
board(p,bead);
System.out.print("\n\n" + name[t] + "'s chance :");
System.out.print("\nTo restart the game, press 'R' and 'Enter'.\nTo quit the game, press 'Q' and 'Enter'.\nPress 'D' and 'Enter' to play dice-- ");
do
{
y = sc.nextLine();
if(y.length()>=1)
play = y.charAt(0);
}while(y.length()>1);
if(play=='q' || play=='Q' || play=='r' || play=='R')
{
box = 101;
break;
}
n = dice();
if(six[t]==3)
{
n = 6;
six[t] = 0;
}
if(p[t]>=90 && n==6)
n = (int) Math.round(Math.random() * 4 + 1);
for(i=0; i<n; i++)
{
for(int j=0; j<100; j++)
for(l=0; l<10000000; l++);
System.out.print(". ");
}
if(start[t]==true)
{
if(p[t]+n>100)
{
System.out.println("You have rolled a number greater than what you need!");
continue;
}
if(n==6)
System.out.println("Congratulations! You rolled a 6! Play again.");
else
System.out.println("You rolled a " + n);
box = p[t];
chance(n,'p');
p[t] = box;
}
else
{
if(n==6)
{
start[t] = true;
System.out.println("Congratulations! You rolled a 6! You have started moving.");
}
else
{
System.out.println("You rolled a " + n + ". Not a 6! Not a start. Better luck for your next chance!");
six[t] += 1;
}
}
if(box==100)
break;
if(n==6)
t--;
for(i=0; i<500; i++)
for(l=0; l<10000000; l++);
}
}
//Game ends.
if(box==100)
System.out.println("Congratulations " + name[t] + "!! You have won the game. :):):):)");
}
void S_n_L()
{
Scanner sc = new Scanner (System.in);
Rules_S(); int pl = 1; box = 0;
do
{
if(pl<1 || pl>4)
System.out.print("\fInvalid input.");
System.out.println("\nPlease enter the number of players from 1 to 4:");
pl = sc.nextInt();
}while(pl<1 || pl>4);
if(pl==1) //Player vs Computer
PvC();
else //Multi-player
PvP(pl);
}
static void play()
{
Scanner in = new Scanner (System.in);
Games ob = new Games();
ob.display("DIGI-GAMING");
for(i=0; i<25; i++)
for(l=0; l<10000000; l++);
int a = 0, x = 0;
do
{
System.out.println("\f\t\tDIGI-GAMING\n\nHOME\n\nWhat do you want to do?\n(Press the serial number and Enter.)\n1. Play Snakes and Ladders\n2. Play Hangman\n3. Quit Menu");
x = in.nextInt();
do
{
a = 0;
switch(x)
{
case 1:
while(play=='r' || play=='R')
ob.S_n_L();
break;
case 2: ob.hangman(); break;
case 3: System.out.println("\nThank you for playing!! BYE!! :):):)"); break;
default: System.out.println("\fInvalid input. Please choose again.");
}
if(x==1 || x==2)
{
String y = in.nextLine();
System.out.println("\nThank you for playing!! :):):)");
while(a!=1 && a!=2)
{
System.out.println("Please make a choice");
System.out.println("1. Play again.\n2. Go back to HOME page.");
a = in.nextInt();
if(a!=1 && a!=2)
{
System.out.println("\fInvalid input.");
for(i=0; i<20; i++)
for(l=0; l<10000000; l++);
break;
}
}
}
if((a!=1 && a!=2)|| x==3)
break;
}while(!(x>0 && x<3) || a==1);
if(x==3)
break;
}while(a==2);
}
}