-
Notifications
You must be signed in to change notification settings - Fork 2
/
maze.c
393 lines (379 loc) · 11.1 KB
/
maze.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
// maze.c
// functions for maze
#include <stdbool.h>
#include <stdlib.h>
#include "maze.h"
#include "direction.h"
#include "decide.h"
#include "main.h"
#include "cell.h"
#include "mouse.h"
#include "walls.h"
// initalize maze
void init_maze(Maze * maze) {
int x;
int y;
if(maze != NULL) {
// init all cells
for (x = 0 ; x < 17 ; x++) {
for (y = 0 ; y < 17 ; y++) {
init_cell(&(maze->cells[x][y]),x,y);
}
}
// add west walls for x = 0, 16
x = 0;
for (y = 0 ; y < 17 ; y++) {
set_w_wall(&(maze->cells[x][y]));
}
x = 16;
for (y = 0 ; y < 17 ; y++) {
set_w_wall(&(maze->cells[x][y]));
}
// add south walls for y = 0, 16
y = 0;
for (x = 0 ; x < 17 ; x++) {
set_s_wall(&(maze->cells[x][y]));
}
y = 16;
for (x = 0 ; x < 17 ; x++) {
set_s_wall(&(maze->cells[x][y]));
}
}
}
// functions for front wall
void set_f_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; set south of y+1
case N:
set_s_wall(&(maze->cells[x][y+1]));
break;
// front is south; set south of x,y
case S:
set_s_wall(&(maze->cells[x][y]));
break;
// front is east; set west of x+1
case E:
set_w_wall(&(maze->cells[x+1][y]));
break;
// front is west; set west of x,y
case W:
set_w_wall(&(maze->cells[x][y]));
break;
default: break;
}
}
}
void del_f_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; delete south of y+1
case N:
del_s_wall(&(maze->cells[x][y+1]));
break;
// front is south; delete south of x,y
case S:
del_s_wall(&(maze->cells[x][y]));
break;
// front is east; delete west of x+1
case E:
del_w_wall(&(maze->cells[x+1][y]));
break;
// front is west; delete west of x,y
case W:
del_w_wall(&(maze->cells[x][y]));
break;
default: break;
}
}
}
bool has_f_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; get south of y+1
case N:
return has_s_wall(&(maze->cells[x][y+1]));
break;
// front is south; get south of x,y
case S:
return has_s_wall(&(maze->cells[x][y]));
break;
// front is east; get west of x+1
case E:
return has_w_wall(&(maze->cells[x+1][y]));
break;
// front is west; get west of x,y
case W:
return has_w_wall(&(maze->cells[x][y]));
break;
default: return false;
}
}
else return true;
}
// functions for left wall
void set_l_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; set west of x,y
case N:
set_w_wall(&(maze->cells[x][y]));
break;
// front is south; set west of x+1
case S:
set_w_wall(&(maze->cells[x+1][y]));
break;
// front is east; set south of y+1
case E:
set_s_wall(&(maze->cells[x][y+1]));
break;
// front is west; set south of x,y
case W:
set_s_wall(&(maze->cells[x][y]));
break;
default: break;
}
}
}
void del_l_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; del west of x,y
case N:
del_w_wall(&(maze->cells[x][y]));
break;
// front is south; del west of x+1
case S:
del_w_wall(&(maze->cells[x+1][y]));
break;
// front is east; del south of y+1
case E:
del_s_wall(&(maze->cells[x][y+1]));
break;
// front is west; del south of x,y
case W:
del_s_wall(&(maze->cells[x][y]));
break;
default: break;
}
}
}
bool has_l_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; get west of x,y
case N:
return has_w_wall(&(maze->cells[x][y]));
break;
// front is south; get west of x+1
case S:
return has_w_wall(&(maze->cells[x+1][y]));
break;
// front is east; get south of y+1
case E:
return has_s_wall(&(maze->cells[x][y+1]));
break;
// front is west; get south of x,y
case W:
return has_s_wall(&(maze->cells[x][y]));
break;
default: return false;
}
}
else return true;
}
// functions for right wall
void set_r_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; set west of x+1,y
case N:
set_w_wall(&(maze->cells[x+1][y]));
break;
// front is south; set west of x,y
case S:
set_w_wall(&(maze->cells[x][y]));
break;
// front is east; set south of x,y
case E:
set_s_wall(&(maze->cells[x][y]));
break;
// front is west; set south of x,y+1
case W:
set_s_wall(&(maze->cells[x][y+1]));
break;
default: break;
}
}
}
void del_r_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; del west of x+1,y
case N:
del_w_wall(&(maze->cells[x+1][y]));
break;
// front is south; del west of x,y
case S:
del_w_wall(&(maze->cells[x][y]));
break;
// front is east; del south of x,y
case E:
del_s_wall(&(maze->cells[x][y]));
break;
// front is west; del south of x,y+1
case W:
del_s_wall(&(maze->cells[x][y+1]));
break;
default: break;
}
}
}
bool has_r_wall(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// front is north; get west of x+1,y
case N:
return has_w_wall(&(maze->cells[x+1][y]));
break;
// front is south; get west of x,y
case S:
return has_w_wall(&(maze->cells[x][y]));
break;
// front is east; get south of x,y
case E:
return has_s_wall(&(maze->cells[x][y]));
break;
// front is west; get south of x,y+1
case W:
return has_s_wall(&(maze->cells[x][y+1]));
break;
default: return false;
}
}
else return true;
}
// functions for cell value
void set_c_val(Maze * maze, int x, int y, int val) {
if(maze != NULL) {
set_val(&(maze->cells[x][y]),val);
}
}
int get_c_val(Maze * maze, int x, int y) {
if(maze != NULL) {
if ((x >= 0 && x <= 15) && (y >= 0 && y <= 15)) {
return get_val(&(maze->cells[x][y]));
}
else return INF_CELL;
}
else return INF_CELL;
}
// functions for treumax
int get_back_val(Maze * maze, Mouse * mouse) {
int x = mouse_x(mouse),
y = mouse_y(mouse);
switch(mouse_c_dir(mouse)) {
case N: return get_c_val(maze, x, y-1);
break;
case W: return get_c_val(maze, x+1, y);
break;
case S: return get_c_val(maze, x, y+1);
break;
case E: return get_c_val(maze, x-1, y);
break;
default: return 0;
break;
}
}
// functions useful for floodfill
// has a wall in c_dir direction
bool has_wall_dir(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
// if north; get south of y+1
case N:
return has_s_wall(&(maze->cells[x][y+1]));
break;
// if west; get west of x,y
case W:
return has_w_wall(&(maze->cells[x][y]));
break;
// if south; get south of x,y
case S:
return has_s_wall(&(maze->cells[x][y]));
break;
// if east; get west of x+1
case E:
return has_w_wall(&(maze->cells[x+1][y]));
break;
default: return false;
}
}
else return true;
}
// get a cell at position
Cell * get_cell(Maze * maze, int x, int y) {
if(maze != NULL) {
if ((x >= 0 && x <= 15) && (y >= 0 && y <= 15)) {
return &(maze->cells[x][y]);
}
else return NULL;
}
else return NULL;
}
// get cell in c_dir direction
Cell * get_cell_dir(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
case N:
return get_cell(maze,x,y+1);
break;
case W:
return get_cell(maze,x-1,y);
break;
case S:
return get_cell(maze,x,y-1);
break;
case E:
return get_cell(maze,x+1,y);
break;
default: return NULL;
}
}
else return NULL;
}
// get value of cell in c_dir direction
int get_val_dir(Maze * maze, int x, int y, int c_dir) {
if(maze != NULL) {
switch(c_dir) {
case N:
return get_c_val(maze,x,y+1);
break;
case W:
return get_c_val(maze,x-1,y);
break;
case S:
return get_c_val(maze,x,y-1);
break;
case E:
return get_c_val(maze,x+1,y);
break;
default: return 0;
}
}
else return INF_CELL;
}
// set wall values from mouse data
void set_walls_mouse(Maze * maze, Mouse * mouse) {
int x = mouse_x(mouse),
y = mouse_y(mouse),
c_dir = mouse_c_dir(mouse);
if(maze != NULL && mouse != NULL) {
// set right wall
if(right_wall() == true) set_r_wall(maze,x,y,c_dir);
// set front wall
if(front_wall() == true) set_f_wall(maze,x,y,c_dir);
// set left wall
if(left_wall() == true) set_l_wall(maze,x,y,c_dir);
}
}