This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menus&Stats.c
676 lines (632 loc) · 21.3 KB
/
Menus&Stats.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include "Menus&Stats.h"
/* Function that allows the user to select a different menu in the bottom of the window
param: the event of the mouse left button press
param: variable indicating the current menu
param; the name of the player
param: auxiliar size of the board
*/
void BottomSelect(SDL_Event event, int *gamestate, char *playername, int *auxsize)
{
if(event.button.y > 700)
{
if(event.button.x > XHOME && event.button.x < (XHOME + XHOMESIZE))
{
*gamestate = 0;
}
else if(event.button.x > XNEWGAME && event.button.x < (XNEWGAME + XNGAMESIZE))
{
*auxsize = 0;
*gamestate = 2;
}
else if(event.button.x > XSTATISTICS && event.button.x < (XSTATISTICS + XSTATISIZE))
{
*gamestate = 3;
}
}
}
/* Function that allows the user to select an option in the home menu
param: the event of the mouse left button press
param: variable indicating the current menu
param: size of the board
param: name of the player
param: variable indicating how many plays have already been made
param; the generated combination of colors for the current game
param: auxiliar size of the board
*/
void HomeSelect(SDL_Event event, int *gamestate, int boardsize, char *playername, int nplays, int *combination, int *auxsize)
{
if(event.button.x > XTEXTBOX && event.button.x < XTEXTBOX + BOXWIDTH &&
event.button.y > YTEXTBOX && event.button.y < YTEXTBOX + 3*BOXHEIGHT)
{
if(event.button.y > YTEXTBOX && event.button.y < (YTEXTBOX + BOXHEIGHT) &&
boardsize >= MINLEVEL && boardsize <= MAXBOARD && playername[0] != 0)
{
*gamestate = 1;
}
else if(event.button.y > (YTEXTBOX + BOXHEIGHT) && event.button.y < (YTEXTBOX + 2*BOXHEIGHT))
{
*auxsize = 0;
*gamestate = 2;
}
else if(event.button.y > (YTEXTBOX + 2*BOXHEIGHT) && event.button.y < (YTEXTBOX + 3*BOXHEIGHT))
{
*gamestate = 3;
}
}
}
/* Function to select the size of the board
param: the event of the mouse left button press
param: size of the board
*/
void SizeSelect(SDL_Event event, int *boardsize)
{
if(event.button.x > XNUMEROS && event.button.x < XNUMEROS + NUMBERWIDTH &&
event.button.y > YNUMEROS && event.button.y < YNUMEROS + MAX_LEVELS*NUMBERSIZE)
{
if(event.button.y > YNUMEROS && event.button.y < (YNUMEROS + NUMBERSIZE))
{
*boardsize = 4;
}
else if(event.button.y > (YNUMEROS + NUMBERSIZE) && event.button.y < (YNUMEROS + 2*NUMBERSIZE))
{
*boardsize = 5;
}
else if(event.button.y > (YNUMEROS + 2*NUMBERSIZE) && event.button.y < (YNUMEROS + 3*NUMBERSIZE))
{
*boardsize = 6;
}
else if(event.button.y > (YNUMEROS + 3*NUMBERSIZE) && event.button.y < (YNUMEROS + 4*NUMBERSIZE))
{
*boardsize = 7;
}
else if(event.button.y > (YNUMEROS + 4*NUMBERSIZE) && event.button.y < (YNUMEROS + MAX_LEVELS*NUMBERSIZE))
{
*boardsize = 8;
}
}
}
/* Function that makes the reading of the player name
param: the event of the keyboard press
param: the name of the player
param: the position of the next letter
*/
void NameReading(SDL_Event event, char *playername, int *namepos)
{
int a=0;
a=*namepos;
if(event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z)
{
playername[*namepos] = event.key.keysym.sym - SDLK_a + 'a';
*namepos = *namepos + 1;
}
else if(event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9)
{
playername[*namepos] = event.key.keysym.sym - SDLK_0 + '0';
*namepos = *namepos + 1;
}
switch(event.key.keysym.mod)
{
case KMOD_CAPS:
if(playername[a] >= 'a' && playername[a] <= 'z')
{
playername[a] = playername[a] - 32;
}
break;
case KMOD_LSHIFT:
if(playername[a] >= 'a' && playername[a] <= 'z')
{
playername[a] = playername[a] - 32;
}
break;
case KMOD_RSHIFT:
if(playername[a] >= 'a' && playername[a] <= 'z')
{
playername[a] = playername[a] - 32;
}
break;
default:
break;
}
}
/* Function that allos the user to erase a letter
param: the event of the keyboard press
param: the name of the player
param: the position of the next letter
*/
void LetterErase(SDL_Event event, char *playername, int *namepos)
{
if(event.key.keysym.sym == SDLK_BACKSPACE)
{
*namepos = *namepos - 1;
playername[*namepos] = 0;
}
}
/* Function that erases all the user name
param: the name of the player
*/
void NameCleanse(char *playername)
{
int i=0;
for(i=0; i < MAXNAME; i++)
{
playername[i] = 0;
}
}
/* Function that allows the user to change the naem
param: the event of the keuboardpress
param: the name of the player
param: the variable indicating if the reading of the player name is on
param: position of the next letter in the player name
param: variable indicating the state of the game
*/
void NameInsert(SDL_Event event, char *playername, int nameread, int *namepos, int gamestate)
{
if(gamestate == 2 && nameread == 1 && *namepos < MAXNAME)
{
NameReading(event, playername, namepos);
LetterErase(event, playername, namepos);
}
}
/* Function that allows the user to select the box for writing the name
param: the event of the mouse left button press
param: variable indicating if the reading of the player name is on
*/
void NameSelect(SDL_Event event, int *nameread)
{
if(event.button.x > XNAMEBOX && event.button.x < XNAMEBOX + NBOXWIDTH &&
event.button.y > YNAMEBOX && event.button.y < YNAMEBOX + NBOXHEIGHT)
{
*nameread = 1;
}
else
{
*nameread = 0;
}
}
/* Function to start the game after pressing "OK"
param: the event of the mouse left button press
param: variable indicating the state of the game
param: name of the player
param: size of the board
param: the generated combination for the current game
param: variable indicating if the name read is on
param: Matrix with the selected pieces by the user for the current game
param: Array with the number of correct pieces in the right places per play
param: Array with the number of correct pieces in the wrong places per play
param: variable indicating how many plays have already been made
param: auxilar name that will be copied to the player name
param: position of the next letter in the player name
param: auxilar size of the board
param: initial time of the game
param: saved time of the game
param: actual time of the game
param: name of the player of the game with best number of plays
param: number of plays for the game with best number of plays
param: time for the game with best number of plays
param: size of the board for the game with best number of plays
param: name of the player of the game with best time
param: number of plays for the game with best time
param: time for the game with best time
param: size of the board for the game with best time
param: total number of games
param: total of the time of all games
param: total of plays for all games
param: total of the sum of the boardsize of all games
*/
void OKPress(SDL_Event event, int *gamestate, char *playername, int *boardsize, int **combination,
int *nameread, int game [][MAXBOARD], int blacks[], int whites[], int *nplays,
char *auxname, int *namepos, int auxsize, time_t *initialtime, time_t *savedtime,
time_t actualtime, char *bpplayername, int *bpplays, int *bptime, int *bpsize,
char *btplayername, int *btplays, int *bttime, int *btsize, int *ngames, int *totaltime,
int *totalplays, int *totalboard)
{
int i = 0;
int j = 0;
if(event.button.x > OKX && event.button.x < OKX + OKWIDTH && event.button.y > OKY &&
event.button.y < OKY + OKHEIGHT && auxsize >= MINLEVEL && auxsize <= MAXBOARD
&& auxname[0] != 0)
{
StatsUpdate(playername, *nplays, actualtime, *boardsize, blacks, bpplayername, bpplays, bptime, bpsize,
btplayername, btplays, bttime, btsize, ngames, totaltime, totalplays, totalboard);
for(i=0; i < MAXPLAYS; i++)
{
for(j=0; j < MAXBOARD; j++)
{
game[i][j] = 0;
}
}
for(i=0; i < MAXAVALARRS; i++)
{
blacks[i] = 0;
whites[i] = 0;
}
for(i=0; i < *namepos; i++)
{
playername[i] = auxname[i];
}
*nplays = 0;
*nameread = 0;
*namepos = 0;
*boardsize = auxsize;
*combination = Generate(*boardsize);
*gamestate = 1;
*initialtime = time(NULL);
*savedtime = 0;
NameCleanse(auxname);
}
}
/* Function that applys the game Timer
param: actual time
param: initial time
param; saved time
param: the renderer to handle all the rendering in the window
param: the number of plays of the current game
param: Array with the number of correct pieces in the right places per play
param: size of the board
param: state of the program (1 = current game)
param: the generated combination
param: Array of the color images
param: A font for the Win and Loss Functions
*/
void Timer(time_t *actualtime, time_t initialtime, time_t savedtime, SDL_Renderer * renderer,
int nplays, int blacks[], int boardsize, int gamestate, int *combination,
SDL_Surface **Colors, TTF_Font *font)
{
if(gamestate == 1 && Loss(renderer, nplays, blacks, boardsize, combination, Colors, font) != 1
&& Win(renderer, nplays, blacks, boardsize, combination, Colors, font) != 1)
{
*actualtime = time(NULL) - initialtime + savedtime;
}
}
/* Function that allocates memory for the player names
param: the name of the player
param: auxiliar name
param: name of the player with the best number of plays
param: name of the player with the best time
*/
void Memnames(char **playername, char **auxname, char **bpplayername, char **btplayername)
{
*playername = (char*)malloc(MAXNAME * sizeof(char));
if(*playername == NULL)
{
printf("ERROR! Failed to allocate memory for the name of the player");
}
*auxname = (char*)malloc(MAXNAME * sizeof(char));
if(*auxname == NULL)
{
printf("ERROR! Failed to allocate memory for the name of the player");
}
*bpplayername = (char*)malloc(MAXNAME * sizeof(char));
if(*bpplayername == NULL)
{
printf("ERROR! Failed to allocate memory for the name of the player");
}
*btplayername = (char*)malloc(MAXNAME * sizeof(char));
if(*btplayername == NULL)
{
printf("ERROR! Failed to allocate memory for the name of the player");
}
NameCleanse(*playername);
NameCleanse(*auxname);
NameCleanse(*bpplayername);
NameCleanse(*btplayername);
}
/* Function that updates the stats
param: the name of the player for the current game
param: number of plays for the current game
param: time for the current game
param: size of the board for the current game
param: Array with the number of correct pieces in the right places per play
param: name of the player for the game with less plays
param: number of plays for the game with less plays
param: time for the game with less plays
param: size of the board for the game with less plays
param: name of the player for the game with best time
param: number of plays for the game with best time
param: time of the game with best time
param: size of the board for the game with best time
param: total number of games
param: total of the time of all games
param: total of plays for all games
param: total of the sum of the boardsize of all games
*/
void StatsUpdate(char *playername, int nplays, time_t actualtime, int boardsize, int blacks[], char *bpplayername,
int *bpplays, int *bptime, int *bpsize, char *btplayername, int *btplays, int *bttime, int *btsize,
int *ngames, int *totaltime, int *totalplays, int *totalboard)
{
int i = 0;
if(*bpplays > nplays && blacks[nplays-1] == boardsize)
{
for(i=0; i < MAXNAME; i++)
{
bpplayername[i] = playername[i];
}
*bpplays = nplays;
*bptime = actualtime;
*bpsize = boardsize;
}
if(*bttime > actualtime && blacks[nplays-1] == boardsize)
{
for(i=0; i < MAXNAME; i++)
{
btplayername[i] = playername[i];
}
*btplays = nplays;
*bttime = actualtime;
*btsize = boardsize;
}
*ngames = *ngames + 1;
*totaltime = *totaltime + actualtime;
*totalplays = *totalplays + nplays;
*totalboard = *totalboard + boardsize;
}
/* Function that writes the updated stats to a file when the program is terminated
param: name of the player for the game with less plays
param: number of plays for the game with less plays
param: time for the game with less plays
param: size of the board for the game with less plays
param: name of the player for the game with best time
param: number of plays for the game with best time
param: time of the game with best time
param: size of the board for the game with best time
param: total number of games
param: total of the time of all games
param: total of plays for all games
param: total of the sum of the boardsize of all games
*/
void SaveStats(char *bpplayername, int bpplays, int bptime, int bpsize, char *btplayername, int btplays,
int bttime, int btsize, int ngames, int totaltime, int totalplays, int totalboard)
{
FILE *fp = NULL;
int i = 0;
int length = 0;
fp = fopen("stats.txt", "w");
if(fp == NULL)
{
printf("ERROR! It was not possible to open the stats file!\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "%d,%d,%d\n", bpplays, bptime, bpsize);
fprintf(fp, "%d,%d,%d\n", btplays,bttime, btsize);
fprintf(fp, "%d,%d,%d,%d\n", ngames, totaltime, totalplays, totalboard);
length = strlen(bpplayername);
fprintf(fp, "%d\n", length);
for(i=0; i < length; i++)
{
fprintf(fp, "%c", bpplayername[i]);
}
fprintf(fp, "\n");
length = strlen(btplayername);
fprintf(fp, "%d\n", length);
for(i=0; i < length; i++)
{
fprintf(fp, "%c", btplayername[i]);
}
fprintf(fp, "\n");
fclose(fp);
}
/* Function that writes the reads all the stats from a file when the program starts
param: name of the player for the game with less plays
param: number of plays for the game with less plays
param: time for the game with less plays
param: size of the board for the game with less plays
param: name of the player for the game with best time
param: number of plays for the game with best time
param: time of the game with best time
param: size of the board for the game with best time
param: total number of games
param: total of the time of all games
param: total of plays for all games
param: total of the sum of the boardsize of all games
*/
void ReadStats(char *bpplayername, int *bpplays, int *bptime, int *bpsize, char *btplayername, int *btplays,
int *bttime, int *btsize, int *ngames, int *totaltime, int *totalplays, int *totalboard)
{
FILE *fp = NULL;
char str[STRMAX] = {0};
int i = 0;
int length = 0;
fp = fopen("stats.txt", "r");
if(fp != NULL)
{
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d,%d,%d", bpplays, bptime, bpsize);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d,%d,%d", btplays, bttime, btsize);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d,%d,%d,%d", ngames, totaltime, totalplays, totalboard);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d", &length);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
for(i=0; i < length; i++)
{
bpplayername[i] = str[i];
}
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d", &length);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
for(i=0; i < length; i++)
{
btplayername[i] = str[i];
}
}
fclose(fp);
}
}
/* Function that saves the curren game for a file when the program is terminated
param: array with the generated combination
param: Matrix with the selected pieces by the user for the current game
param: variable indicating how many plays have already been made
param: size of the board, number of pieces per play
param: Array with the number of correct pieces in the right places per play
param: Array with the number of correct pieces in the wrong places per play
param: Name of the player for the current game
param: Time of the game when the program was terminated
*/
void SaveGame(int *combination, int game [][MAXBOARD], int nplays, int boardsize,
int blacks[], int whites[], char *playername, time_t actualtime)
{
FILE *fp = NULL;
int i = 0;
int j = 0;
fp = fopen("savedgame.txt", "w");
if(fp == NULL)
{
printf("ERROR! It was not possible to open the file to save the game\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "%ld\n", actualtime);
fprintf(fp, "%d\n", boardsize);
for(i=0; i < boardsize; i++)
{
fprintf(fp, "%d,", combination[i]);
}
fprintf(fp, "\n");
fprintf(fp, "%d\n", nplays);
for(i=0; i < nplays; i++)
{
fprintf(fp, "%d,", blacks[i]);
}
fprintf(fp, "\n");
for(i=0; i < nplays; i++)
{
fprintf(fp, "%d,", whites[i]);
}
fprintf(fp, "\n");
for(i=0; i < nplays; i++)
{
for(j=0; j < boardsize; j++)
{
fprintf(fp, "%d,", game[i][j]);
}
fprintf(fp, "\n");
}
fprintf(fp, "%s\n", playername);
fclose(fp);
}
/* Function that opens the saved game when the program starts
param: array with the generated combination
param: Matrix with the selected pieces by the user for the current game
param: variable indicating how many plays have already been made
param: size of the board, number of pieces per play
param: Array with the number of correct pieces in the right places per play
param: Array with the number of correct pieces in the wrong places per play
param: Name of the player for the current game
param: variable that will store the saved time
*/
void OpenGame(int *combination, int game[][MAXBOARD], int *nplays, int *boardsize,
int blacks[], int whites[], char *playername, time_t *savedtime)
{
FILE *fp = NULL;
char str[STRMAX] = {0};
char *token = NULL;
int auxnumber = 0;
int i = 0;
int j = 0;
fp = fopen("savedgame.txt", "r");
if(fp != NULL)
{
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%ld", savedtime);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d", boardsize);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
token = strtok(str, ",");
}
while (token != NULL && i < *boardsize)
{
sscanf(token, "%d", &auxnumber);
combination[i] = auxnumber;
i++;
token = strtok(NULL, ",");
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
sscanf(str, "%d", nplays);
}
i = 0;
fgets(str, STRMAX, fp);
if(str != NULL)
{
token = strtok(str, ",");
}
while (token != NULL && i < *nplays)
{
sscanf(token, "%d", &auxnumber);
blacks[i] = auxnumber;
i++;
token = strtok(NULL, ",");
}
i = 0;
fgets(str, STRMAX, fp);
if(str != NULL)
{
token = strtok(str, ",");
}
while (token != NULL && i < *nplays)
{
sscanf(token, "%d", &auxnumber);
whites[i] = auxnumber;
i++;
token = strtok(NULL, ",");
}
i = 0;
fgets(str, STRMAX, fp);
while(i < *nplays && str != NULL)
{
j = 0;
token = strtok(str, ",");
while(j < *boardsize && token != NULL)
{
sscanf(token, "%d", &auxnumber);
game[i][j] = auxnumber;
j++;
token = strtok(NULL, ",");
}
i++;
fgets(str, STRMAX, fp);
}
fgets(str, STRMAX, fp);
if(str != NULL)
{
strcpy(playername, str);
}
fclose(fp);
}
}