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