-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_generator.cpp
112 lines (98 loc) · 3.58 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
// DaanyaMGA = Daanya's Maze Generator Algorithm
# include "DaanyaMGA.hpp"
// using namespace std;
DaanyaMGA::DaanyaMGA()
{
}
vector<Direction> DaanyaMGA::possibleDirections(Maze& maze)
{
vector<Direction> possible;
if (currentCell_y - 1 >= 0) {
// ensures next cell being tested is within maze confines
// next part checks that next cell has all 4 walls drawn
if (maze.wallExists(currentCell_x, currentCell_y-1, Direction::up)
&& maze.wallExists(currentCell_x, currentCell_y-1, Direction::right)
&& maze.wallExists(currentCell_x, currentCell_y-1, Direction::down)
&& maze.wallExists(currentCell_x, currentCell_y-1, Direction::left)){
possible.push_back(Direction::up);
}
}
if (currentCell_x + 1 < maze.getWidth()) {
if (maze.wallExists(currentCell_x+1, currentCell_y, Direction::up)
&& maze.wallExists(currentCell_x+1, currentCell_y, Direction::right)
&& maze.wallExists(currentCell_x+1, currentCell_y, Direction::down)
&& maze.wallExists(currentCell_x+1, currentCell_y, Direction::left)){
possible.push_back(Direction::right);
}
}
if (currentCell_y + 1 < maze.getHeight()) {
if (maze.wallExists(currentCell_x, currentCell_y+1, Direction::up)
&& maze.wallExists(currentCell_x, currentCell_y+1, Direction::right)
&& maze.wallExists(currentCell_x, currentCell_y+1, Direction::down)
&& maze.wallExists(currentCell_x, currentCell_y+1, Direction::left)){
possible.push_back(Direction::down);
}
}
if (currentCell_x - 1 >= 0) {
if (maze.wallExists(currentCell_x-1, currentCell_y, Direction::up)
&& maze.wallExists(currentCell_x-1, currentCell_y, Direction::right)
&& maze.wallExists(currentCell_x-1, currentCell_y, Direction::down)
&& maze.wallExists(currentCell_x-1, currentCell_y, Direction::left)){
possible.push_back(Direction::left);
}
}
return possible;
}
void DaanyaMGA::move(Maze& maze)
{
vector<Direction> possible = possibleDirections(maze);
// to handle when algorithm hits dead end and needs to backtrack
int count = 1;
while (possible.size() == 0
&& visited.size() < (maze.getWidth() * maze.getHeight())) {
currentCell_x = get<0>(visited[visited.size()-count]);
currentCell_y = get<1>(visited[visited.size()-count]);
possible = possibleDirections(maze);
count ++;
}
// randomly picks which direction to go in next and keeps looking until
// direction is valid, then removes wall in that direction
Direction d;
int direction = 0;
do{
direction = distribution(engine);
switch(direction){
case 1: d = Direction::up; break;
case 2: d = Direction::right; break;
case 3: d = Direction::down; break;
case 4: d = Direction::left; break;
}
} while (find(possible.begin(), possible.end(), d) == possible.end());
maze.removeWall(currentCell_x, currentCell_y, d);
// mark current cell as visited
visited.push_back(make_tuple(currentCell_x, currentCell_y));
// change current cell coordinates to new cell whose wall we just removed
switch(direction){
case 1: currentCell_y -= 1; break;
case 2: currentCell_x += 1; break;
case 3: currentCell_y += 1; break;
case 4: currentCell_x -= 1; break;
}
}
void DaanyaMGA::mainRecursive(Maze& maze)
{
move(maze);
if (visited.size() == (maze.getHeight() * maze.getWidth())) {
return;
} else {
mainRecursive(maze);
}
}
void DaanyaMGA::generateMaze(Maze& maze)
{
maze.addAllWalls();
currentCell_x = 0;
currentCell_y = 0;
visited.push_back(make_tuple(currentCell_x, currentCell_y));
mainRecursive(maze);
}