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