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