/
stats.c
193 lines (170 loc) · 4.71 KB
/
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
/*********************************
* *
* Écran des stats *
* *
*********************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "stats.h"
#include "jeu.h"
/* Renvoie 'a' le nombres de parties,
'b' le nombre de victoires,
'c' le nombre de victoires sans aide */
void read_stats (char data[3][6]) {
FILE *f_stats = fopen("DATA", "r");
if (f_stats == NULL) {
strcpy(data[0], "0");
strcpy(data[1], "0");
strcpy(data[2], "0");
reset_stats();
return;
}
int i = 0;
int l = 0;
char c = (char)fgetc(f_stats);
while (!feof(f_stats)) {
if (l < 5 && strchr("0123456789", (int)c)) {
data[i][l] = c;
data[i][l+1] = '\0';
l++;
} else if (i < 2 && c == ' ') {
i++;
l = 0;
} else if (!(i == 2 && c == '\n')) {
fclose(f_stats);
reset_stats();
strcpy(data[0], "0");
strcpy(data[1], "0");
strcpy(data[2], "0");
return;
}
c = (char)fgetc(f_stats);
}
fclose(f_stats);
}
void write_stats (char data[3][6]) {
FILE *f_stats = fopen("DATA", "w");
if (f_stats == NULL) {
exit(EXIT_FAILURE);
}
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; data[i][j] != '\0'; j++) {
fputc((int)data[i][j], f_stats);
}
if (i != 2) {
fputc(' ', f_stats);
}
}
fclose(f_stats);
}
void inc_stats (int a, int b, int c) {
char data[3][6];
read_stats(data);
sprintf(data[0], "%i", atoi(data[0])+a);
sprintf(data[1], "%i", atoi(data[1])+b);
sprintf(data[2], "%i", atoi(data[2])+c);
write_stats(data);
}
void reset_stats () {
FILE *f_stats = fopen("DATA", "w");
if (f_stats == NULL) {
exit(EXIT_FAILURE);
}
fwrite("0 0 0", 5, sizeof(char), f_stats);
fclose(f_stats);
}
void init_pos_s (SDL_Rect pos[3][5]) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 5; j++) {
pos[i][j].x = 360 + j*20;
pos[i][j].y = 147 + i*63;
}
}
}
int printStats (SDL_Surface *ecran) {
SDL_Surface *jeu = IMG_Load("img/fond.png");
SDL_Surface *nb0 = IMG_Load("img/nb0.png");
SDL_Surface *nb1 = IMG_Load("img/nb1.png");
SDL_Surface *nb2 = IMG_Load("img/nb2.png");
SDL_Surface *nb3 = IMG_Load("img/nb3.png");
SDL_Surface *nb4 = IMG_Load("img/nb4.png");
SDL_Surface *nb5 = IMG_Load("img/nb5.png");
SDL_Surface *nb6 = IMG_Load("img/nb6.png");
SDL_Surface *nb7 = IMG_Load("img/nb7.png");
SDL_Surface *nb8 = IMG_Load("img/nb8.png");
SDL_Surface *nb9 = IMG_Load("img/nb9.png");
SDL_Surface *none = IMG_Load("img/none.png");
SDL_Surface *stats = IMG_Load("img/stats.png");
SDL_Surface *menu = IMG_Load("img/menu2.png");
SDL_Surface *menus = IMG_Load("img/menu2s.png");
SDL_Surface *reset = IMG_Load("img/reset.png");
SDL_Surface *resets = IMG_Load("img/resets.png");
SDL_Surface *nb[10] = { nb0, nb1, nb2, nb3, nb4,
nb5, nb6, nb7, nb8, nb9};
SDL_Rect pos = {0, 0};
SDL_Rect pos_reset = {240, 440};
SDL_Rect pos_men = {440, 440};
SDL_Rect pos_stats = {100, 150};
SDL_Rect pos_s[3][5];
SDL_BlitSurface(jeu, NULL, ecran, &pos);
SDL_BlitSurface(menu, NULL, ecran, &pos_men);
SDL_BlitSurface(reset, NULL, ecran, &pos_reset);
SDL_BlitSurface(stats, NULL, ecran, &pos_stats);
int continuer = 1;
SDL_Event event;
int x_m, y_m;
int i, j;
char data[3][6];
init_pos_s (pos_s);
read_stats(data);
for (i = 0; i < 3; i++) {
for (j = 0; data[i][j] != '\0'; j++) {
SDL_BlitSurface(nb[data[i][j] - '0'], NULL, ecran, &pos_s[i][j]);
}
}
SDL_Flip(ecran);
while (continuer == 1) {
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT:
continuer = 0;
break;
case SDL_MOUSEMOTION:
x_m = event.motion.x;
y_m = event.motion.y;
if (is_over(x_m, y_m, *menu, pos_men)) {
SDL_BlitSurface(menus, NULL, ecran, &pos_men);
} else if (is_over(x_m, y_m, *reset, pos_reset)) {
SDL_BlitSurface(resets, NULL, ecran, &pos_reset);
} else {
SDL_BlitSurface(menu, NULL, ecran, &pos_men);
SDL_BlitSurface(reset, NULL, ecran, &pos_reset);
}
break;
case SDL_MOUSEBUTTONUP:
x_m = event.button.x;
y_m = event.button.y;
if (is_over(x_m, y_m, *menu, pos_men)) {
continuer = 2;
} else if (is_over(x_m, y_m, *reset, pos_reset)) {
reset_stats(data);
for (i = 0; i < 3; i++) {
for (j = 0; data[i][j] != '\0'; j++) {
SDL_BlitSurface(none, NULL, ecran, &pos_s[i][j]);
}
SDL_BlitSurface(nb[0], NULL, ecran, &pos_s[i][0]);
}
}
break;
default:
break;
}
SDL_Flip(ecran);
}
return continuer;
}