-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
211 lines (180 loc) · 6.18 KB
/
maze.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <fstream>
#include <ctime>
#include <emscripten/bind.h>
class Maze
{
private:
// Structure to represent a maze cell with its parent and rank.
struct Cell { int parent, rank; };
// Structure to represent a wall between two cells.
struct Wall { int cell1, cell2; };
// Find the root of a cell using path compression.
const int Find(int x)
{
int root = x;
while (root != cells[root].parent) root = cells[root].parent;
// Path compression
while (x != root)
{
int parent = cells[x].parent;
cells[x].parent = root;
x = parent;
}
return root;
}
// Union two sets of cells using the rank-based union optimisation.
void UnionSets(int x, int y)
{
if (cells[x].rank < cells[y].rank) std::swap(x, y);
cells[y].parent = x;
if (cells[x].rank == cells[y].rank) cells[x].rank++;
}
// Maze dimensions and data structures to store cells and the grid.
int width, height;
std::vector<Cell> cells;
std::vector<char> grid;
public:
// Empty constructor
Maze() = default;
// Generate a random maze using Kruskal's Algorithm.
void Generate(int w, int h, int seed = -1)
{
// If the seed is -1, use the current time as the seed.
if (seed == -1) seed = std::time(0);
// Set width and height
width = w;
height = h;
// Resize and initialize cells
cells.resize(w * h);
for (int i = 0; i < width * height; i++) cells[i] = { i, 0 };
// Resize and initialize the maze grid.
grid.resize((h * 2 + 1) * (w * 2 + 1), ' ');
for (int y = 0; y < height * 2 + 1; y++)
for (int x = 0; x < width * 2 + 1; ++x) grid[y * (width * 2 + 1) + x] = (x % 2 == 0 || y % 2 == 0) ? '#' : ' ';
// Vector to store the walls between the cells.
std::vector<Wall> walls;
walls.reserve((width - 1) * height + (height - 1) * width);
// Add walls between adjacent cells.
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
int index = y * width + x;
if (x < width - 1) walls.push_back({ index, index + 1 });
if (y < height - 1) walls.push_back({ index, index + width });
}
// Shuffle the walls randomly.
std::mt19937 rng(seed);
std::shuffle(walls.begin(), walls.end(), rng);
// Iterate through the shuffled walls.
for (const auto& wall : walls)
{
// Find the roots of the cells on either side of the wall.
int root1 = this->Find(wall.cell1), root2 = this->Find(wall.cell2);
// If the roots are different, remove the wall and union the sets.
if (root1 != root2)
{
this->UnionSets(root1, root2);
int x1 = wall.cell1 % width, y1 = wall.cell1 / width, x2 = wall.cell2 % width, y2 = wall.cell2 / width;
grid[y1 * 2 + 1 + (width * 2 + 1) * (y1 * 2 + 1)] = grid[y2 * 2 + 1 + (width * 2 + 1) * (y2 * 2 + 1)] = grid[(y1 + y2) + 1 + (width * 2 + 1) * ((x1 + x2) + 1)] = '-';
}
}
// Mark the start and end points in the maze. This is always the top left and bottom right corners.
grid[(width * 2 + 1) + 1] = 'S';
grid[(height * 2 - 1) * (width * 2 + 1) + width * 2] = 'E';
}
// Print the generated maze to the console.
void Print()
{
for (int y = 0; y < height * 2 + 1; y++)
{
for (int x = 0; x < width * 2 + 1; x++) std::cout << grid[y * (width * 2 + 1) + x];
std::cout << '\n';
}
}
// Convert the generated maze to a string.
std::string MazeToString()
{
std::string mazeStr;
for (int y = 0; y < height * 2 + 1; y++)
{
for (int x = 0; x < width * 2 + 1; x++)
{
mazeStr += grid[y * (width * 2 + 1) + x];
}
mazeStr += '\n';
}
return mazeStr;
}
// Convert the generated maze to a string.
std::string MazeToHTMLString()
{
std::string mazeStr;
for (int y = 0; y < height * 2 + 1; y++)
{
for (int x = 0; x < width * 2 + 1; x++)
{
switch(grid[y * (width * 2 + 1) + x]) {
case '#':
mazeStr += "<div class=\"cell wall\"></div>";
break;
case '-':
mazeStr += "<div class=\"cell path\"></div>";
break;
case 'S':
mazeStr += "<div class=\"cell start\"></div>";
break;
case 'E':
mazeStr += "<div class=\"cell end\"></div>";
break;
default:
mazeStr += "<div class=\"cell unknown\"></div>";
}
}
}
return mazeStr;
}
// Save the generated maze to a file.
void SaveToFile(const std::string& filename, const std::string& content)
{
std::ofstream file(filename, std::ios::binary);
// Check if the file is open.
if (file.is_open())
{
// Write the maze to the file.
file << content;
// Close the file.
file.close();
}
else std::cerr << "Unable to open file " << filename << std::endl;
}
};
// Binding to WASM
EMSCRIPTEN_BINDINGS(maze_class) {
emscripten::class_<Maze>("Maze")
.constructor()
.function("Generate", &Maze::Generate)
.function("Print", &Maze::Print)
.function("MazeToString", &Maze::MazeToString)
.function("MazeToHTMLString", &Maze::MazeToHTMLString)
;
}
void LocalDemo()
{
// Create a maze with the specified dimensions
Maze maze;
// Generate the maze
maze.Generate(10, 10);
// Print the maze to the console
maze.Print();
// Save the maze to a file
maze.SaveToFile("maze.txt", maze.MazeToString());
}
int main()
{
// LocalDemo();
return 0;
}