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
/
menu.c
362 lines (302 loc) · 14.1 KB
/
menu.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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "forest.h"
#include "pathfinding.h"
#include "menu.h"
void edit_cell(struct Forest forest) {
int x, y, n;
do {
printf("Which cell do you want to edit? (X;Y, e.g. 2;1, type Q to cancel) ");
n = scanf("%d;%d", &x, &y);
if (n < 2) {
char tmp[100];
scanf("%s", tmp);
if (tmp[0] == 'Q' || tmp[0] == 'q') return;
printf("Please enter valid coordinates.\n");
} else if (x <= 0 || y <= 0 || x > forest.width || y > forest.length)
printf("Please enter coordinates inside the forest.\n");
} while (n < 2 || x <= 0 || y <= 0 || x > forest.width || y > forest.length);
printf("The current state of this cell is:\n");
printf("Type: %c\n", forest.cells[y - 1][x - 1]->type.symbol);
printf("Degree: %d / %d\n", forest.cells[y - 1][x - 1]->degree, forest.cells[y - 1][x - 1]->type.degree);
printf("State: %s\n", forest.cells[y - 1][x - 1]->state? "On fire" : forest.cells[y - 1][x - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL? "Extinguished" : "Normal");
char type;
do {
printf("Enter the new type of cell (use _ for space, type Q to cancel): ");
do {
scanf("%c", &type);
} while (type == '\n' || type == ' ');
for (int i = 0; i < TYPE_COUNT; ++i) {
if (type == TYPES[i].symbol || (type == '_' && TYPES[i].symbol == ' ')) {
type = '\0';
struct Cell *next = (struct Cell *) malloc(sizeof(struct Cell));
next->previous = forest.cells[y - 1][x - 1];
next->type = TYPES[i];
if (TYPES[i].degree > 0) {
do {
printf("Enter the new degree of this cell (maximum is %d): ", TYPES[i].degree);
n = scanf("%d", &next->degree);
if (n < 1) {
char tmp[100];
scanf("%s", tmp);
printf("Please enter a valid degree.\n");
} else if (next->degree < 0 || next->degree > TYPES[i].degree)
printf("Please enter a degree between 0 and %d.\n", TYPES[i].degree);
} while (n < 1 || next->degree < 0 || next->degree > TYPES[i].degree);
} else next->degree = 0;
if (TYPES[i].symbol != GROUND_SYMBOL && TYPES[i].symbol != WATER_SYMBOL && TYPES[i].symbol != EXTINGUISHED_ASHES_SYMBOL) {
do {
printf("Enter the new state of this cell (0 or 1): ");
n = scanf("%d", &next->state);
if (n < 1) {
char tmp[100];
scanf("%s", tmp);
printf("Please enter a valid state.\n");
} else if (next->state != 0 && next->state != 1)
printf("Please enter a state between 0 (normal/extinguished) and 1 (on fire).\n");
} while (n < 1 || (next->state != 0 && next->state != 1));
} else next->state = 0;
for (int y2 = 0; y2 < forest.length; ++y2) {
for (int x2 = 0; x2 < forest.width; ++x2) {
if (x2 != x - 1 || y2 != y - 1) {
struct Cell *cell = (struct Cell *) malloc(sizeof(struct Cell));
cell->previous = forest.cells[y2][x2];
cell->type = forest.cells[y2][x2]->type;
cell->degree = forest.cells[y2][x2]->degree;
cell->state = forest.cells[y2][x2]->state;
forest.cells[y2][x2] = cell;
} else forest.cells[y - 1][x - 1] = next;
}
}
break;
}
}
if (type != '\0' && type != 'Q' && type != 'q')
printf("Please enter a valid type.\n");
} while (type != '\0' && type != 'Q' && type != 'q');
}
void run_simulation(struct Forest *forest) {
bool fire = false;
for (int y = 0; y < forest->length && !fire; ++y)
for (int x = 0; x < forest->width && !fire; ++x)
if (forest->cells[y][x]->state) fire = true;
if (!fire) {
int x, y, n;
do {
printf("No cell is on fire, enter the cell to light up (X;Y, e.g. 2;1, type Q to cancel): ");
n = scanf("%d;%d", &x, &y);
if (n < 2) {
char tmp[100];
scanf("%s", tmp);
if (tmp[0] == 'Q' || tmp[0] == 'q') return;
printf("Please enter valid coordinates.\n");
} else if (x <= 0 || y <= 0 || x > forest->width || y > forest->length)
printf("Please enter coordinates inside the forest.\n");
else if (forest->cells[y - 1][x - 1]->type.symbol == GROUND_SYMBOL || forest->cells[y - 1][x - 1]->type.symbol == WATER_SYMBOL || forest->cells[y - 1][x - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL)
printf("Please enter the coordinates of a flammable cell.\n");
} while (n < 2 || x <= 0 || y <= 0 || x > forest->width || y > forest->length || forest->cells[y - 1][x - 1]->type.symbol == GROUND_SYMBOL || forest->cells[y - 1][x - 1]->type.symbol == WATER_SYMBOL || forest->cells[y - 1][x - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL);
for (int y2 = 0; y2 < forest->length; ++y2) {
for (int x2 = 0; x2 < forest->width; ++x2) {
struct Cell *cell = (struct Cell *) malloc(sizeof(struct Cell));
cell->previous = forest->cells[y2][x2];
cell->type = forest->cells[y2][x2]->type;
cell->degree = forest->cells[y2][x2]->degree;
cell->state = x2 != x - 1 || y2 != y - 1? forest->cells[y2][x2]->state : 1;
forest->cells[y2][x2] = cell;
}
}
}
int iterations, n;
do {
printf("Enter the number of iterations to perform (enter -1 to run the simulation until the end): ");
n = scanf("%d", &iterations);
if (n < 1) {
char tmp[100];
scanf("%s", tmp);
printf("Please enter a valid number of iterations.\n");
} else if (iterations < -1)
printf("Please enter a positive number of iterations or -1 to run the simulation until the end.\n");
} while (n < 1 || iterations < -1);
if (iterations >= 0) {
for (int i = 1; i <= iterations; ++i) {
struct Forest previous_forest = *forest;
*forest = iterate_forest(previous_forest);
free_forest(previous_forest);
system("clear");
printf("Iteration #%d / %d:\n", i, iterations);
display_forest(*forest);
nanosleep((const struct timespec[]) {{0, 50000000L}}, NULL);
}
} else {
int i = 1;
struct Forest previous_forest = *forest;
*forest = iterate_forest(*forest);
while (!compare_forests(*forest, previous_forest)) {
system("clear");
printf("Iteration #%d:\n", i++);
display_forest(*forest);
nanosleep((const struct timespec[]) {{0, 50000000L}}, NULL);
free_forest(previous_forest);
previous_forest = *forest;
*forest = iterate_forest(*forest);
}
free_forest(*forest);
*forest = previous_forest;
printf("\nSimulation ended!\n");
}
}
void rollback_simulation(struct Forest forest) {
int iterations, n;
do {
printf("Enter the number of iterations to rollback (enter -1 to return the simulation to its original state): ");
n = scanf("%d", &iterations);
if (n < 1) {
char tmp[100];
scanf("%s", tmp);
printf("Please enter a valid number of iterations.\n");
} else if (iterations < -1)
printf("Please enter a positive number of iterations or -1 to return the simulation to its original state.\n");
} while (n < 1 || iterations < -1);
if (iterations >= 0) {
for (int i = 1; i <= iterations; ++i) {
rollback_forest(forest);
system("clear");
printf("Rollback #%d / %d:\n", i, iterations);
display_forest(forest);
nanosleep((const struct timespec[]) {{0, 50000000L}}, NULL);
}
} else {
int i = 1;
while (rollback_forest(forest)) {
system("clear");
printf("Rollback #%d:\n", i++);
display_forest(forest);
nanosleep((const struct timespec[]) {{0, 50000000L}}, NULL);
}
printf("\nSimulation restored to its original state.\n");
}
}
void find_path(struct Forest forest) {
int xA, yA, n;
do {
printf("Enter the coordinates of the starting cell (X;Y, e.g. 2;1, type Q to cancel): ");
n = scanf("%d;%d", &xA, &yA);
if (n < 2) {
char tmp[100];
scanf("%s", tmp);
if (tmp[0] == 'Q' || tmp[0] == 'q') return;
printf("Please enter valid coordinates.\n");
} else if (xA <= 0 || yA <= 0 || xA > forest.width || yA > forest.length)
printf("Please enter coordinates inside the forest.\n");
else if (forest.cells[yA - 1][xA - 1]->type.symbol == GROUND_SYMBOL || forest.cells[yA - 1][xA - 1]->type.symbol == WATER_SYMBOL || forest.cells[yA - 1][xA - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL)
printf("Please enter the coordinates of a flammable cell.\n");
} while (n < 2 || xA <= 0 || yA <= 0 || xA > forest.width || yA > forest.length || forest.cells[yA - 1][xA - 1]->type.symbol == GROUND_SYMBOL || forest.cells[yA - 1][xA - 1]->type.symbol == WATER_SYMBOL || forest.cells[yA - 1][xA - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL);
int xB, yB;
do {
printf("Enter the coordinates of the ending cell (X;Y, e.g. 1;2, type Q to cancel): ");
n = scanf("%d;%d", &xB, &yB);
if (n < 2) {
char tmp[100];
scanf("%s", tmp);
if (tmp[0] == 'Q' || tmp[0] == 'q') return;
printf("Please enter valid coordinates.\n");
} else if (xB <= 0 || yB <= 0 || xB > forest.width || yB > forest.length)
printf("Please enter coordinates inside the forest.\n");
else if (forest.cells[yB - 1][xB - 1]->type.symbol == GROUND_SYMBOL || forest.cells[yB - 1][xB - 1]->type.symbol == WATER_SYMBOL || forest.cells[yB - 1][xB - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL)
printf("Please enter the coordinates of a flammable cell.\n");
} while (n < 2 || xB <= 0 || yB <= 0 || xB > forest.width || yB > forest.length || forest.cells[yB - 1][xB - 1]->type.symbol == GROUND_SYMBOL || forest.cells[yB - 1][xB - 1]->type.symbol == WATER_SYMBOL || forest.cells[yB - 1][xB - 1]->type.symbol == EXTINGUISHED_ASHES_SYMBOL);
struct Coords start = { xA - 1, yA - 1 }, goal = { xB - 1, yB - 1 };
struct Coords *path = a_star(forest, start, goal, &n);
if (path != NULL) {
struct Forest copy;
copy.length = forest.length;
copy.width = forest.width;
copy.cells = (struct Cell ***) malloc(sizeof(struct Cell **) * forest.length);
for (int y = 0; y < forest.length; ++y) {
copy.cells[y] = (struct Cell **) malloc(sizeof(struct Cell *) * forest.width);
for (int x = 0; x < forest.width; ++x) {
copy.cells[y][x] = (struct Cell *) malloc(sizeof(struct Cell));
copy.cells[y][x]->previous = NULL;
copy.cells[y][x]->type = forest.cells[y][x]->type;
copy.cells[y][x]->degree = forest.cells[y][x]->degree;
copy.cells[y][x]->state = 0;
}
}
for (int i = 0; i < n; ++i) {
copy.cells[path[i].y][path[i].x]->state = 1;
system("clear");
printf("Here is the shortest path between %d;%d and %d;%d (%d iterations):\n", xA, yA, xB, yB, n);
display_forest(copy);
nanosleep((const struct timespec[]) {{0, 50000000L}}, NULL);
}
free_forest(copy);
free(path);
} else {
printf("No path could be found between these two points.\n");
}
}
void menu(struct Forest forest) {
int option, n;
do {
printf("Select an option among:\n");
printf("1. Display the current state of the forest\n");
printf("2. Randomly fill the forest\n");
printf("3. Fill the forest manually\n");
printf("4. Change a cell's type, degree or state\n");
printf("5. Simulate a fire for a number of iterations\n");
printf("6. Rollback the simulation by a number of iterations\n");
printf("7. Find the path between two points fire can spread on\n");
printf("0. Leave the simulation\n");
n = scanf("%d", &option);
if (n < 1) {
char tmp[100];
scanf("%s", tmp);
printf("Please enter a valid option.\n");
} else if (option < 0 || option > 7)
printf("Please enter an option between 0 and 7.\n");
} while (n < 1 || option < 0 || option > 7);
system("clear");
switch(option) {
case 1:
printf("Here is the current forest:\n");
display_forest(forest);
printf("\n");
break;
case 2:
randomize_forest(forest);
printf("Here is the randomly generated forest:\n");
display_forest(forest);
printf("\n");
break;
case 3:
input_forest(forest);
system("clear");
printf("Here is the forest:\n");
display_forest(forest);
printf("\n");
break;
case 4:
edit_cell(forest);
system("clear");
printf("Here is the new forest:\n");
display_forest(forest);
printf("\n");
break;
case 5:
run_simulation(&forest);
printf("\n");
break;
case 6:
rollback_simulation(forest);
printf("\n");
break;
case 7:
find_path(forest);
printf("\n");
break;
case 0:
return;
}
menu(forest);
}