1 |
/* peterm@langmuir.eecs.berkeley.edu: this function generates a random |
2 |
snake-type layout. |
3 |
|
4 |
input: xsize, ysize; |
5 |
output: a char** array with # and . for closed and open respectively. |
6 |
|
7 |
a char value of 0 represents a blank space: a '#' is |
8 |
a wall. |
9 |
|
10 |
*/ |
11 |
|
12 |
|
13 |
#include <stdio.h> |
14 |
#include <global.h> |
15 |
#include <time.h> |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
char ** |
21 |
make_snake_layout (int xsize, int ysize, int options) |
22 |
{ |
23 |
int i, j; |
24 |
|
25 |
/* allocate that array, set it up */ |
26 |
char **maze = (char **) calloc (sizeof (char *), xsize); |
27 |
|
28 |
for (i = 0; i < xsize; i++) |
29 |
{ |
30 |
maze[i] = (char *) calloc (sizeof (char), ysize); |
31 |
} |
32 |
|
33 |
/* write the outer walls */ |
34 |
for (i = 0; i < xsize; i++) |
35 |
maze[i][0] = maze[i][ysize - 1] = '#'; |
36 |
for (j = 0; j < ysize; j++) |
37 |
maze[0][j] = maze[xsize - 1][j] = '#'; |
38 |
|
39 |
/* Bail out if the size is too small to make a snake. */ |
40 |
if (xsize < 8 || ysize < 8) |
41 |
return maze; |
42 |
|
43 |
/* decide snake orientation--vertical or horizontal , and |
44 |
make the walls and place the doors. */ |
45 |
|
46 |
if (RANDOM () % 2) |
47 |
{ /* vertical orientation */ |
48 |
int n_walls = RANDOM () % ((xsize - 5) / 3) + 1; |
49 |
int spacing = xsize / (n_walls + 1); |
50 |
int orientation = 1; |
51 |
|
52 |
for (i = spacing; i < xsize - 3; i += spacing) |
53 |
{ |
54 |
if (orientation) |
55 |
{ |
56 |
for (j = 1; j < ysize - 2; j++) |
57 |
{ |
58 |
maze[i][j] = '#'; |
59 |
} |
60 |
maze[i][j] = 'D'; |
61 |
} |
62 |
else |
63 |
{ |
64 |
for (j = 2; j < ysize; j++) |
65 |
{ |
66 |
maze[i][j] = '#'; |
67 |
} |
68 |
maze[i][1] = 'D'; |
69 |
} |
70 |
orientation ^= 1; /* toggle the value of orientation */ |
71 |
} |
72 |
} |
73 |
else |
74 |
{ /* horizontal orientation */ |
75 |
int n_walls = RANDOM () % ((ysize - 5) / 3) + 1; |
76 |
int spacing = ysize / (n_walls + 1); |
77 |
int orientation = 1; |
78 |
|
79 |
for (i = spacing; i < ysize - 3; i += spacing) |
80 |
{ |
81 |
if (orientation) |
82 |
{ |
83 |
for (j = 1; j < xsize - 2; j++) |
84 |
{ |
85 |
maze[j][i] = '#'; |
86 |
} |
87 |
maze[j][i] = 'D'; |
88 |
} |
89 |
else |
90 |
{ |
91 |
for (j = 2; j < xsize; j++) |
92 |
{ |
93 |
maze[j][i] = '#'; |
94 |
} |
95 |
maze[1][i] = 'D'; |
96 |
} |
97 |
orientation ^= 1; /* toggle the value of orientation */ |
98 |
} |
99 |
} |
100 |
|
101 |
/* place the exit up/down */ |
102 |
if (RANDOM () % 2) |
103 |
{ |
104 |
maze[1][1] = '<'; |
105 |
maze[xsize - 2][ysize - 2] = '>'; |
106 |
} |
107 |
else |
108 |
{ |
109 |
maze[1][1] = '>'; |
110 |
maze[xsize - 2][ysize - 2] = '<'; |
111 |
} |
112 |
|
113 |
|
114 |
return maze; |
115 |
} |