-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
299 lines (267 loc) · 8.24 KB
/
gui.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
#include <gui.h>
/**
* @brief Print the grid
*
* @param size int - size of the grid
* @param grid char** - pointer to the grid
*/
void print_grid(int size, char** grid) {
#ifdef _WIN32
for (int i = 0; i < size; i++) {
for (int k = 0; k < size; k++) {
printf("%c%c%c%c", k==0 ? i==0 ? 201 : 204 : i==0 ? 203 : 206, 205, 205, 205);
}
printf("%c\n%c", i==0 ? 187 : 185, 186);
for (int j = 0; j < size; j++) {
printf( " %c %c", grid[i][j], 186);
}
printf("\n");
}
for (int i = 0; i < size; i++) {
printf("%c%c%c%c", i==0 ? 200 : 202, 205, 205, 205);
}
printf("%c\n", 188);
#else
for (int i = 0; i < size; i++) {
for (int k = 0; k < size; k++) {
printf("+---");
}
printf("+\n|");
for (int j = 0; j < size; j++) {
printf(" %c |", grid[i][j]);
}
printf("\n");
}
for (int i = 0; i < size; i++) {
printf("+---");
}
printf("+\n\n");
#endif
}
/**
* @brief Print a player in a stream
*
* @param stream FILE* - The stream to print to
* @param player Player - The player to print.
*/
void printPlayer(FILE* stream, Player player) {
fprintf(stream, "%s\t%.2f\t%d\t%d\n", player.pseudo, player.score, player.sizegrid, player.timeplayed);
}
/**
* @brief Print the playerlist in a stream.
*
* @param stream FILE* - The stream to print to.
* @param playerlist Player* - The list of the players.
* @param size int - The size of the playerlist.
*/
void printPlayerlist(FILE* stream, Player* playerlist, int size) {
for (int i = 0; i < size; i++) {
printPlayer(stream, playerlist[i]);
}
}
/**
* @brief Get an integer from the user
*
* @param message char* - the message to print to the user.
* @param min int - the minimum value of the integer.
* @param max int - the maximum value of the integer.
* @return int - the integer entered by the user.
*/
int get_integer_input(const char* message, int min, int max) {
char inputstring[MAX_CHAR_ARRAY_LENGTH];
int tmp, input=0;
do {
printf("%s", message);
scanf("%s", inputstring);
fflush(stdin);
tmp = atoi(inputstring);
if (tmp >= min && tmp <= max) {
input = tmp;
}
} while (input == 0);
return input;
}
/**
* @brief Get a string from the user
*
* @param message char* - the message to print to the user.
* @return Word - the word entered by the user.
*/
Word get_string_input(const char* message) {
printf("%s\n", message);
char temp[MAX_CHAR_ARRAY_LENGTH];
scanf("%s", temp);
fflush(stdin);
int k = strlen(temp);
char* str = (char*) malloc(sizeof(char)*k);
Word word = (Word) {k, str};
strncpy(word.str, temp, k+1);
return word;
}
/**
* @brief Get an input, just a validation to the next step
*
* @param message char* - the message to print to the user
*/
void validate(const char* message) {
printf("%s\n", message);
#ifdef _WIN32
getchar();
#else
FILE* f = fopen("/dev/tty", "r"); // done by M. Gaud
if (f != NULL) {
int c = fgetc(f);
(void) c;
}
#endif
}
/**
* @brief Prints the logo 'BOGGLE' to stdin.
*
*/
void print_logo() {
printf("\n ____ ____ ______________ ______ \n");
printf(" / __ )/ __ \\/ ____/ ____/ / / ____/ \n");
printf(" / __ / / / / / __/ / __/ / / __/ \n");
printf(" / /_/ / /_/ / /_/ / /_/ / /___/ /___ \n");
printf("/_____/\\____/\\____/\\____/_____/_____/\n\n");
}
/**
* @brief Clear stdin.
*
*/
void clear() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
/**
* @brief Wait some time.
*
* @param seconds int - the number of seconds to wait
*/
void wait(int seconds) {
double dtime = time(0);
while (difftime(time(0), dtime) < seconds) {
//wait
}
}
/**
* @brief Start a game.
*
* @return Player - The player who played.
*/
Player play() {
Player player;
int yes=1;
do {
Word pseudo = get_string_input("Choisissez un pseudo : ");
int n, i = 0;
Player* gamelist = (Player*) malloc(sizeof(Player));
read_games("scores.txt", &n, &gamelist);
while (i <= n && strcmp(gamelist[i].pseudo, pseudo.str) != 0) {
i++;
}
if (i <= n) {
printf("Le pseudo %s %c d%cja %ct%c utilis%c.\n", pseudo.str, ACCENT_E, ACCENT_A, ACCENT_E, ACCENT_E, ACCENT_E);
printf("Vous ne pouvez pas le r%cutiliser.\n", ACCENT_E);
yes = 0;
} else {
strcpy(player.pseudo, pseudo.str);
yes = 1;
}
free(gamelist);
freeWord(&pseudo);
} while (yes == 0); // we ask for an unused nickname, if not, we ask again
printf("Vous allez jouer en tant que : %s\n", player.pseudo);
int size = get_integer_input("Choisissez la taille de votre grille (4-8): ", 4, 8);
player.sizegrid = size;
char** grid = create_grid(size);
grid = fill_grid_algo(size, grid);
double playtime = (double) get_integer_input("Choisissez le temps de jeu (60s-180s): ", 60, 180);
player.timeplayed = playtime;
Word* words = (Word*) malloc(sizeof(Word)); // array of Word -> will be realloc'ed
if (words == NULL) {
fprintf(stderr, "ERROR: Could not allocate memory for words\n");
return player;
}
int wordsize = 0; // size of the array of words
int iter = 1; // number of iterations
double dtime = 0; // time passed since the beginning of the game
clear();
print_logo();
print_grid(size, grid);
time_t debut = time(0);
GrpWords* grpwords = read_dico();
do {
Word word = get_string_input("Entrez un mot: ");
if (search2D(size, grid, word)) {
// the word is in the grid
if (valid_word(word, grpwords)) {
// the word is also in the dictionary
Boolean found = FALSE;
int i = 0;
while (i < wordsize && !found) {
if (strcmp(words[i].str, word.str) == 0) {
found = TRUE;
}
i++;
}
if (!found) {
// this word has not been found yet
printf("Le mot %s est dans la grille\n", word.str);
words[wordsize].length = word.length;
words[wordsize].str = malloc(sizeof(char)*(word.length+1));
strncpy(words[wordsize].str, word.str, word.length+1); // maybe use memcpy ?
wordsize++;
words = realloc(words, sizeof(Word)*(wordsize+1));
} else {
// word already found
printf("Vous avez d%cja trouv%c le mot %s\n", ACCENT_E, ACCENT_E, word.str);
}
} else {
printf("Le mot %s n'est pas dans le dictionnaire\n", word.str);
}
} else {
printf("Le mot %s n'est pas dans la grille\n", word.str);
}
freeWord(&word);
if (iter % 2 == 0){
wait(1);
clear();
print_logo();
print_grid(size, grid);
for (int i = 0; i < wordsize; i++) {
printf("%s ", words[i].str);
}
printf("\n");
}
iter++;
dtime = difftime(time(0), debut);
printf("Temps %ccoul%c : %.0fs sur %.0fs\n", ACCENT_E, ACCENT_E, dtime, playtime);
} while (dtime < playtime);
printf("%d mots trouv%cs:\n", wordsize, ACCENT_E);
printf("Mot : Taille\n");
for (int i = 0; i < wordsize; i++) {
printf("%s : %d\n", words[i].str, words[i].length);
}
printf("\n");
player.score = score(wordsize, words);
printf("C'est fini, votre score est de %.2f\n", player.score);
validate("Appuyez sur une touche pour continuer");
save_game(player, "scores.txt");
free_grid(size, grid);
for (int i = 0; i < wordsize; i++) {
freeWord(&words[i]);
}
free(words);
words = NULL;
for (int i = 0; i < NB_LETTER; i++) {
freeGrpWords(&(grpwords[i]));
}
free(grpwords);
grpwords = NULL;
return player;
}