1 | |
|
|
2 | |
1 | |
3 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
2 | /* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
4 | blocked maze with the property that there is only one path from one spot |
3 | blocked maze with the property that there is only one path from one spot |
5 | to any other, and there is always a path from one spot to any other. |
4 | to any other, and there is always a path from one spot to any other. |
6 | |
5 | |
… | |
… | |
20 | #include <global.h> |
19 | #include <global.h> |
21 | |
20 | |
22 | /*#include <random_map.h>*/ |
21 | /*#include <random_map.h>*/ |
23 | #include <maze_gen.h> |
22 | #include <maze_gen.h> |
24 | #include <time.h> |
23 | #include <time.h> |
25 | |
|
|
26 | |
|
|
27 | /* this include solely, and only, is needed for the definition of RANDOM */ |
|
|
28 | |
|
|
29 | |
|
|
30 | |
24 | |
31 | /* global variables that everyone needs: don't want to pass them in |
25 | /* global variables that everyone needs: don't want to pass them in |
32 | as parameters every time. */ |
26 | as parameters every time. */ |
33 | int *wall_x_list = 0; |
27 | int *wall_x_list = 0; |
34 | int *wall_y_list = 0; |
28 | int *wall_y_list = 0; |
… | |
… | |
137 | /* randomly returns one of the elements from the wall point list */ |
131 | /* randomly returns one of the elements from the wall point list */ |
138 | |
132 | |
139 | void |
133 | void |
140 | pop_wall_point (int *x, int *y) |
134 | pop_wall_point (int *x, int *y) |
141 | { |
135 | { |
142 | int index = RANDOM () % wall_free_size; |
136 | int index = rndm (wall_free_size); |
143 | |
137 | |
144 | *x = wall_x_list[index]; |
138 | *x = wall_x_list[index]; |
145 | *y = wall_y_list[index]; |
139 | *y = wall_y_list[index]; |
146 | /* write the last array point here */ |
140 | /* write the last array point here */ |
147 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
141 | wall_x_list[index] = wall_x_list[wall_free_size - 1]; |
… | |
… | |
225 | if (count == 0) |
219 | if (count == 0) |
226 | return -1; /* failed to find any clear points */ |
220 | return -1; /* failed to find any clear points */ |
227 | |
221 | |
228 | /* choose a random direction */ |
222 | /* choose a random direction */ |
229 | if (count > 1) |
223 | if (count > 1) |
230 | count = RANDOM () % count; |
224 | count = rndm (count); |
231 | else |
225 | else |
232 | count = 0; |
226 | count = 0; |
|
|
227 | |
233 | switch (dirlist[count]) |
228 | switch (dirlist[count]) |
234 | { |
229 | { |
235 | case 1: /* up */ |
230 | case 1: /* up */ |
236 | { |
231 | { |
237 | *y = yc + 1; |
232 | *y = yc + 1; |
238 | *x = xc; |
233 | *x = xc; |
239 | break; |
234 | break; |
240 | }; |
235 | }; |
241 | case 2: /* down */ |
236 | case 2: /* down */ |
242 | { |
237 | { |
243 | *y = yc - 1; |
238 | *y = yc - 1; |
244 | *x = xc; |
239 | *x = xc; |
245 | break; |
240 | break; |
246 | }; |
241 | }; |
247 | case 3: /* right */ |
242 | case 3: /* right */ |
248 | { |
243 | { |
249 | *y = yc; |
244 | *y = yc; |
250 | *x = xc + 1; |
245 | *x = xc + 1; |
251 | break; |
246 | break; |
252 | } |
247 | } |
253 | case 4: /* left */ |
248 | case 4: /* left */ |
254 | { |
249 | { |
255 | *x = xc - 1; |
250 | *x = xc - 1; |
256 | *y = yc; |
251 | *y = yc; |
257 | break; |
252 | break; |
258 | } |
253 | } |
259 | default: /* ??? */ |
254 | default: /* ??? */ |
260 | { |
255 | { |
261 | return -1; |
256 | return -1; |
262 | } |
257 | } |
263 | } |
258 | } |
264 | return 1; |
259 | return 1; |
265 | } |
260 | } |
266 | |
261 | |
267 | /* recursive routine which will fill every available space in the maze |
262 | /* recursive routine which will fill every available space in the maze |
… | |
… | |
274 | |
269 | |
275 | /* write a wall here */ |
270 | /* write a wall here */ |
276 | maze[x][y] = '#'; |
271 | maze[x][y] = '#'; |
277 | |
272 | |
278 | /* decide if we're going to pick from the wall_free_list */ |
273 | /* decide if we're going to pick from the wall_free_list */ |
279 | if (RANDOM () % 4 && wall_free_size > 0) |
274 | if (rndm (4) && wall_free_size > 0) |
280 | { |
275 | { |
281 | pop_wall_point (&xc, &yc); |
276 | pop_wall_point (&xc, &yc); |
282 | fill_maze_full (maze, xc, yc, xsize, ysize); |
277 | fill_maze_full (maze, xc, yc, xsize, ysize); |
283 | } |
278 | } |
284 | |
279 | |
… | |
… | |
300 | |
295 | |
301 | /* write a wall here */ |
296 | /* write a wall here */ |
302 | maze[x][y] = '#'; |
297 | maze[x][y] = '#'; |
303 | |
298 | |
304 | /* decide if we're going to pick from the wall_free_list */ |
299 | /* decide if we're going to pick from the wall_free_list */ |
305 | if (RANDOM () % 4 && wall_free_size > 0) |
300 | if (rndm (4) && wall_free_size > 0) |
306 | { |
301 | { |
307 | pop_wall_point (&xc, &yc); |
302 | pop_wall_point (&xc, &yc); |
308 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
303 | fill_maze_sparse (maze, xc, yc, xsize, ysize); |
309 | } |
304 | } |
310 | |
305 | |