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