-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeGenerator.cpp
More file actions
95 lines (82 loc) · 3.1 KB
/
MazeGenerator.cpp
File metadata and controls
95 lines (82 loc) · 3.1 KB
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
#include "MazeGenerator.h"
MazeGenerator::MazeGenerator(int width, int height)
: mazeWidth(width), mazeHeight(height), animating(false) {
if (!validateDimensions(width, height)) {
throw std::invalid_argument("Invalid maze dimensions");
}
directions = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};
}
void MazeGenerator::generate(vector<vector<int>>& maze) {
// Start with all walls
for (int y = 0; y < 2 * mazeHeight + 1; y++) {
for (int x = 0; x < 2 * mazeWidth + 1; x++) {
maze[y][x] = 1;
}
}
// Start at a random cell
current_x = 2 * (static_cast<int>(ofRandom(mazeWidth))) + 1;
current_y = 2 * (static_cast<int>(ofRandom(mazeHeight))) + 1;
maze[current_y][current_x] = 0;
unvisited = mazeWidth * mazeHeight - 1;
while (unvisited > 0) {
// Pick a random direction
int dir_idx = static_cast<int>(ofRandom(4));
int dx = directions[dir_idx].first;
int dy = directions[dir_idx].second;
int next_x = current_x + dx;
int next_y = current_y + dy;
if (isValid(next_x, next_y) && maze[next_y][next_x] == 1) {
maze[(current_y + next_y) / 2][(current_x + next_x) / 2] = 0;
maze[next_y][next_x] = 0;
unvisited--;
current_x = next_x;
current_y = next_y;
} else {
do {
current_x = 2 * (static_cast<int>(ofRandom(mazeWidth))) + 1;
current_y = 2 * (static_cast<int>(ofRandom(mazeHeight))) + 1;
} while (maze[current_y][current_x] == 1);
}
}
// Create entrance and exit
maze[0][1] = 0;
maze[2 * mazeHeight][2 * mazeWidth - 1] = 0;
maze[1][1] = 0;
maze[2 * mazeHeight - 1][2 * mazeWidth - 1] = 0;
}
void MazeGenerator::updateAnimation(vector<vector<int>>& maze) {
if (!animating || unvisited <= 0) return;
int dir_idx = static_cast<int>(ofRandom(4));
int dx = directions[dir_idx].first;
int dy = directions[dir_idx].second;
int next_x = current_x + dx;
int next_y = current_y + dy;
if (isValid(next_x, next_y) && maze[next_y][next_x] == 1) {
maze[(current_y + next_y) / 2][(current_x + next_x) / 2] = 0;
maze[next_y][next_x] = 0;
unvisited--;
current_x = next_x;
current_y = next_y;
} else {
do {
current_x = 2 * (static_cast<int>(ofRandom(mazeWidth))) + 1;
current_y = 2 * (static_cast<int>(ofRandom(mazeHeight))) + 1;
} while (maze[current_y][current_x] == 1);
}
if (unvisited <= 0) {
maze[0][1] = 0;
maze[2 * mazeHeight][2 * mazeWidth - 1] = 0;
maze[1][1] = 0;
maze[2 * mazeHeight - 1][2 * mazeWidth - 1] = 0;
animating = false;
}
}
void MazeGenerator::reset() {
current_x = 2 * (static_cast<int>(ofRandom(mazeWidth))) + 1;
current_y = 2 * (static_cast<int>(ofRandom(mazeHeight))) + 1;
unvisited = mazeWidth * mazeHeight - 1;
animating = true;
}
bool MazeGenerator::isValid(int x, int y) const {
return x > 0 && x < 2 * mazeWidth && y > 0 && y < 2 * mazeHeight;
}