forked from Aradhya2708/ICS_Major_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minigames.c
1883 lines (1710 loc) · 64.5 KB
/
minigames.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
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ICS_Project.h"
// Minigames
// Hangtheman (1)................................................................................................
void delay(float number_of_seconds)
{
// Converting time into milliseconds
int milli_seconds = 1000 * number_of_seconds;
// Storing start time---
clock_t start_time = clock();
// Looping till required time is not achieved
while (clock() < start_time + milli_seconds)
;
}
int z = 0; // To prevent printing unecessary Mouse click in the beginning
// Function to control mouse input
void MouseControl(int *x, int *y)
{
HANDLE hStdin;
DWORD fdwMode;
INPUT_RECORD irInBuf[128];
DWORD cNumRead;
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error: Unable to get standard input handle\n");
return;
}
// Enable mouse input events
fdwMode = ENABLE_EXTENDED_FLAGS | ENABLE_MOUSE_INPUT;
if (!SetConsoleMode(hStdin, fdwMode))
{
fprintf(stderr, "Error: Unable to set console mode\n");
return;
}
while (1)
{
if (!ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead))
{
fprintf(stderr, "Error: Unable to read console input\n");
return;
}
for (DWORD i = 0; i < cNumRead; i++)
{
if (irInBuf[i].EventType == MOUSE_EVENT)
{
MOUSE_EVENT_RECORD mouseEvent = irInBuf[i].Event.MouseEvent;
if (mouseEvent.dwEventFlags == 0 && mouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
*x = mouseEvent.dwMousePosition.X;
*y = mouseEvent.dwMousePosition.Y;
if (z == 0)
{
z = 1;
SetConsoleMode(hStdin, fdwMode & (8));
return;
}
SetConsoleMode(hStdin, fdwMode & (8));
// printf("Mouse clicked at (%d, %d)\n", mouseEvent.dwMousePosition.X, mouseEvent.dwMousePosition.Y);
return;
}
}
}
}
}
int lives = 8, choice, l = 0; // l to remove slight bugs during displaying
int num_of_correct_guesses;
void intro(); // Intro pages
int select_choice(); // To select the category
int return_current_status(char guessed_string[], char *current_status[], char selected_word[], int length); // Updating the current status of the string
void print_current_status(char *current_status[], int length); // Printing the current status of the string
void display_hangman(int result); // Displaying the updated Hangman podium
void sigint_handler(int sig);
int start_hangman()
{
while (1)
{
srand(time(0)); // Generating the seed to randomise the text to be predicted
printf("\e[1;1H\e[2J"); // To clear the terminal on Unix/Linux/Mac //Use: system("cls"); for Windows
intro();
char data[][500] = {
"Rome wasn't built in a day",
"All roads lead to Rome",
"When in Rome, do as the Romans do",
"Et tu, Brute?",
"To the victor belong the spoils",
"What we do in life echoes in eternity",
"Veni, vidi, vici",
"Senatus Populusque Romanus",
"I came, I saw, I conquered"
"Let him who desires peace prepare for war"
// "You couldn't live with your own failure, and where does that get you? Back to me",
// "The hardest choices require the strongest wills",
// "I am deadpool",
// "What is hate, if not love persevering",
// "I am iron man",
// "Whatever it takes",
// "Just because something works, doesn't mean it can't be improved", // Marvel Quotes
// "One small step for man, one giant leap for mankind",
// "Be the change that you wish to see in the world",
// "Live as if you were to die tomorrow. Learn as if you were to live forever",
// "The future depends on what you do today",
// "United we stand, divided we fall",
// "I'll be back",
// "Dream, dream, dream. Dreams transform into thoughts and thoughts result in action", // Famous quotes
// "Avengers",
// "Joker",
// "Interstellar",
// "Avatar",
// "The dark knight", // Famous movies
// "Introduction to computer science",
// "Introduction to electrical engineering",
// "Introduction to bioengineering",
// "Engineering realization",
// "Mathematics" // Graded sem 2 Courses
};
int n = 23;
int selected_index;
printf("\n\n");
display_hangman(lives);
for (int i = 0; i < 1000; i++) // To add a delay
rand();
choice = select_choice(); // Selecting the category
if (choice == 1)
selected_index = rand() % 11;
// else if (choice == 2)
// selected_index = rand() % 7 + 7;
// else if (choice == 3)
// selected_index = rand() % 5 + 14;
// else if (choice == 4)
// selected_index = rand() % 5 + 19;
// else
// {
// printf("Wrong Choice\n");
// return 0;
// }
char selected_word[500];
int length = strlen(data[selected_index]); // length of the selected word
strcpy(selected_word, data[selected_index]);
int num = 1, x = 0;
char player1[500], player2[500];
printf("\033[0;32m\n"); // Green colour
// printf("Enter the number of players: ");
// scanf("%d", &num);
printf("\033[0m\n"); // Default colour
int life[num]; // To store the lives of all the players
while (x < num) // x = current player index
{
fgetc(stdin); // To Enter on the keyboard
printf("\e[1;1H\e[2J");
lives = 8;
char guessed_string[500];
char *current_status[length];
for (int i = 0; i < length; i++) // Allocating memory to the current_status pointer array using DMA
current_status[i] = (char *)malloc(sizeof(char));
int spaces = 0;
for (int i = 0; i < length; i++)
{
if (selected_word[i] == ' ')
{
*current_status[i] = ' ';
spaces++; // To find the number of spaces
}
else // Inserting zero to the locations where a letter exists
*current_status[i] = '0';
}
int exit = 0; // To check if the whole string is correct
num_of_correct_guesses = 0;
printf("\033[0;36m\n"); // Cyan color
printf("\n DANIE'S HANGMAN GAME \n");
printf("\033[0m\n"); // Default color
while (num_of_correct_guesses < (length - spaces) && lives > 0)
{
if (choice == 1)
printf("\n\n Marvel Quotes\n");
else if (choice == 2)
printf("\n\n Famous Quotes\n");
else if (choice == 3)
printf("\n\n Famous Movies\n");
else if (choice == 4)
printf("\n\n Graded Sem2 Courses\n");
printf("\033[0;33m"); // Yellow color
printf("\n\nPlayer%d\n", (x + 1));
printf("\033[0m\n");
display_hangman(lives);
printf("\n**************************\n\n");
print_current_status(current_status, length);
printf("\n\nEnter a letter or directly predict the sentence!!\n\n!!!DO NOT REPEAT THE LETTERS ALREADY TYPED!!!\n\n\nNumber of lives left: %d\n", lives);
printf("\033[0;32m\n");
printf("\n\nYour Prediction:: ");
fgets(guessed_string, length + 1, stdin); // Accepting the letter or the entire string
printf("\033[0m\n");
if (l == 0)
{
printf("\033[0;36m\n");
printf("\n DANIE'S HANGMAN GAME \n");
printf("\033[0m\n");
}
if (guessed_string[1] == '\n') // While using fgets, it automatically insert a \n to the end. To remove that
guessed_string[1] = 0; // Zero can be inserted to a string to represent NULL/ otherwise a letter would be considered as a string
if (strcmp("quit\n", guessed_string) == 0)
{
printf("\e[1;1H\e[2J");
printf("\033[0;31m\n");
printf("You Quit!!!\n");
printf("\033[0m\n");
printf("The correct answer was: %s\n", selected_word);
display_hangman(0);
lives = 0;
break;
}
int status = return_current_status(guessed_string, current_status, selected_word, length);
if (status == 1) // letter is present
{
printf("\e[1;1H\e[2J");
printf("\033[0;32m\n"); // Green color
printf("Correct\n");
if (num_of_correct_guesses == length - spaces) // To check if the control has reached the end of the iteration
{
printf("\033[0m\n");
display_hangman(-1);
printf("\n**************************\n");
printf("\033[0;32m\n"); // green
printf("\nCongrats!! You have predicted successfully!!\n\n");
return_current_status(guessed_string, current_status, selected_word, length);
print_current_status(current_status, length);
printf("\033[0m\n"); // default
printf("\n********************\n");
break;
}
printf("\033[0m\n");
}
else if (status == 0) // letter guessed is absent in the string
{
printf("\e[1;1H\e[2J");
printf("\033[0;31m\n");
printf("WRONG GUESS\nYou lose a life!!");
printf("\033[0m\n");
lives--;
}
else if (status == 2) // Correctly guessed the String
{
printf("\e[1;1H\e[2J");
display_hangman(-1);
printf("\n**************************\n");
printf("\033[0;32m");
printf("\nCongrats!! You have predicted successfully!!\n\n");
return_current_status(guessed_string, current_status, selected_word, length);
print_current_status(current_status, length);
printf("\033[0m");
printf("\n********************\n");
fgetc(stdin);
break;
}
else if (status == 3) // Guessed string is wrong
{
printf("\033[0;31m\n");
printf("You lose a life!!\n");
printf("\033[0m\n");
}
if (lives == 0)
{
printf("\033[0;31m\n");
printf("You lose because you ran out of lives\n");
printf("\033[0m\n");
printf("The correct answer was: %s\n", selected_word);
display_hangman(lives); // DEAD!!
}
printf("\n********************\n");
l = 0;
if (status == 3) // If a wrong string was typed when we predicted the entire string, we get some extra characters in the input buffer
{
printf("\033[0;31m\n");
if (lives == 0)
printf("You predicted the Wrong String!!!\n\n");
else
printf("You predicted the Wrong String!!! Click Enter to continue");
l = 1;
while ((getchar()) != '\n')
; // In order to get rid of those extra characters
printf("\033[0m\n");
printf("\e[1;1H\e[2J");
}
}
for (int i = 0; i < length; i++) // Freeing the memory which was allocated to the current_status
free(current_status[i]);
printf("\n\nNumber of lives left: %d\n", lives);
life[x] = lives;
x++; // Next player
if (num > 1 && num != x)
{
printf("\033[0;36m");
printf("\n\nClick Enter for the next player:");
printf("\033[0m");
}
}
int dupe[num];
for (int i = 0; i < num; i++) // Duplicating the life array
dupe[i] = life[i];
printf("\033[0;36m");
printf("\n\nClick Enter to display the Leaderboard!!!");
fgetc(stdin);
printf("\033[0m\n\n");
printf("\e[1;1H\e[2J");
printf("\033[0;36m\n");
printf(" DANIE'S HANGMAN GAME\n\n");
printf("\033[0m\n");
printf("\n\n********************************************\n");
printf("\033[0;33m\n");
printf(" ________________________________________");
printf(" \n| !! LEADERBOARD !! |\n");
printf("| |\n");
printf("| |\n");
int winner = life[0], y, temp = 0, z = 0;
for (int i = 0; i < num; i++) // Sorting the life array from highest to lowest life
{
y = i;
winner = life[i];
for (int j = i + 1; j < num; j++)
{
if (life[j] > winner)
{
winner = life[j];
y = j;
}
}
temp = life[i];
life[i] = life[y];
life[y] = temp;
}
int pnum = 0, w = 0;
for (int i = 0; i < num; i++)
{
if (life[i] == life[i + 1] && i != num - 1) // To display the correct result when two players of same life exist
continue;
for (int j = 0; j < num; j++)
{
if (life[i] == dupe[j])
{
// The different conditions here are just to correctly align the data in the leaderboard while printing
if (pnum + 1 > 9)
printf("| %d. Player%d : Lives left: %d |\n", ++pnum, j + 1, life[i]);
else if (j + 1 > 9)
printf("| %d. Player%d : Lives left: %d |\n", ++pnum, j + 1, life[i]);
else
printf("| %d. Player%d : Lives left: %d |\n", ++pnum, j + 1, life[i]);
}
}
}
printf("| |\n");
printf("|________________________________________|\n\n");
printf("\033[0m\n");
printf("\n********************************************\n\n\n\n\n\n\n");
return 0;
}
}
int return_current_status(char guessed_string[], char *current_status[], char selected_word[], int length)
{
int status = 0;
if (guessed_string[1] == 0) // If a single letter was input by the user
{
for (int i = 0; i < length; i++) // To check whether the letter is present in the string or not
{
if (selected_word[i] == guessed_string[0] || tolower(selected_word[i]) == guessed_string[0])
{
*current_status[i] = selected_word[i];
status = 1;
num_of_correct_guesses++;
}
}
}
else // If the user directly wants to predict the string
{
if (strcmp(guessed_string, selected_word) == 0)
{
for (int i = 0; i < length; i++)
*current_status[i] = guessed_string[i];
return 2;
}
else
{
lives--;
return 3;
}
}
return status;
}
void print_current_status(char *current_status[], int length)
{
for (int i = 0; i < length; i++)
{
if (*current_status[i] == ' ')
printf("\n");
else if (*current_status[i] == '0')
printf("_ ");
else
printf("%c", *current_status[i]);
}
printf("\n");
}
int select_choice()
{
printf("\n\n\n\nWelcome to Hang the Man Game\n");
// printf("Choose a Category to play the Game.\n1. Marvel Quotes\n2. Famous Quotes\n3. Famous Movies\n4. Graded Sem 2 Courses\n\n\n");
printf("\033[0;32m\n");
// printf("Choice: ");
int choice = 1;
// scanf("%d", &choice);
printf("\033[0m\n");
printf("\n#*#*#*#*#*#*#*#*#*#*#*#*\n");
fgetc(stdin); // To remove extra \n from the input stream which may mess with the code!!
return choice;
}
void display_hangman(int result) // Displaying the hanged man based on the number of lives left
{
if (result == 8)
printf(" ___________________\n |/\n |\n |\n |\n |\n |\n |\n |________________________");
if (result == 7)
printf(" ___________________\n |/ |\n |\n |\n |\n |\n |\n |\n |________________________");
if (result == 6)
printf(" ___________________\n |/ |\n | |\n |\n |\n |\n |\n |\n |________________________");
if (result == 5)
printf(" ___________________\n |/ |\n | |\n | O\n |\n |\n |\n |\n |________________________");
if (result == 4)
printf(" ___________________\n |/ |\n | |\n | O\n | |\n | |\n |\n |\n |________________________");
if (result == 3)
printf(" ___________________\n |/ |\n | |\n | O\n | /|\n | |\n |\n |\n |________________________");
if (result == 2)
printf(" ___________________\n |/ |\n | |\n | O\n | /|\\\n | |\n |\n |\n |________________________");
if (result == 1)
printf(" ___________________\n |/ |\n | |\n | O\n | /|\\\n | |\n | / \n |\n |________________________");
if (result == 0)
printf(" ___________________\n |/ |\n | |\n | O\n | /|\\ YOU DIED!!!\n | |\n | / \\ \n |\n |________________________");
if (result == -1)
printf(" ___________________\n |/ \n |\033[0;32m YOU WIN!!\033[0m \n | \n | \n | \\O/\n | |\n | |\n |_________/_\\______________");
}
void intro()
{
// printf("\033[0;34m\n");//blue
printf("\n \x1b[33m");
char welcome[100] = "HANGMAN GAME";
for (int i = 0; i < strlen(welcome); i++)
{
putchar(welcome[i]);
fflush(stdout);
// delay(0.2);
}
printf("\x1b[0m");
printf("\n");
printf("\033[1;31m\033[0m\n"); // red
printf("\033[0m\n"); // default
printf("\n\n __________________________________________________________\n");
printf(" | HANGMAN GAME | \n");
printf(" | | \n");
printf(" | ____________ ____________ | \n");
printf(" | | | | | \n");
printf(" | | O | | \n");
printf(" | | /|\\ | | \n");
printf(" | | / \\ | \\ O / | \n");
printf(" | | | | | \n");
printf(" | |___________ |_____/_\\_____ | \n");
printf(" | | \n");
printf(" | Welcome to Danie's Hangman Game!! | \n");
printf(" | Guess the string and save the stickman's life! | \n");
printf(" |________________________________________________________| \n");
printf("\033[0;32m\n"); // Green
printf("\n\n\nClick Enter to continue: ");
fgetc(stdin);
printf("\033[0m\n");
printf("\e[1;1H\e[2J");
printf("\033[0;36m\n");
printf("\n DANIE'S HANGMAN GAME \n");
printf("\033[0m\n");
printf("\033[0m\n");
printf("\n\n _____________________________________________________________\n");
printf(" || ||\n");
printf(" || RULES AND REGULATIONS ||\n");
printf(" || ||\n");
printf(" || ||\n");
printf(" || 1. The game has both single and multiplayer modes. ||\n");
printf(" || 2. A category can be chosen from a given set. ||\n");
printf(" || 3. Any number of players can compete ||\n");
printf(" || against each other. ||\n");
printf(" || 4. In a game, all the players will be given the ||\n");
printf(" || same word/quote randomly and the winner is decided ||\n");
printf(" || on the basis of which player has accurately ||\n");
printf(" || guessed the answer consuming the least number ||\n");
printf(" || of lives. ||\n");
printf(" || 5.Each player will have 8 lives. ||\n");
printf(" || 6.In every round, a player can either predict ||\n");
printf(" || a letter in the word/quote or the entire string ||\n");
printf(" || itself if confident. ||\n");
printf(" || 7. In either case, If their prediction is wrong, ||\n");
printf(" || they lose a life. ||\n");
printf(" || 8. At any point in the game, if the player feels like ||\n");
printf(" || giving up, they can do so by typing \"quit\". ||\n");
printf(" || ||\n");
printf(" || ||\n");
printf(" || ");
printf("\033[0;31m"); // red
printf("PRECAUTION!!: Do not re-enter your predicted letter!!");
printf("\033[0m");
printf(" ||\n");
printf(" || ||\n");
printf(" ||__________________________________________________________||\n");
printf("\033[0;32m\n");
printf("\n\n\nClick Enter if you have properly read the Rules: ");
fgetc(stdin);
printf("\033[0m\n");
printf("\e[1;1H\e[2J");
printf("\033[0;36m\n");
printf("\n HANGMAN GAME \n");
printf("\033[0m\n");
return;
}
int Hangtheman()
{
int x = start_hangman();
}
// Snakegame (2)...................................................................................................
int Snakegame()
{
printf("Sorry, this minigame is available for MacOS only\n");
return 1;
}
// Minesweeper (3) .................................................................................................
// Function to delay output on terminal
// Function to generate the minefield
void genField(int size, int **field)
{
// Initialize random number generator
srand(time(NULL));
// Allocate memory for the minefield
*field = (int *)malloc(size * size * sizeof(int));
// Initialize field with 0s
for (int i = 0; i < size * size; i++)
{
(*field)[i] = 0;
}
// Generate mines
int mines = size * size / 6; // Adjust the density of mines as needed
for (int k = 0; k < mines; k++)
{
int x = rand() % size;
int y = rand() % size;
if ((*field)[x * size + y] != -1)
(*field)[x * size + y] = -1; // -1 represents a mine
else
k--;
}
// Calculate the number of mines in proximity for each cell
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if ((*field)[i * size + j] != -1)
{ // If the cell is not a mine
int count = 0;
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
int nx = i + dx;
int ny = j + dy;
if (nx >= 0 && nx < size && ny >= 0 && ny < size && (*field)[nx * size + ny] == -1)
{
count++;
}
}
}
(*field)[i * size + j] = count;
}
}
}
}
// Function to reveal clicked position
void reveal(int size, int x, int y, int *field, int *revealed)
{
if (x < 0 || x >= size || y < 0 || y >= size || revealed[x * size + y])
return;
revealed[x * size + y] = 1;
if (field[x * size + y] == 0)
{
for (int dx = -1; dx <= 1; dx++)
{
for (int dy = -1; dy <= 1; dy++)
{
reveal(size, x + dx, y + dy, field, revealed);
}
}
}
}
// Function to flag or unflag clicked position
void flag(int size, int x, int y, int *field, int *revealed, int *flag_count)
{
if (x < 0 || x >= size || y < 0 || y >= size)
return;
// Check if the clicked position is already flagged
if (revealed[x * size + y] == -1)
{
// Unflag the position
revealed[x * size + y] = 0;
(*flag_count)++;
}
else
{
// Flag the position
revealed[x * size + y] = -1;
(*flag_count)--;
}
// You may want to add UI updates here for flagging/unflagging
}
// Function to print the minefield with revealed cells
void printReveal(int size, int *revealed, int *field, int flag_mode)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (!revealed[i * size + j])
{
printf("\033[32m# \033[0m"); // Hidden cell
}
else if (revealed[i * size + j] == -1)
{
printf("\033[31mF \033[0m"); // Flagged cell
}
else
{
printf("%d ", field[i * size + j]); // Number of adjacent mines
}
}
// Print Flag Mode indicator
if (i == size / 2)
{
printf("\t%d \033[33m:flagmode\033[0m", flag_mode);
}
printf("\n");
}
}
// Function to check if a cell contains a mine
int yesnoMine(int x, int y, int *field, int size)
{
if (field[x * size + y] == -1)
return 1;
return 0;
}
// Function to count the number of revealed cells
int numRevealed(int size, int *revealed)
{
int count = 0;
for (int i = 0; i < size * size; i++)
{
if (revealed[i] == 1)
count++;
}
// printf("%d\n", count); // Debugging output
delay(1);
return count;
}
// Function to cheat and reveal the entire minefield
void cheat(int size, int *field)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d ", field[i * size + j]); // Print the content of each cell
}
printf("\n");
}
delay(2);
}
// Function to play Minesweeper
int playMinesweeper(int size)
{
printf("\e[1;1H\e[2J");
printf("\033[34m");
// UI for Game introduction
char title[100] = "Welcome to Minesweeper";
for (int i = 0; i < strlen(title); i++)
{
printf("%c", title[i]);
// delay(0.2);
}
printf("\033[0m");
int *field;
int *revealed;
int flag_count = size * size / 6; // Initial flag count equals mine count
int click_x, click_y;
int flag_mode = 0; // Flag mode indicator
// Allocate memory for the revealed array
revealed = (int *)malloc(size * size * sizeof(int));
// Initialize revealed array to all zeros (initially non revealed)
for (int i = 0; i < size * size; i++)
{
revealed[i] = 0;
}
// Generate the minefield
genField(size, &field);
// Cheat to reveal the entire minefield (for debugging purposes)
// cheat(size, field);
// Main game loop
int c = 0;
while (1)
{
// Take user input
MouseControl(&click_x, &click_y);
// Change mode to "Flag" by pressing "F" input
if (click_x == 2 * size + 4 && click_y == size / 2)
{
flag_mode = !flag_mode;
}
// Check victory condition
if (numRevealed(size, revealed) == (size * size) - size * size / 6)
{
free(field);
free(revealed);
printf("\033[36m\nCongratulations! You have won!\n\n\033[0m");
HANDLE hStdin;
DWORD fdwMode;
DWORD cNumRead;
SetConsoleMode(hStdin, fdwMode & ~(ENABLE_MOUSE_INPUT));
return 1;
}
// Check lose condition
if (flag_mode == 0 && yesnoMine(click_y, click_x / 2, field, size))
{
free(field);
free(revealed);
printf("\n\033[31mGame over! You stepped on a mine.\n\033[0m\n\n");
HANDLE hStdin;
DWORD fdwMode;
DWORD cNumRead;
SetConsoleMode(hStdin, fdwMode & ~(ENABLE_MOUSE_INPUT));
return 0;
}
// Reveal or flag clicked areas based on flag mode
if (flag_mode == 1)
{
flag(size, click_y, click_x / 2, field, revealed, &flag_count);
}
else
{
reveal(size, click_y, click_x / 2, field, revealed);
}
// Clear the screen and print the updated minefield
printf("\e[1;1H\e[2J");
printReveal(size, revealed, field, flag_mode);
printf("\nNumber of Flags Left: %d\n", flag_count);
printf("\033[34m\n\nMINESWEEPER\n\033[0m");
printf("Status: ");
}
}
int Minesweeper()
{
printf("This game may cause problems in future control, proceed at your own risk\nYou may choose to skip this game (complete quest automatically)\n");
printf("Enter (0) to play:\n");
printf("Enter (1) to skip minigame\n");
int choice;
scanf(" %d", &choice);
if (choice == 1)
return 1;
int resMine = playMinesweeper(6);
// Minesweeper Rules
printf("\n\n _____________________________________________________________\n");
printf(" || ||\n");
printf(" || RULES AND REGULATIONS ||\n");
printf(" || ||\n");
printf(" || 1. The game will automatically generate a minefield of||\n");
printf(" || a specified size (6x6). The minefield ||\n");
printf(" || contains hidden cells, some of which may contain ||\n");
printf(" || mines. ||\n");
printf(" || 2. Objective: The objective of Minesweeper is to ||\n");
printf(" || uncover all the cells on the grid that do not ||\n");
printf(" || contain mines without detonating any of the mines. ||\n");
printf(" || Controls: ||\n");
printf(" || Mouse Click: Click on a cell to reveal it. ||\n");
printf(" || If the revealed cell contains a mine, the game ends||\n");
printf(" || 3. Flagging Mode (Optional): ||\n");
printf(" || Press \"F\"to toggle flagging mode.In flagging mode ||\n");
printf(" || you can flag cells that you suspect contain mines ||\n");
printf(" || to avoid accidentally clicking on them. Flagging a ||\n");
printf(" || cell prevents it from being clicked. ||\n");
printf(" || 4. Mouse Movement: Move the mouse cursor to position ||\n");
printf(" || the click. ||\n");
printf(" || 5. Number of Flags Left: The number of remaining flags||\n");
printf(" || is displayed on the screen. ||\n");
printf(" || 6. Gameplay: ||\n");
printf(" || Revealing Cells: Left-click on a cell to reveal it.||\n");
printf(" || If the cell is empty, it will show the number of ||\n");
printf(" || adjacent mines. If it contains a mine,the game ends||\n");
printf(" || 7. In flagging mode, left-click on a cell to flag it ||\n");
printf(" || as containing a mine. ||\n");
printf(" || Win Condition: You win the game when all non-mine ||\n");
printf(" || cells are revealed. ||\n");
printf(" || Lose Condition: You lose the game if you reveal a ||\n");
printf(" || cell containing a mine. ||\n");
printf(" || ||\n");
printf(" ||||\n");
return resMine;
}
// Memorygame (4)..............................................................................................
// Function to swap two integers
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function to shuffle an array
void shuffle_array(int arr[], int n)
{
srand(time(NULL)); // Seed for random number generator
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1); // Generate random index from 0 to i
swap(&arr[i], &arr[j]); // Swap elements at i and j
}
}
// Function to generate the random matrix
void genMat(int size, int num, int mat[size][size])
{
int arr[num];
int count = 0;
for (int i = 0; i < num; i++)
arr[i] = i + 1;
shuffle_array(arr, num);
while (count < num)
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size / 2; j++)
{
mat[i][j] = arr[count];
count++;
}
}
count = 0;
shuffle_array(arr, num);
while (count < num)
{
for (int i = 0; i < size; i++)
{
for (int j = size / 2; j < size; j++)
{
mat[i][j] = arr[count];
count++;
}
}
}
delay(0.5);
}
// Functions for terminal display (Need UI Change)
void printMemory1(int size, int num, int mat[size][size], int prog_arr[num])
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
int k;
for (k = 0; k < num; k++)
{
if (mat[i][j] == prog_arr[k])
{
break;
}
}
if (k != num)
printf("%d ", mat[i][j]);
else
printf("\x1b[33m# \x1b[0m");
}
printf("\n");
}
printf("\x1b[34m\n\n\nMEMORY GAME!!!! \n\x1b[0m\n");
}
void printMemory2(int size, int num, int mat[size][size], int prog_arr[num], int click1_x, int click1_y, int click2_x, int click2_y)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
int k;
for (k = 0; k < num; k++)
{
if (mat[i][j] == prog_arr[k])
{
break;
}
}
if (k != num)
printf("%d ", mat[i][j]);
else if (i == click1_y && j == click1_x / 2)
{
printf("%d ", mat[i][j]);
}
else if (i == click2_y && j == click2_x / 2)
{