forked from Nimdoc/pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_generator.cpp
334 lines (293 loc) · 9.3 KB
/
maze_generator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "maze_generator.h"
void generate_maze::generate_map()
{
// TEMP CHEATS VARIABLE
// replace with command line argument or toggle?
// or we can just leave it out of the game but show it in
// console for debugging
bool cheats = false;
int random_wall = 0;
int pos_X, pos_Y;
int total_num_cells;
// pick a random starting point with odd coordinates
srand(unsigned(time(0)));
pos_X = ( ( (rand()/2) *2) +1) % (map_size -1);
pos_Y = ( ( (rand()/2) *2) +1) % (map_size -1);
// total number of walls to be removed to make the maze
total_num_cells = ((map_size-1)/2)*((map_size-1)/2);
// store starting pos in class members
set_start_pos(pos_Y, pos_X);
// mark the starting cell as a start cell, and mark it as visited
map[start_Y][start_X].cell_type = START;
map[start_Y][start_X].visited = true;
int num_visited_cells = 1;
// stores the pos for X and Y
// when dead end is reached, pop X and Y off and check
// for a traversable cell
// repeat until maze is generated
stack<int> traversed_X, traversed_Y;
// stores the cheat path positoins for X and Y
// using different stacks so that we can store
// the odd and even coordinates without changing the
// maze generation, using traversed_X and traversed_Y
// marks only the odd coordinates as cheat_path
stack<int> cheat_X, cheat_Y;
// loop through until all the walls are knocked out that need to be
while((num_visited_cells < total_num_cells))
{
bool can_move_north = false;
bool can_move_south = false;
bool can_move_west = false;
bool can_move_east = false;
// if current cell is 2 or more cells from north edge of map
// and the target cell hasn't been visited yet
if ( (pos_Y > 1) && (map[pos_Y-2][pos_X].visited == false ) &&
// and the north wall exists for current cell
(map[pos_Y][pos_X].north_wall == true &&
// and the south wall exists for target cell
map[pos_Y-2][pos_X].south_wall == true) )
{
can_move_north = true;
}
// if current cell is 2 or more cells from south edge of map
// and the target cell hasn't been visited yet
if ( (pos_Y < map_size-2) && (map[pos_Y+2][pos_X].visited == false ) &&
// and the south wall exists for current cell
(map[pos_Y][pos_X].south_wall == true &&
// and the north wall exists for target cell
map[pos_Y+2][pos_X].north_wall == true) )
{
can_move_south = true;
}
// if current cell is 2 or more cells from the west edge of map
// and the target cell hasn't been visited yet
if ( (pos_X > 1) && (map[pos_Y][pos_X-2].visited == false) &&
// and the west wall exists for current cell
(map[pos_Y][pos_X].west_wall == true &&
// and the east wall exists for target cell
map[pos_Y][pos_X-2].east_wall == true) )
{
can_move_west = true;
}
// if is 2 or more cells fromt he east edge of the map
// and the target cell hasn't been visited yet
if ( (pos_X < map_size-2) &&
(map[pos_Y][pos_X+2].visited == false) &&
// and the east wall exists for current cell
(map[pos_Y][pos_X].east_wall == true &&
// and the west wall exists for target pos
map[pos_Y][pos_X+2].west_wall == true) )
{
can_move_east = true;
}
// if we can move to a target cell in any direction and clear the wall
// between them..
if (can_move_north || can_move_south || can_move_west || can_move_east)
{
// found_next_move is true when a random direction is chosen
bool found_next_move = false;
// cycle through until next move is found
while (!found_next_move) {
// pick a random wall bordering the current cell
random_wall = (rand() % 4);
// pick a direction to move
Direction dir;
switch (random_wall)
{
case 0:
dir = Direction::NORTH;
break;
case 1:
dir = Direction::SOUTH;
break;
case 2:
dir = Direction::WEST;
break;
case 3:
dir = Direction::EAST;
break;
}
// move north
if (dir == Direction::NORTH && can_move_north)
{
found_next_move = true;
map[pos_Y-1][pos_X].cell_type = SPACE; // clear the cell
map[pos_Y-1][pos_X].visited = true; // mark cell as visited
map[pos_Y][pos_X].north_wall = false; // remove north wall
// store x and y pos on stack
traversed_X.push(pos_X);
traversed_Y.push(pos_Y);
// used store the path from start to end
if (cheats) {
cheat_X.push(pos_X);
cheat_Y.push(pos_Y);
cheat_X.push(pos_X);
cheat_Y.push(pos_Y-1);
}
pos_Y -= 2; // move to new cell
map[pos_Y][pos_X].visited = true; // mark new cell as visited
map[pos_Y][pos_X].cell_type = SPACE; // clear the new cell
map[pos_Y][pos_X].south_wall = false; // remove south wall
num_visited_cells++;
}
// move south
else if (dir == Direction::SOUTH && can_move_south)
{
found_next_move = true;
map[pos_Y+1][pos_X].cell_type = SPACE; // clear the cell
map[pos_Y+1][pos_X].visited = true; // mark cell as visited
map[pos_Y][pos_X].south_wall = false; // remove south wall
// store x and y pos on stack
traversed_X.push(pos_X);
traversed_Y.push(pos_Y);
// used store the path from start to end
if (cheats) {
cheat_X.push(pos_X);
cheat_Y.push(pos_Y);
cheat_X.push(pos_X);
cheat_Y.push(pos_Y+1);
}
pos_Y += 2; // move to new cell
map[pos_Y][pos_X].visited = true; // mark new cell as visited
map[pos_Y][pos_X].cell_type = SPACE; // clear the new cell
map[pos_Y][pos_X].north_wall = false; // remove north wall
num_visited_cells++;
}
// move west
else if (dir == Direction::WEST && can_move_west)
{
found_next_move = true;
map[pos_Y][pos_X-1].cell_type = SPACE; // clear the cell
map[pos_Y][pos_X-1].visited = true; // mark cell as visited
map[pos_Y][pos_X].west_wall = false; // remove west wall
// store x and y pos on stack
traversed_X.push(pos_X);
traversed_Y.push(pos_Y);
// used store the path from start to end
if (cheats) {
cheat_X.push(pos_X);
cheat_Y.push(pos_Y);
cheat_X.push(pos_X-1);
cheat_Y.push(pos_Y);
}
pos_X -= 2; // move to new cell
map[pos_Y][pos_X].visited = true; // mark new cell as visited
map[pos_Y][pos_X].cell_type = SPACE; // clear the new cell
map[pos_Y][pos_X].east_wall = false; // remove east wall
num_visited_cells++;
}
// move east
else if (dir == Direction::EAST && can_move_east)
{
found_next_move = true;
map[pos_Y][pos_X+1].cell_type = SPACE; // clear the cell
map[pos_Y][pos_X+1].visited = true; // mark cell as visited
map[pos_Y][pos_X].east_wall = false; // remove east wall
// store x and y pos on stack
traversed_X.push(pos_X);
traversed_Y.push(pos_Y);
// used store the path from start to end
if (cheats) {
cheat_X.push(pos_X);
cheat_Y.push(pos_Y);
cheat_X.push(pos_X+1);
cheat_Y.push(pos_Y);
}
pos_X += 2; // move to new cell
map[pos_Y][pos_X].visited = true; // mark cell as visited
map[pos_Y][pos_X].cell_type = SPACE; // clear the new cell
map[pos_Y][pos_X].west_wall = false; // remove west wall
num_visited_cells++;
}
}
}
else
{
// pop off x and y pos, reiterate and check for walls on new x and y pos
// until a new path can be formed
if ((traversed_X.size() != 0)) {
pos_Y = traversed_Y.top();
pos_X = traversed_X.top();
traversed_Y.pop();
traversed_X.pop();
cheat_X.pop();
cheat_X.pop();
cheat_Y.pop();
cheat_Y.pop();
}
}
}
// mark last cell as End
map[pos_Y][pos_X].cell_type = END;
// store ending pos in class members
set_end_pos(pos_Y, pos_X);
// if cheats then label the cells which make up the correct path
if (cheats)
cheat_mode(cheat_Y, cheat_X);
}
// mark the cheat path
void generate_maze::cheat_mode(stack<int> cheat_Y, stack<int> cheat_X) {
while (cheat_Y.size() > 1) {
map[cheat_Y.top()][cheat_X.top()].cell_type = 4;
cheat_Y.pop();
cheat_X.pop();
}
}
// initialize the map
void generate_maze::init_map()
{
for(int i = 0; i < map_size; i++)
{
for(int j = 0; j < map_size; j++)
{
map[i][j].cell_type = WALL; // mark each cell as a wall
map[i][j].visited = false; // mark each cell as unvisited
// mark each cell as surrounded by walls
map[i][j].north_wall = true;
map[i][j].south_wall = true;
map[i][j].west_wall = true;
map[i][j].east_wall = true;
}
}
// for each cell bordering the edge
// mark the walls bordering the edge as false
// given the maze:
// 0 0 0 0 0
// 0 1 1 1 0
// 0 1 0 1 0
// 0 1 1 1 0
// 0 0 0 0 0
// the 1s denote the cells to be changed in this nested loop
for(int i = 1; i < map_size-1; i++)
{
for(int j = 1; j < map_size-1; j++)
{
map[1][j].north_wall = false;
map[map_size-2][j].south_wall = false;
map[i][1].west_wall = false;
map[i][map_size-2].east_wall = false;
}
}
}
// prints the map in console
void generate_maze::print_map()
{
for (int i = 0; i < map_size; i++)
{
cout << endl;
for (int j = 0; j < map_size; j++)
{
if (map[i][j].cell_type == SPACE)
cout << " ";
else if (map[i][j].cell_type == WALL)
cout << "#";
else if (map[i][j].cell_type == START)
cout << "S";
else if (map[i][j].cell_type == END)
cout << "E";
else if (map[i][j].cell_type == CHEAT_PATH)
cout << ".";
}
}
cout << endl << endl;
}