-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROJECT.c
716 lines (640 loc) · 17.6 KB
/
PROJECT.c
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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 3
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define CYAN "\033[36m"
#define RESET "\033[0m"
// Rock paper scissor
int generateComputerChoice();
int determineWinner(int userChoice, int computerChoice);
void printChoices();
void rock_paper();
// Guess the number game
void guessing_the_number();
int RandomNumber();
int isCorrect(int numberToGuess, int userGuess);
// Tic tac toe
void printBoard(char board[SIZE][SIZE]);
int isBoardFull(char board[SIZE][SIZE]);
int winner(char board[SIZE][SIZE], char ch);
int isValid(char board[SIZE][SIZE], int row, int col);
int evaluate(char board[SIZE][SIZE]);
int minimax(char board[SIZE][SIZE], int depth, bool isMax);
void best_move(char board[SIZE][SIZE]);
void save(char board[SIZE][SIZE]);
void reload(char board[SIZE][SIZE]);
void easy_move(char board[SIZE][SIZE]);
void computer(char board[SIZE][SIZE], int mode);
void multiplayer(char board[SIZE][SIZE]);
void file_func();
void tictac_name();
int main()
{
char any[2];
int choice;
printf("Enter any key to continue: ");
scanf("%s", any);
system("cls");
do
{
printf(CYAN "^^^^^^^^^" YELLOW " WELCOME" RESET " TO " RESET RED " T " GREEN "R " BLUE "I " YELLOW "P " GREEN "L " RED "E " RESET YELLOW " CHAL" GREEN "LENGE" CYAN " SHOW" RED "DOWN " CYAN "^^^^^^^^^\n" RESET);
printf("\nChoose your interest: "
"\n\t1. Tic Tac Toe (Multiplayer/Solo) "
"\n\t2. Guessing the number "
"\n\t3. Rock, paper, scissors, lizard, spock"
"\n\t4. Exit game\n");
scanf("%d", &choice);
system("cls");
if (choice == 1)
{
int cont, level, mode;
srand(time(0));
tictac_name();
do
{
char board[SIZE][SIZE] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};
file_func();
printf("1.Want to play with friend\n"
"2.Want to play with computer in easy mode\n"
"3.Want to play with computer in hard mode\n"
"4.EXIT\n");
scanf("%d", &mode);
system("cls");
if (mode == 1)
{
multiplayer(board);
}
else if (mode == 2)
{
computer(board, mode); // easy mode
}
else if (mode == 3)
{
computer(board, mode); // hard mode
}
else
break;
printf("Do you want to play again\n"
"1. To continue\n"
"0. To go back\n");
scanf("%d", &cont);
system("cls");
} while (cont);
}
else if (choice == 2)
{
guessing_the_number();
}
else if (choice == 3)
{
rock_paper();
}
else if (choice == 4)
{
break;
}
else
{
printf("\nMake a valid choice");
}
} while (1);
printf("\n\n\t\tTHANKS FOR PLAYING! SEE YOU AGAIN");
Sleep(3000);
return 0;
}
// to print the board of tic tac toe
void printBoard(char board[SIZE][SIZE])
{
printf(YELLOW "******\n" RESET);
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] == 'X')
{
printf(YELLOW "|" RESET RED " %c " RESET YELLOW "|" RESET, board[i][j]);
}
else if (board[i][j] == 'O')
{
printf(YELLOW "|" RESET BLUE " %c " RESET YELLOW "|" RESET, board[i][j]);
}
else
printf(YELLOW "|" RESET " %c " YELLOW "|" RESET, board[i][j]);
}
printf("\n");
if (i < SIZE - 1)
printf(YELLOW "---------------\n" RESET);
}
printf(YELLOW "******\n" RESET);
}
// check whether the board have available inputs or not
int isBoardFull(char board[SIZE][SIZE])
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (board[i][j] >= '1' && board[i][j] <= '9')
{
return 0; // board is not full
}
}
}
return 1; // board is full
}
// to check whether player wins or not
int winner(char board[SIZE][SIZE], char ch)
{
// checking for rows and columns
for (int i = 0; i < SIZE; i++)
{
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] == ch) ||
(board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] == ch))
{
return 1; // Player wins
}
}
// checking for horizontals
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] == ch) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] == ch))
{
return 1; // Player wins
}
return 0;
}
// to check whether the move is valid or it is already input before
int isValid(char board[SIZE][SIZE], int row, int col)
{
if (board[row][col] >= '1' && board[row][col] <= '9')
{
return 1; // yes position valid
}
return 0; // position not valid
}
// evaluation funciton used for minimax algorithm
int evaluate(char board[SIZE][SIZE])
{
if (winner(board, 'X'))
{
return 1; // computer wins
}
else if (winner(board, 'O'))
{
return -1; // player wins
}
else if (isBoardFull(board))
{
return 0; // its a draw
}
else
{
return 2; // game is going on
}
}
// minimax algorithm which is recursive function, calculates the best input by checking the whole game (ARTIFICIAL INTELLIGENCE)
int minimax(char board[SIZE][SIZE], int depth, bool isMax)
{
int score = evaluate(board);
if (score != 2)
{
return score;
}
char ch;
if (isMax)
{
int max = -1000;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (isValid(board, i, j))
{
ch = board[i][j];
board[i][j] = 'X';
int current = minimax(board, depth + 1, !isMax);
max = (current > max) ? current : max;
board[i][j] = ch;
}
}
}
return max;
}
else
{
int min = 1000;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (isValid(board, i, j))
{
ch = board[i][j];
board[i][j] = 'O';
int current = minimax(board, depth + 1, !isMax);
min = (current < min) ? current : min;
board[i][j] = ch;
}
}
}
return min;
}
}
// the turn of the best_move which calls minimax algo to find its input
void best_move(char board[SIZE][SIZE])
{
int best = -1000, row = -1, col = -1;
char ch;
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (isValid(board, i, j))
{
ch = board[i][j];
board[i][j] = 'X';
int move = minimax(board, 0, 0);
board[i][j] = ch;
if (move > best)
{
row = i;
col = j;
best = move;
}
}
}
}
printf("computer" RED " (X) " RESET "move: %d\n", (row * SIZE) + (col + 1));
board[row][col] = 'X';
}
void easy_move(char board[SIZE][SIZE])
{
int move, row, col;
do
{
move = (rand() % 9) + 1;
row = (move - 1) / 3;
col = (move - 1) % 3;
} while (isValid(board, row, col) == 0);
board[row][col] = 'X';
printf("Computer" RED " (x) " RESET "move %d\n", move);
}
void save(char board[SIZE][SIZE])
{
FILE *fsave = fopen("Store.txt", "w+");
if (fsave == NULL)
{
printf(RED "Error opening file\n" RESET);
return;
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
fprintf(fsave, "%c ", board[i][j]);
}
}
printf(GREEN "Game saved successfully!\n" RESET);
Sleep(1000);
fclose(fsave);
return;
}
void load(char board[SIZE][SIZE])
{
FILE *fresume = fopen("Store.txt", "r+");
if (fresume == NULL)
{
printf(RED "Error opening file\n" RESET);
return;
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
fscanf(fresume, "%c ", &board[i][j]);
}
}
printf(GREEN "Previous game loaded successfully!\n" RESET);
Sleep(1000);
printBoard(board);
return;
}
void reset(char board[SIZE][SIZE])
{
save(board); // Save the current state before resetting
for (int i = 0, k = 1; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++, k++)
{
board[i][j] = k + '0'; // Convert the integer to character
}
}
printf(GREEN "Game reset successfully!\n" RESET);
Sleep(1000);
printBoard(board);
return;
}
void tictac_name()
{
printf(BLUE "^^^^^^^^^^" RESET " WELCOME TO " RED "TIC " BLUE "TAC " GREEN "BOARD" RESET " BATTLE " BLUE "^^^^^^^^^^^\n\n" RESET);
}
// when the player is playing with computer
void computer(char board[SIZE][SIZE], int mode)
{
tictac_name();
printBoard(board);
while (1)
{
char move[10];
int row, col;
printf("Enter your" BLUE " (O) " RESET "move: ");
scanf("%s", move);
if (strcmp(move, "s") == 0)
{
save(board);
continue;
}
else if (strcmp(move, "l") == 0)
{
load(board);
continue;
}
else if (strcmp(move, "e") == 0)
{
return;
}
else if (strcmp(move, "r") == 0)
{
reset(board);
continue;
}
int moveInt = atoi(move);
row = (moveInt - 1) / 3;
col = (moveInt - 1) % 3;
if (isValid(board, row, col))
{
if (row < 0 || col < 0 || row >= SIZE || col >= SIZE)
{
printf(YELLOW "Invalid move. Try again. \n" RESET);
continue;
}
board[row][col] = 'O';
if (winner(board, 'O'))
{
system("cls");
tictac_name();
printBoard(board);
printf(GREEN "Congratulations you won!\n " RESET);
break;
}
if (isBoardFull(board))
{
printf(BLUE "Game is draw\n" RESET);
break;
}
printBoard(board);
printf("Computer loading...");
Sleep(1000);
system("cls");
tictac_name();
if (mode == 2)
easy_move(board);
if (mode == 3)
best_move(board);
printBoard(board);
if (winner(board, 'X'))
{
printf(RED "Computer wins\n " RESET);
break;
}
if (isBoardFull(board))
{
printf(BLUE "Game is draw\n" RESET);
break;
}
}
else
{
printf(YELLOW "Invalid input.\n" RESET);
}
}
return;
}
void multiplayer(char board[SIZE][SIZE])
{
tictac_name();
char player = 'X';
char move[10];
int row, col;
while (1)
{
system("cls");
tictac_name();
printBoard(board);
if (player == 'X')
{
printf("player" RED " %c " RESET "enter move ", player);
}
else
{
printf("player" BLUE " %c " RESET "enter move ", player);
}
scanf("%s", move);
if (strcmp(move, "s") == 0)
{
save(board);
continue;
}
else if (strcmp(move, "l") == 0)
{
load(board);
continue;
}
else if (strcmp(move, "e") == 0)
{
return;
}
else if (strcmp(move, "r") == 0)
{
reset(board);
continue;
}
row = (atoi(move) - 1) / 3;
col = (atoi(move) - 1) % 3;
if (isValid(board, row, col))
{
board[row][col] = player;
}
else
{
printf(YELLOW "Invalid input, Enter correct move\n" RESET);
Sleep(1000);
player = (player == 'X') ? 'O' : 'X';
}
if (winner(board, player))
{
printf(GREEN "Congratulations player %c won the game!\n" RESET, player);
return;
}
if (isBoardFull(board))
{
printf(BLUE "Game is Draw\033" RESET);
return;
}
player = (player == 'X') ? 'O' : 'X';
system("cls");
}
}
int RandomNumber()
{
return rand() % 100 + 1;
}
// Function to check if the user's guess is correct
int isCorrect(int numberToGuess, int userGuess)
{
if (userGuess == numberToGuess)
{
return 1; // Guess is correct
}
else if (userGuess > numberToGuess)
{
printf("Too high! Try again.\n");
}
else
{
printf("Too low! Try again.\n");
}
return 0; // Guess is incorrect
}
void guessing_the_number()
{
char exitChar;
int numberToGuess, userGuess, attempts = 0;
// Set seed for random number generation
srand(time(NULL));
do
{
// Generate a random number between 1 and 100
numberToGuess = RandomNumber();
printf(BLUE "^^^^^^^^^^^^^ Guess the Number ^^^^^^^^^^^^\n\n" RESET);
printf("Try to guess the number between 1 and 100.\n");
do
{
printf("Enter your guess (or enter 'e' to go back to the main menu): ");
if (scanf("%d", &userGuess) != 1)
{
// Invalid input, clear the input buffer
scanf("%s");
exitChar = getchar();
if (exitChar == 'e' || exitChar == 'E')
{
return; // Exit the guessing game and return to the main menu
}
else
{
printf("Invalid input. Please enter a number or 'e' to go back to the main menu.\n");
continue;
}
}
attempts++;
} while (!isCorrect(numberToGuess, userGuess));
printf(GREEN "Congratulations! You guessed the number in %d attempts.\n" RESET, attempts);
Sleep(1000);
system("cls");
} while (1);
}
int generateComputerChoice()
{
return rand() % 5 + 1; // 1: Rock, 2: Paper, 3: Scissors, 4: Lizard, 5: Spock
}
// Function to determine the winner based on choices on rock paper
int determineWinner(int userChoice, int computerChoice)
{
if ((userChoice == 1 && (computerChoice == 3 || computerChoice == 4)) ||
(userChoice == 2 && (computerChoice == 1 || computerChoice == 5)) ||
(userChoice == 3 && (computerChoice == 2 || computerChoice == 4)) ||
(userChoice == 4 && (computerChoice == 2 || computerChoice == 5)) ||
(userChoice == 5 && (computerChoice == 1 || computerChoice == 3)))
{
return 1; // User wins
}
else if (userChoice == computerChoice)
{
return 0; // It's a tie
}
else
{
return -1; // Computer wins
}
}
// Function to print choices
void printChoices()
{
printf("Choices:\n"
"\t1. Rock\n"
"\t2. Paper\n"
"\t3. Scissors\n"
"\t4. Lizard\n"
"\t5. Spock\n");
}
void rock_paper()
{
int userChoice, computerChoice, result;
char playAgain;
// Set seed for random number generation
srand(time(NULL));
printf("^^^^^^^^^^ Welcome to Rock, Paper, Scissors, Lizard, Spock! ^^^^^^\n");
do
{
// Get user choice
printChoices();
printf("Enter your choice (1-5): ");
scanf("%d", &userChoice);
if (userChoice < 1 || userChoice > 5)
{
printf(YELLOW "Invalid choice. Please enter a number between 1 and 5.\n" RESET);
continue;
}
// Generate computer choice
computerChoice = generateComputerChoice();
// Display choices
printf("You chose: %d\n", userChoice);
printf("Computer chose: %d\n", computerChoice);
// Determine the winner
result = determineWinner(userChoice, computerChoice);
if (result == 1)
{
printf(GREEN "Congratulations! You win!\n" RESET);
}
else if (result == -1)
{
printf(RED "Sorry, you lose. Better luck next time!\n" RESET);
}
else
{
printf(BLUE "It's a tie. Try again!\033[0m\n" RESET);
}
// Ask if the user wants to play again
printf("\nDo you want to play again? (y/n): ");
scanf(" %c", &playAgain);
system("cls");
} while (playAgain == 'y' || playAgain == 'Y');
return;
}
void file_func()
{
printf(
GREEN "\t\t\t s for save\n" RESET
BLUE "\t\t\t r for reset\n" RESET
YELLOW "\t\t\t l for load\n" RESET
RED "\t\t\t e for exit\n\n" RESET);
}