-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_data.c
699 lines (699 loc) · 16.7 KB
/
game_data.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <Windows.h>
void gamePage();
#if _WIN32
#define CLEAR_THE_SCREEN system("cls")
#define MAX_LIMIT 100
struct TIME
{
int seconds;
int minutes;
};
struct GameData
{
char player_one[MAX_LIMIT];
char player_two[MAX_LIMIT];
int game_status;
struct TIME time_of_game_play;
struct TIME start;
struct TIME stop;
};
SYSTEMTIME str_t;
char game_array[3][3];
struct GameData data;
void developerInformation();
void welcome();
void howToPlay();
void thankYou();
void loading();
void pressAkeyToContinue();
void badInput();
void gameHomeDisplay();
void option();
void doublePlayerMode();
void initializeGameArray();
void takeUserData();
void creatingBoard();
int markingBoard(char[], char);
int check();
void gameStatistics();
void textColorDisplay();
void screenBrightness();
char optionChoice();
void writeGameData();
void timeOfGamePlay();
int main()
{
FILE *out_check_data_file;
char position;
goto Start;
DoublePlayerMode:
loading();
doublePlayerMode();
printf("\n%10s", "");
if (data.game_status == 1)
{
printf("%s WON THE GAME", data.player_one); }
else if (data.game_status == 2)
{
printf("%s WON THE GAME", data.player_two); }
else if (data.game_status == -1)
{
printf("GAME IS DRAW");
}
pressAkeyToContinue();
writeGameData();
goto Home;
Option:
loading();
gamePage(" Options ");
option();
position = optionChoice();
switch (position)
{
case 'G':
loading();
gamePage("GAME STATISTICS");
gameStatistics();
pressAkeyToContinue();
goto Option;
break;
case 'T':
goto TextColor;
break;
case ('S'):
goto ScreenBrightness;
break;
case ('B'):
goto Home;
break;
case 'H':
loading();
gamePage("HOW TO PLAY?"); howToPlay();
pressAkeyToContinue();
goto Option;
break;
case 'D':
loading();
gamePage("DEVELOPER"); developerInformation();
pressAkeyToContinue();
goto Option;
break;
default:
loading();
badInput();
pressAkeyToContinue(); goto Option;
break;
}
TextColor:
loading();
gamePage("TEXT COLOR"); textColorDisplay();
position = optionChoice();
switch (position)
{
case '1':
system("color 1");
break;
case '2':
system("color 2");
break;
case '3':
system("color 3");
break;
case '4':
system("color 4");
break;
case '5':
system("color 5");
break;
case '6':
system("color 6"); break;
case '7':
system("color 7"); break;
case '8':
system("color 8"); break;
case '9':
system("color 9"); break;
case 'A':
system("color A"); break;
case 'B':
system("color B"); break;
case 'C':
system("color C"); break;
case 'D':
system("color D"); break;
case 'E':
system("color E"); break;
case 'F':
system("color F"); break;
default:
loading();
badInput();
pressAkeyToContinue();
goto TextColor;
break;
}
pressAkeyToContinue();
goto Option;
ScreenBrightness:
loading();
gamePage("SCREEN BRIGHTNESS");
screenBrightness();
position = optionChoice();
switch (position)
{
case '1':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,10)"); break;
case '2':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,20)"); break;
case '3':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,30)"); break;
case '4':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,40)"); break;
case '5':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,50)"); break;
case '6':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,60)"); break;
case '7':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,70)"); break;
case '8':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,80)"); break;
case '9':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,90)"); break;
case 'A':
system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,100)"); break;
default:
badInput();
pressAkeyToContinue();
goto ScreenBrightness;
break;
}
pressAkeyToContinue();
goto Option;
Start:
out_check_data_file = fopen("./Data/Statistics.DAT", "r");
if (out_check_data_file == NULL)
{
system("attrib +h +s Data");
welcome();
pressAkeyToContinue();
fclose(out_check_data_file);
}
goto Home;
Home:
loading();
gameHomeDisplay();
position = optionChoice();
switch (position)
{
case 'D':
goto DoublePlayerMode;
break;
case 'O':
goto Option;
break;
case 'E':
loading();
gamePage("THANK YOU");
thankYou();
pressAkeyToContinue();
exit(0);
default:
loading();
badInput();
pressAkeyToContinue();
goto Home;
}
}
void developerInformation()
{
"This will show the developer information"; FILE *out_developer;
char ch;
out_developer = fopen("./Data/Developer.txt", "r");
while (1)
{
ch = fgetc(out_developer);
if (ch == EOF)
{
break;
}
printf("%c", ch);
Sleep(10);
}
fclose(out_developer);
}
void welcome()
{
"This will welcome the new user";
gamePage("WELCOME");
FILE *out_welcome;
char ch;
out_welcome = fopen("./Data/Welcome.txt", "r");
while (1)
{
ch = fgetc(out_welcome);
if (ch == EOF)
{
break;
}
printf("%c", ch);
Sleep(15);
}
fclose(out_welcome);
}
void howToPlay()
{
"This will teach user how to play the game";
FILE *out_how_to_play;
char ch;
out_how_to_play = fopen("./Data/How_To_Play.txt", "r");
while (1)
{
ch = fgetc(out_how_to_play);
if (ch == EOF)
{
break;
}
printf("%c", ch);
Sleep(5);
}
fclose(out_how_to_play);
}
void thankYou()
{
"This will thanks to the user who had played the game"; FILE *out_thank_you;
char ch;
int sml = 2;
out_thank_you = fopen("./Data/Thank_You.txt", "r");
while (1)
{
ch = fgetc(out_thank_you);
if (ch == EOF)
{
break;
}
printf("%c", ch);
Sleep(20);
}
printf("\n\n");
for (int i = 0; i < 25; i++)
{
printf("%c", sml);
Sleep(20);
}
fclose(out_thank_you);
}
void loading()
{
"A pattern of loading the data";
int i = 0, j = 0;
CLEAR_THE_SCREEN;
gamePage("");
printf("%35s%-15s\n", "", "Loading");
printf("%35s", "");
while (i < 10)
{
Sleep(50);
printf("\xdb");
i++;
}
}
void pressAkeyToContinue()
{
"Ask user a key to press to move ahead"; printf("\n\n%30s", "");
system("pause");
}
void badInput()
{
"Display the bad input information to the user"; gamePage("BAD INPUT");
printf("%25sPlease try to given valid input.\n", "");
printf("\n%5sTry again with valid input.\n", " "); }
void gameHomeDisplay()
{
"This will show the home page of screen with option";
gamePage(" TIC TAK TOE");
printf("\n%30s%-30s", "", "Double Player Mode");
printf("\n%30s%-30s", "", "Option");
printf("\n%30s%-30s", "", "Exit");
}
void doublePlayerMode()
{
"Double Player Game Mode";
char position[3], player_symbol;
int player = 1, mark_status = 1;
initializeGameArray();
takeUserData();
do
{
TOP:
printf("%10s", "");
gamePage("DOUBLE PLAYER MODE");
creatingBoard();
player = (player % 2) ? 1 : 2;
(player == 1) ? printf("%s\nPosition: ", data.player_one) : printf("%s\nPosition: ", data.player_two);
scanf("%s", &position);
player_symbol = (player == 1) ? 'X' : 'O';
mark_status = markingBoard(position, player_symbol);
if (mark_status == 0)
{
printf("\n%10sIt may have been already choosen or the option doesnot exist.\n", ""); pressAkeyToContinue();
goto TOP;
}
data.game_status = check();
player++;
} while (data.game_status == 0);
gamePage("DOUBLE PLAYER MODE");
creatingBoard();
GetSystemTime(&str_t);
data.stop.minutes = str_t.wMinute;
data.stop.seconds = str_t.wSecond;
}
void initializeGameArray()
{
"Make all the element of the game array to space";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
game_array[i][j] = ' ';
}
}
}
void takeUserData()
{
"This takes user data like player one name, player two name";
gamePage("TAKING USER INPUT");
printf("%30sPlayer One: ", "");
scanf(" %[^\n]", data.player_one);
printf("\n");
printf("%30sPlayer Two: ", "");
scanf(" %[^\n]", data.player_two);
data.game_status = 0;
printf("\n%30sPress any key to start the game", "");
getch();
GetSystemTime(&str_t);
data.start.minutes = str_t.wMinute;
data.start.seconds = str_t.wSecond;
}
void creatingBoard()
{
"Display TIC TAK TOE Board";
printf("%30s%s(X) vrs %s(O) \n\n", "", data.player_one, data.player_two);
printf("%15s ----------------------- %4s", "", "");
printf("|");
printf("%4s ----------------------- \n", "");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s| %c | %c | %c |%4s", "", game_array[0][0], game_array[0][1], game_array[0][2], "");
printf("|");
printf("%4s| %s | %s | %s |\n", "", "NW", "NN", "NE");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s ----------------------- %4s", "", "");
printf("|");
printf("%4s ----------------------- \n", "");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s| %c | %c | %c |%4s", "", game_array[1][0], game_array[1][1], game_array[1][2], "");
printf("|");
printf("%4s| %s | %s | %s |\n", "", "CW", "CC", "CE");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s ----------------------- %4s", "", "");
printf("|");
printf("%4s ----------------------- \n", "");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s| %c | %c | %c |%4s", "", game_array[2][0], game_array[2][1], game_array[2][2], "");
printf("|");
printf("%4s| %s | %s | %s |\n", "", "SW", "SS", "SE");
printf("%15s| | | |%4s", "", "");
printf("|");
printf("%4s| | | |\n", "");
printf("%15s ----------------------- %4s", "", "");
printf("|");
printf("%4s ----------------------- \n\n", "");
}
int markingBoard(char position[], char player)
{
"Update Character Array";
if (strcmpi(position, "NW") == 0 || strcmpi(position, "NN") == 0 || strcmpi(position, "NE") == 0 || strcmpi(position, "CW") == 0 || strcmpi(position, "CC") == 0 || strcmpi(position, "CE") == 0 ||
strcmpi(position, "SW") == 0 || strcmpi(position, "SS") == 0 || strcmpi(position, "SE") == 0) {
if (strcmpi(position, "NW") == 0)
{
if (game_array[0][0] != ' ')
{
return 0;
}
game_array[0][0] = player;
}
else if (strcmpi(position, "NN") == 0)
{
if (game_array[0][1] != ' ')
{
return 0;
}
game_array[0][1] = player;
}
else if (strcmpi(position, "NE") == 0)
{
if (game_array[0][2] != ' ')
{
return 0;
}
game_array[0][2] = player;
}
else if (strcmpi(position, "CW") == 0)
{
if (game_array[1][0] != ' ')
{
return 0;
}
game_array[1][0] = player;
}
else if (strcmpi(position, "CC") == 0)
{
if (game_array[1][1] != ' ')
{
return 0;
}
game_array[1][1] = player;
}
else if (strcmpi(position, "CE") == 0)
{
if (game_array[1][2] != ' ')
{
return 0;
}
game_array[1][2] = player;
}
else if (strcmpi(position, "SW") == 0) {
if (game_array[2][0] != ' ')
{
return 0;
}
game_array[2][0] = player;
}
else if (strcmpi(position, "SS") == 0) {
if (game_array[2][1] != ' ')
{
return 0;
}
game_array[2][1] = player;
}
else if (strcmpi(position, "SE") == 0) {
if (game_array[2][2] != ' ')
{
return 0;
}
game_array[2][2] = player;
}
return 1;
}
else
{
return 0;
}
}
int check()
{
"Check whether anyone has won or not";
if (game_array[1][1] == 'X')
{
if ((game_array[0][0] == 'X' && game_array[2][2] == 'X') || (game_array[0][2] == 'X' && game_array[2][0] == 'X') || (game_array[0][1] == 'X' && game_array[2][1] == 'X') || (game_array[1][0] == 'X' && game_array[1][2] == 'X'))
{
return 1;
}
}
if (game_array[0][0] == 'X')
{
if ((game_array[1][0] == 'X' && game_array[2][0] == 'X') || (game_array[0][1] == 'X' && game_array[0][2] == 'X'))
{
return 1;
}
}
if (game_array[2][2] == 'X')
{
if ((game_array[0][2] == 'X' && game_array[1][2] == 'X') || (game_array[2][0] == 'X' && game_array[2][1] == 'X'))
{
return 1;
}
}
if (game_array[1][1] == 'O')
{
if ((game_array[0][0] == 'O' && game_array[2][2] == 'O') || (game_array[0][2] == 'O' && game_array[2][0] == 'O') || (game_array[0][1] == 'O' && game_array[2][1] == 'O') || (game_array[1][0] == 'O' && game_array[1][2] == 'O'))
{
return 2;
}
}
if (game_array[0][0] == 'O')
{
if ((game_array[1][0] == 'O' && game_array[2][0] == 'O') || (game_array[0][1] == 'O' && game_array[0][2] == 'O'))
{
return 2;
}
}
if (game_array[2][2] == 'O')
{
if ((game_array[0][2] == 'O' && game_array[1][2] == 'O') || (game_array[2][0] == 'O' && game_array[2][1] == 'O'))
{
return 2;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (game_array[i][j] == ' ')
{
return 0;
}
}
}
return -1;
}
void timeOfGamePlay()
{
"Calculate time of game play between two player";
data.time_of_game_play.minutes = data.stop.minutes - data.start.minutes; data.time_of_game_play.seconds = data.stop.seconds - data.start.seconds;
if (data.time_of_game_play.seconds < 0)
{
data.time_of_game_play.seconds = data.time_of_game_play.seconds + 60; data.time_of_game_play.minutes = data.time_of_game_play.minutes - 1; }
}
void writeGameData()
{
"Write the game data to the file";
FILE *in_game_data;
in_game_data = fopen("./Data/Statistics.DAT", "a");
if (in_game_data == NULL)
{
return;
}
timeOfGamePlay();
fprintf(in_game_data, "%-15s%-20s%-20s %5dsec %s%s\n", __DATE__, data.player_one, data.player_two, data.time_of_game_play.minutes * 60 + data.time_of_game_play.seconds, (data.game_status == -1) ? ("Draw") : ((data.game_status == 1) ? (data.player_one) : (data.player_two)), (data.game_status != -1) ? (" won") : (" "));
fclose(in_game_data);
}
void gameStatistics()
{
"Disply all the game record that has been stored in our data base";
FILE *out_game_data;
char ch;
out_game_data = fopen("./Data/Statistics.DAT", "r");
if (out_game_data == NULL)
{
printf("%10sSome error occured while retrieving the data.\n", "");
printf("%10sPlease try again later\n", "");
printf("%10sIf it doesn't work try restarting the game.\n", "");
return;
}
printf("\n\n%-15s%-20s%-20s%-20s%-30s\n", "DATE", "PLAYER ONE", "PLAYER TWO", "TIME OF GAME PLAY", "RESULT");
for (int i = 0; i < 90; i++)
{
printf("=");
}
printf("\n\n");
while (1)
{
ch = fgetc(out_game_data);
if (ch == EOF)
{
break;
}
printf("%c", ch);
}
fclose(out_game_data);
}
void option()
{
"Show option for option in Home page";
printf("\n%30s%-30s", "", "Game Statistics");
printf("\n%30s%-30s", "", "Text Color");
printf("\n%30s%-30s", "", "Screen Brightness");
printf("\n%30s%-30s", "", "How to Play?");
printf("\n%30s%-30s", "", "Developer");
printf("\n%30s%-30s", "", "Back");
}
char optionChoice()
{
"This will let user to position a option and return the choiced option"; char position;
printf("\n\n%28s%-20s", " ", "Choose any one of the above?\n"); printf("%25s-->", " ");
fflush(stdin); /*This line is important*/
position = getchar();
position = toupper(position);
return position;
}
void textColorDisplay()
{
"Option for Text Color";
printf("%25s%-15s%10s%-15s\n", "", "1 = Blue ", "", "9 = Light Blue "); printf("%25s%-15s%10s%-15s\n", "", "2 = Green ", "", "A = Light Green "); printf("%25s%-15s%10s%-15s\n", "", "3 = Aqua ", "", "B = Light Aqua "); printf("%25s%-15s%10s%-15s\n", "", "4 = Red ", "", "C = Light Red "); printf("%25s%-15s%10s%-15s\n", "", "5 = Purple", "", "D = Light Purple"); printf("%25s%-15s%10s%-15s\n", "", "6 = Yellow", "", "E = Light Yellow"); printf("%25s%-15s%10s%-15s\n", "", "7 = White ", "", "F = Bright White"); printf("%25s%-15s%10s%-15s\n", "", "8 = Gray ", "", "");
}
void screenBrightness()
{
"Option for screen Display";
printf("%30s%-15s\n", "", " 1 = 10% Brightness ");
printf("%30s%-15s\n", "", " 2 = 20% Brightness ");
printf("%30s%-15s\n", "", " 3 = 30% Brightness ");
printf("%30s%-15s\n", "", " 4 = 40% Brightness ");
printf("%30s%-15s\n", "", " 5 = 50% Brightness ");
printf("%30s%-15s\n", "", " 6 = 60% Brightness ");
printf("%30s%-15s\n", "", " 7 = 70% Brightness ");
printf("%30s%-15s\n", "", " 8 = 80% Brightness ");
printf("%30s%-15s\n", "", " 9 = 90% Brightness ");
printf("%30s%-15s\n", "", " A = 100% Brightness ");
}
#elif __APPLE__
void main()
{
gamePage("MACHINE ERROR");
printf("%10sThis game is still under processing to be run in Mac machine", "");
getch();
}
#elif __linux__
void main()
{
gamePage("Machine ERROR");
printf("%10sThis game is still under processing to be run in Linux machine", "");
getch();
}
#endif
void gamePage(char name[MAX_LIMIT])
{
"Show the passed string as a heading";
CLEAR_THE_SCREEN;
printf("%35s%-25s\n", "", name);
printf("%35s", "");
for (int i = 0; i < strlen(name); i++)
{
printf("=");
}
printf("\n\n\n");
}