This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menus.c
executable file
·356 lines (306 loc) · 12.9 KB
/
menus.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
#include <stdio.h>
#include <stdlib.h>
#include "save.h"
#include "grids.h"
void customize_fleet(int *ship_count, char ***ships, int **sizes, char **signs, int lines, int columns)
{
int option;
do
{
printf("\n###### Your fleet ######\n\n");
printf("Number of ships: %d\n", *ship_count);
for (int i = 0; i < *ship_count; ++i)
printf("#%d. %s (%c): %d squares\n", i + 1, (*ships)[i], (*signs)[i], (*sizes)[i]);
option = 0;
do
{
printf("\nSelect an option among:\n1. Add a ship to your fleet\n2. Change the name of a ship\n3. Change the size of a ship\n4. Delete a ship from your fleet\n5. Go back to the settings\n");
int n = scanf("%d", &option);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (option < 1 || option > 5)
printf("Please select an option between 1 and 5.\n");
} while (option < 1 || option > 5);
switch (option)
{
case 1:
{
*ships = (char **) realloc(*ships, sizeof(char *) * (*ship_count + 1));
*sizes = (int *) realloc(*sizes, sizeof(int) * (*ship_count + 1));
*signs = (char *) realloc(*signs, sizeof(char) * (*ship_count + 1));
(*ships)[*ship_count] = (char *) malloc(sizeof(char) * 100);
printf("What should be the name of your new ship? ");
scanf("%s", (*ships)[*ship_count]);
(*sizes)[*ship_count] = 0;
do
{
printf("What should be its size? ");
int n = scanf("%d", &(*sizes)[*ship_count]);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if ((*sizes)[*ship_count] < 1 || ((*sizes)[*ship_count] > lines && (*sizes)[*ship_count] > columns))
printf("This size is invalid, please select one between 1 and %d.\n", lines >= columns? lines : columns);
} while ((*sizes)[*ship_count] < 1 || ((*sizes)[*ship_count] > lines && (*sizes)[*ship_count] > columns));
scanf("%c", &(*signs)[*ship_count]); // This scanf always returns \n so we use this line to discard it
(*signs)[*ship_count] = '\0';
do
{
printf("What should be its symbol? ");
int n = scanf("%c", &(*signs)[*ship_count]);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter a valid symbol, different from O and X.\n");
}
} while ((*signs)[*ship_count] == '\0' || (*signs)[*ship_count] == 'O' || (*signs)[*ship_count] == 'X');
// TODO: Check that this symbol is not already used
// TODO: Check that all the ships can fit on the grid
++(*ship_count);
}
break;
case 2:
{
int i = 0;
do
{
printf("Which ship's name do you want to change? ");
int n = scanf("%d", &i);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (i < 1 || i > *ship_count)
printf("This boat doesn't exist, please select one between between 1 and %d.\n", *ship_count);
} while (i < 1 || i > *ship_count);
--i;
printf("What should be its new name? ");
scanf("%s", (*ships)[i]);
scanf("%c", &(*signs)[i]); // This scanf always returns \n so we use this line to discard it
(*signs)[i] = '\0';
do
{
printf("What should be its new symbol? ");
int n = scanf("%c", &(*signs)[i]);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter a valid symbol, different from O and X.\n");
}
} while ((*signs)[i] == '\0' || (*signs)[i] == 'O' || (*signs)[i] == 'X');
// TODO: Check that this symbol is not already used
}
break;
case 3:
{
int i = 0;
do
{
printf("Which ship's size do you want to change? ");
int n = scanf("%d", &i);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (i < 1 || i > *ship_count)
printf("This boat doesn't exist, please select one between between 1 and %d.\n", *ship_count);
} while (i < 1 || i > *ship_count);
--i;
(*sizes)[i] = 0;
do
{
printf("What should be its size? ");
int n = scanf("%d", &(*sizes)[i]);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if ((*sizes)[i] < 1 || ((*sizes)[i] > lines && (*sizes)[i] > columns))
printf("This size is invalid, please select one between 1 and %d.\n", lines >= columns? lines : columns);
} while ((*sizes)[i] < 1 || ((*sizes)[i] > lines && (*sizes)[i] > columns));
// TODO: Check that all the ships can fit on the grid
}
break;
case 4:
{
int i = 0;
do
{
printf("Which ship do you want to delete? ");
int n = scanf("%d", &i);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (i < 1 || i > *ship_count)
printf("This boat doesn't exist, please select one between between 1 and %d.\n", *ship_count);
} while (i < 1 || i > *ship_count);
--i;
free((*ships)[i]);
--(*ship_count);
for (; i < *ship_count; ++i)
{
(*ships)[i] = (*ships)[i + 1];
(*sizes)[i] = (*sizes)[i + 1];
(*signs)[i] = (*signs)[i + 1];
}
*ships = (char **) realloc(*ships, sizeof(char *) * *ship_count);
*sizes = (int *) realloc(*sizes, sizeof(int) * *ship_count);
*signs = (char *) realloc(*signs, sizeof(char) * *ship_count);
}
break;
}
} while (option != 5);
}
void settings(int *ship_count, char ***ships, int **sizes, char **signs, char ***pgrid, char ***cgrid, int *lines, int *columns)
{
int option;
do
{
printf("\n###### Settings ######\n\n");
printf("Size of the grid: %d x %d\nNumber of ships: %d\n", *lines, *columns, *ship_count);
option = 0;
do
{
printf("\nSelect an option among:\n1. Customize your fleet\n2. Change the size of the grid\n3. Go back to the menu\n");
int n = scanf("%d", &option);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (option < 1 || option > 3)
printf("Please select an option between 1 and 3.\n");
} while (option < 1 || option > 3);
switch (option)
{
case 1:
customize_fleet(ship_count, ships, sizes, signs, *lines, *columns);
break;
case 2:
{
int previous_lines = *lines, previous_columns = *columns;
*lines = 0;
do
{
printf("Give the number of lines: ");
int n = scanf("%d", lines);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (*lines < 1 || *lines > 99) // We limit at 99 lines not to have to handle numbers with 3 digits
printf("The number of lines must be between 1 and 99.\n");
} while (*lines < 1 || *lines > 99);
*columns = 0;
do
{
printf("Give the number of columns: ");
int n = scanf("%d", columns);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (*columns < 1 || *columns > 26) // We limit at 26 columns not to have to handle when there are more columns than letters in the alphabet
printf("The number of columns must be between 1 and 26.\n");
} while (*columns < 1 || *columns > 26);
// TODO: Check that all the ships can fit on the grid
/*
for (int i = *lines; i < previous_lines; ++i)
{
free((*pgrid)[i]);
free((*cgrid)[i]);
}
*pgrid = (char **) realloc(*pgrid, sizeof(char) * *lines);
*cgrid = (char **) realloc(*cgrid, sizeof(char) * *lines);
for (int i = 0; i < *lines && i < previous_lines; ++i)
{
(*pgrid)[i] = (char *) realloc((*pgrid)[i], sizeof(char) * *columns);
(*cgrid)[i] = (char *) realloc((*cgrid)[i], sizeof(char) * *columns);
for (int j = previous_columns; j < *columns; ++j)
{
(*pgrid)[i][j] = ' ';
(*cgrid)[i][j] = ' ';
}
}
for (int i = previous_lines; i < *lines; ++i)
{
(*pgrid)[i] = (char *) malloc(sizeof(char) * *columns);
(*cgrid)[i] = (char *) malloc(sizeof(char) * *columns);
for (int j = 0; j < *columns; ++j)
{
(*pgrid)[i][j] = ' ';
(*cgrid)[i][j] = ' ';
}
}
*/
free_grids(*pgrid, *cgrid, previous_lines, previous_columns);
create_grids(pgrid, cgrid, *lines, *columns);
}
break;
}
} while (option != 3);
}
int menu(int *ship_count, char ***ships, int **sizes, char **signs, char ***pgrid, char ***cgrid, int *lines, int *columns, int *turn, bool *shooting, int *X_shooting, int *Y_shooting)
{
int option;
do
{
printf("\n###### Main menu ######\n");
option = 0;
do
{
printf("\nSelect an option among:\n1. Play the game\n2. Open the settings\n3. Load the previous save\n4. Quit the game\n");
int n = scanf("%d", &option);
if (n < 1)
{
char null[100];
scanf("%s", null);
printf("Please enter an integer.\n");
}
else if (option < 1 || option > 4)
printf("Please select an option between 1 and 4.\n");
} while (option < 1 || option > 4);
switch (option)
{
case 2:
settings(ship_count, ships, sizes, signs, pgrid, cgrid, lines, columns);
break;
case 3:
if (!load_game(ship_count, ships, sizes, signs, pgrid, cgrid, lines, columns, turn, shooting, X_shooting, Y_shooting))
{
printf("Failed to load the previous save. None exists or it was altered since then.\n");
option = 0;
}
break;
}
} while (option != 1 && option != 3 && option != 4);
if (option == 1)
return 1;
else if (option == 3)
return 2;
else
return 0;
}