-
Notifications
You must be signed in to change notification settings - Fork 0
/
genMaze.cpp
168 lines (148 loc) · 5.49 KB
/
genMaze.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
/********************************************************
*
* Author: Akshat Malviya (@akshat157)
* Date: Sunday, June 07
* Desc: A random maze generator using backtracking!
*
*********************************************************/
#include <iostream>
#include <stack>
#include <vector>
#include <random>
using namespace std;
// Default values
int m = 4, n = 4;
void displayMaze(int M, int N, char** maze) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cout << maze[i][j] << " ";
}
cout << endl;
}
}
// A utility function to get the index of cell with indices x, y;
int getIdx(int x, int y, vector < pair<int, pair<int, int> > > cell_list) {
for (int i = 0; i < cell_list.size(); i++)
{
if (cell_list[i].second.first == x && cell_list[i].second.second == y)
return cell_list[i].first;
}
cout << "getIdx() couldn't find the index!" << endl;
return -1;
}
void createMaze(int M, int N, char** maze) {
vector < pair<int, pair<int, int> > > cell_list;
vector <bool> visited(m*n, false);
stack<pair<int, pair<int, int> > > m_stack;
random_device rdev;
mt19937 rng(rdev());
uniform_int_distribution<mt19937::result_type> dist100(1, 100);
int nVisited = 0;
int k = 0;
for (int i = 1; i < M; i+=2) {
for (int j = 1; j < N; j+=2) {
cell_list.push_back(make_pair(k, make_pair(i, j)));
k++;
}
}
int randIdx = dist100(rng) % m*n;
m_stack.push(cell_list[randIdx]);
visited[randIdx] = true;
nVisited++;
// Algo
while(nVisited < m*n) {
vector <int> neighbours;
// North
if (m_stack.top().second.first > 1) {
if (maze[m_stack.top().second.first - 2][m_stack.top().second.second + 0] &&
!visited[getIdx(m_stack.top().second.first - 2, m_stack.top().second.second + 0, cell_list)]) {
neighbours.push_back(0);
}
}
// East
if (m_stack.top().second.second < N - 2) {
if (maze[m_stack.top().second.first + 0][m_stack.top().second.second + 2] &&
!visited[getIdx(m_stack.top().second.first + 0, m_stack.top().second.second + 2, cell_list)]) {
neighbours.push_back(1);
}
}
// South
if (m_stack.top().second.first < M - 2) {
if (maze[m_stack.top().second.first + 2][m_stack.top().second.second + 0] &&
!visited[getIdx(m_stack.top().second.first + 2, m_stack.top().second.second + 0, cell_list)]) {
neighbours.push_back(2);
}
}
// West
if (m_stack.top().second.second > 1) {
if (maze[m_stack.top().second.first + 0][m_stack.top().second.second - 2] &&
!visited[getIdx(m_stack.top().second.first + 0, m_stack.top().second.second - 2, cell_list)]) {
neighbours.push_back(3);
}
}
// Neighbours available?
if (!neighbours.empty()) {
// Choose a random direction
int next_cell_dir = neighbours[dist100(rng) % neighbours.size()];
// Create a path between this cell and neighbour
switch (next_cell_dir) {
case 0: // North
maze[m_stack.top().second.first - 1][m_stack.top().second.second + 0] = ' ';
m_stack.push(cell_list[getIdx(m_stack.top().second.first - 2, m_stack.top().second.second + 0, cell_list)]);
break;
case 1: // East
maze[m_stack.top().second.first + 0][m_stack.top().second.second + 1] = ' ';
m_stack.push(cell_list[getIdx(m_stack.top().second.first + 0, m_stack.top().second.second + 2, cell_list)]);
break;
case 2: // South
maze[m_stack.top().second.first + 1][m_stack.top().second.second + 0] = ' ';
m_stack.push(cell_list[getIdx(m_stack.top().second.first + 2, m_stack.top().second.second + 0, cell_list)]);
break;
case 3: // West
maze[m_stack.top().second.first + 0][m_stack.top().second.second - 1] = ' ';
m_stack.push(cell_list[getIdx(m_stack.top().second.first + 0, m_stack.top().second.second - 2, cell_list)]);
break;
}
visited[m_stack.top().first] = true;
nVisited++;
}
else {
m_stack.pop();
}
}
}
int main(int argc, char const *argv[]) {
cout << "Random Maze Generator!" << endl;
cout << "Enter the order of maze you want (rows (> 1) x cols (> 1)): ";
cin >> m >> n;
while (m < 1 || n < 1) {
cout << "Desired dimensions impossible. Re-enter pls." << endl;
cin >> m >> n;
}
int M = 2*m+1;
int N = 2*n+1;
char **maze;
maze = new char* [M];
for (int i = 0; i < M; i++) {
maze[i] = new char [N];
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (!(i&1)||!(j&1))
maze[i][j] = '#';
else
maze[i][j] = ' ';
}
}
for (int i = 1; i < M; i+=2) {
for (int j = 1; j < N; j+=2) {
maze[i][j] = ' ';
}
}
createMaze(M, N, maze);
maze[0][1] = 'S';
maze[2*m][2*n-1] = 'E';
cout << "Here's the maze you asked for. Enjoy! :D" << endl;
displayMaze(M, N, maze);
return 0;
}