-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayG.java
More file actions
406 lines (359 loc) · 10.8 KB
/
PlayG.java
File metadata and controls
406 lines (359 loc) · 10.8 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
import java.util.*;
class PlayG extends guess{
public static void main(String[] args){
Scanner Sc = new Scanner(System.in);
int choice;
do{
System.out.println("\t\t---------- GAME OF GAMES -----------");
System.out.println("\t\t ~~~~~~~~~~~~~~~ ");
System.out.println("1. GUESS THE NUMBER");
System.out.println("2. STONE PAPER SCISSOR");
System.out.println("3. TIC TAC TOE");
System.out.println("4. HANG-MAN");
System.out.println("5. EXIT");
System.out.println("\nENTER CHOICE: ");
choice = Sc.nextInt();
switch(choice){
case 1:
guess Player1 = new guess();
Player1.now();
break;
case 2:
sps Player2 = new sps();
Player2.main1();
break;
case 3:
TicTacToe Player3 = new TicTacToe();
Player3.main2();
break;
case 4:
Hangman2 Player4 = new Hangman2();
Player4.main3();
break;
case 5:
System.exit(0);
default:
System.out.println("Wrong Choice, Try Again");
}
}while(choice != 5);
}
}
// STONE PAPER SCISSOR GAME START
class sps extends sps1{
void main1(){
System.out.println("\t\t\t----- STONE PAPER SCISSOR GAME -----");
sps a1 = new sps();
for(int i= 1;i<=2 ;i++){
System.out.println("\n\t\t SET "+i);
System.out.println("\t\t------");
a1.user();
a1.computer();
a1.result();
a1.score();
}
a1.fresult();
}
}
class sps1{
String comp,user;
int comp_score, user_score;
public void computer(){
String[] s = {"Stone","Paper","Scissor"};
int rand1 = (int)(Math.random()*3);
comp = s[rand1];
System.out.println("COMPUTERS CHOICE IS --> "+comp);
}
public void user(){
System.out.print("\n1]Stone");
System.out.print("\t\t2]Paper");
System.out.print("\t\t3]Scissor");
System.out.print("\n\nEnter Your Choice --> ");
Scanner sc = new Scanner(System.in);
int u = sc.nextInt();
switch(u){
case 1: user = "Stone";
break;
case 2: user = "Paper";
break;
case 3: user = "Scissor";
break;
}
System.out.println("\nYOUR CHOICE IS --> "+user);
}
public void result(){
if(user == "Stone" && comp == "Paper"){
comp_score++;
System.out.println("\nCOMPUTER WON !!!");
}
else if(user == "Stone" && comp == "Scissor"){
user_score++;
System.out.println("\nYOU WON !!!");
}
else if(user == "Paper" && comp == "Stone"){
user_score++;
System.out.println("\nYOU WON !!!");
}
else if(user == "Paper" && comp == "Scissor"){
comp_score++;
System.out.println("\nCOMPUTER WON !!!");
}
else if(user == "Scissor" && comp == "Stone"){
comp_score++;
System.out.println("\nCOMPUTER WON !!!");
}
else if(user == "Scissor" && comp == "Paper"){
user_score++;
System.out.println("\nYOU WON !!!");
}
else{
System.out.println("\nITS A DRAW !!!");
}
}
public void score(){
System.out.println("\n\nYour score --> "+user_score+" computer score --> "+comp_score);
}
public void fresult(){
if(user_score > comp_score)
System.out.println("\n\nYOU WON THE GAME");
else if(comp_score > user_score)
System.out.println("\n\nCOMPUTER WON THE GAME");
else
System.out.println("\n\nGAME IS A DRAW");
}
}
// TIC TAC TOE GAME START
class TicTacToe {
static Scanner in;
static String[] board;
static String turn;
void main2(){
in = new Scanner(System.in) ;
board = new String[9];
turn = "X";
String winner = null;
populateEmptyBoard();
System.out.println("\n\n\t\t---------- Tic Tac Toe ----------");
System.out.println("\n\t\tWELCOME TO 2 PLAYER TIC TAC TOE");
printBoard();
System.out.print("X will play first. Enter a slot number to place X in: ");
while (winner == null) {
int numInput;
try {
numInput = in.nextInt();
if (!(numInput > 0 && numInput <= 9)) {
System.out.println("Invalid input a; re-enter slot number:");
continue;
}
} catch (InputMismatchException e) {
System.out.println("Invalid input; re-enter slot number:");
continue;
}
if (board[numInput-1].equals(String.valueOf(numInput))) {
board[numInput-1] = turn;
if (turn.equals("X")) {
turn = "O";
} else {
turn = "X";
}
printBoard();
winner = checkWinner();
} else {
System.out.println("Slot already taken; re-enter slot number:");
continue;
}
}
if (winner.equalsIgnoreCase("draw")) {
System.out.println("It's a draw! Thanks for playing.");
} else {
System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");
}
}
static String checkWinner() {
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = board[0] + board[1] + board[2];
break;
case 1:
line = board[3] + board[4] + board[5];
break;
case 2:
line = board[6] + board[7] + board[8];
break;
case 3:
line = board[0] + board[3] + board[6];
break;
case 4:
line = board[1] + board[4] + board[7];
break;
case 5:
line = board[2] + board[5] + board[8];
break;
case 6:
line = board[0] + board[4] + board[8];
break;
case 7:
line = board[2] + board[4] + board[6];
break;
}
if (line.equals("XXX")) {
return "X";
} else if (line.equals("OOO")) {
return "O";
}
}
for (int a = 0; a < 9; a++) {
if (Arrays.asList(board).contains(String.valueOf(a+1))) {
break;
}
else if (a == 8) return "draw";
}
System.out.println(turn + "'s turn; enter a slot number to place " + turn + " in:");
return null;
}
static void printBoard() {
System.out.println("\n");
System.out.println("\t\t| " + board[0] + " | " + board[1] + " | " + board[2] + " |");
System.out.println("\t\t|-----------|");
System.out.println("\t\t| " + board[3] + " | " + board[4] + " | " + board[5] + " |");
System.out.println("\t\t|-----------|");
System.out.println("\t\t| " + board[6] + " | " + board[7] + " | " + board[8] + " |");
System.out.println("\n");
}
static void populateEmptyBoard() {
for (int a = 0; a < 9; a++) {
board[a] = String.valueOf(a+1);
}
}
}
//HANG-MAN CODE START
class Hangman2 {
private static String[] words = {"terminator", "banana", "computer", "cow", "rain", "water","program","java", "project","india" };
private static String word = words[(int) (Math.random() * 10)];
private static String asterisk = new String(new char[word.length()]).replace("\0", "*");
private static int count = 0;
void main3() {
System.out.println("\n\n\t\t---------- HANG MAN ----------");
System.out.println("\n\n\tGUESS THE WORD IN ASTERISK !");
Scanner sc = new Scanner(System.in);
Hangman2 obj= new Hangman2();
while (count < 7 && asterisk.contains("*")) {
System.out.println("\nWORD: "+ asterisk);
System.out.print("GUESS ANY LETTER IN THE ABOVE WORD : ");
String guess = sc.next();
obj.hang(guess);
}
}
void hang(String guess) {
String newasterisk = "";
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess.charAt(0)) {
newasterisk += guess.charAt(0);
} else if (asterisk.charAt(i) != '*') {
newasterisk += word.charAt(i);
} else {
newasterisk += "*";
}
}
if (asterisk.equals(newasterisk)) {
count++;
hangmanImage();
} else {
System.out.println("\nCORRECT GUESS !!\n");
asterisk = newasterisk;
}
if (asterisk.equals(word)) {
System.out.println("CORRECT! YOU WIN! THE WORD WAS: " + word);
}
}
void hangmanImage() {
if (count == 1) {
System.out.println("\nWrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("\t\t___|___");
System.out.println("\n");
}
if (count == 2) {
System.out.println("Wrong guess, try again");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t___|___");
System.out.println("\n");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
System.out.println("\t\t ____________");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t | ");
System.out.println("\t\t___|___");
System.out.println("\n");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
System.out.println("\t\t ____________");
System.out.println("\t\t | _|_");
System.out.println("\t\t | / \\");
System.out.println("\t\t | | |");
System.out.println("\t\t | \\_ _/");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t |");
System.out.println("\t\t___|___");
System.out.println("\n");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
System.out.println("\t\t ____________");
System.out.println("\t\t | _|_");
System.out.println("\t\t | / \\");
System.out.println("\t\t | | |");
System.out.println("\t\t | \\_ _/");
System.out.println("\t\t | |");
System.out.println("\t\t | |");
System.out.println("\t\t |");
System.out.println("\t\t___|___");
System.out.println("\n");
}
if (count == 6) {
System.out.println("Wrong guess, try again\n");
System.out.println("\t\t ____________");
System.out.println("\t\t | _|_");
System.out.println("\t\t | / \\");
System.out.println("\t\t | | |");
System.out.println("\t\t | \\_ _/");
System.out.println("\t\t | |");
System.out.println("\t\t | |");
System.out.println("\t\t | / \\ ");
System.out.println("\t\t___|___ / \\");
System.out.println("\n");
}
if (count == 7) {
System.out.println("\n\t\t-----GAME OVER-------");
System.out.println("\t\t ____________");
System.out.println("\t\t | _|_");
System.out.println("\t\t | / \\");
System.out.println("\t\t | | |");
System.out.println("\t\t | \\_ _/");
System.out.println("\t\t | _|_");
System.out.println("\t\t | / | \\");
System.out.println("\t\t | / \\ ");
System.out.println("\t\t___|___ / \\");
System.out.println("\n\t\tGAME OVER! The word was: " + word);
}
System.out.println("\n\n");
}
}