forked from Parrva-Shah/Maze_Pathfinding_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
70 lines (56 loc) · 1.8 KB
/
Copy pathMaze.cpp
File metadata and controls
70 lines (56 loc) · 1.8 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
#include "Maze.h"
#include <random>
#include <algorithm>
#include <ctime>
using namespace std;
Maze::Maze(int rows, int cols, unsigned seed_)
: rows(rows), cols(cols)
{
// Ensure minimum usable dimensions
rows = max(rows, 5);
cols = max(cols, 5);
grid.assign(rows, string(cols, '#'));
if (seed_ != 0) {
seed = seed_;
} else {
std::random_device rd; // Get a true random seed
seed = rd();
}
generateSolvableMaze(25);
}
void Maze::generateSolvableMaze(int wallDensity) {
// 1. Create the random number generator
mt19937 rng(seed);
uniform_int_distribution<int> rowDist(1, rows - 2);
uniform_int_distribution<int> colDist(1, cols - 2);
uniform_int_distribution<int> percent(0, 99);
// 2. Pick random start (S) and end (E) points *first*
start = {rowDist(rng), colDist(rng)};
do {
goal = {rowDist(rng), colDist(rng)};
} while (start == goal); // Ensure they aren't the same spot
// 3. Fill the entire grid with walls
for (auto& row : grid)
fill(row.begin(), row.end(), '#');
// 4. Carve out the *entire* inside area
for (int r = 1; r < rows - 1; ++r) {
for (int c = 1; c < cols - 1; ++c) {
grid[r][c] = ' ';
}
}
// 5. Add random walls inside
for (int r = 1; r < rows - 1; ++r) {
for (int c = 1; c < cols - 1; ++c) {
// Don't place a wall on S or E
if ((r == start.first && c == start.second) ||
(r == goal.first && c == goal.second))
continue;
if (percent(rng) < wallDensity) {
grid[r][c] = '#';
}
}
}
// 6. Finally, place Start and End
grid[start.first][start.second] = 'S';
grid[goal.first][goal.second] = 'E';
}