-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cpp
339 lines (313 loc) · 9.05 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "Maze2D.h"
Maze2D::Maze2D(int row, int column)
{
_height = column;
_width = row;
_maze = new char* [column];
for (int i = 0; i < column; i++)
_maze[i] = new char[row];
}
Maze2D::Maze2D(const Maze2D& maze)
{
_height = maze._height;
_width = maze._width;
_maze = new char* [_height];
for (int i = 0; i < _height; i++)
_maze[i] = new char[_width];
for (int i = 0; i < _height; i++)
for (int j = 0; j < _width; j++)
_maze[i][j] = maze._maze[i][j];
}
void Maze2D::setMaze(char** maze)
{
for (int i = 0; i < _height; i++)
for (int j = 0; j < _width; j++)
_maze[i][j] = maze[i][j];
}
std::ostream& operator<<(std::ostream& out, const Maze2D& maze)
{
for (int i = 0; i < maze._width * 2 + 2; i++)
out << "$";
out << std::endl;
for (int i = 0; i < maze._height; i++)
{
out << "$";
for (int j = 0; j < maze._width; j++)
{
if (maze._maze[i][j] == '1')
out << "##";
else if (maze._maze[i][j] == '*')
out << "**";
else if (maze._maze[i][j] == 'S')
out << "S ";
else if (maze._maze[i][j] == 'E')
out << "E ";
else
out << " ";
}
out << "$" << std::endl;
}
for (int i = 0; i < maze._width * 2 + 2; i++)
out << "$";
out << std::endl;
return out;
};
Position Maze2D::getStartPosition() const
{
for (int i = 0; i < _height; i++)
for (int j = 0; j < _width; j++)
if (_maze[i][j] == 'S')
return Position(j, i);
return Position();
}
std::string Maze2D::getPossibleMoves(const Position& p) const
{
std::string str = "";
if (p.getY() > _height - 1 && p.getX() <= 0)
return str;
if (!(p.getY() == 0))
if (_maze[p.getY() - 1][p.getX()] != '1')
str += "Up ";
if (!(p.getY() >= _height - 1))
if (_maze[p.getY() + 1][p.getX()] != '1')
str += "Down ";
if (!(p.getX() == 0))
if (_maze[p.getY()][p.getX() - 1] != '1')
str += "Left ";
if (!(p.getX() >= _width - 1))
if (_maze[p.getY()][p.getX() + 1] != '1')
str += "Right ";
return str;
}
Position Maze2D::getGoalPosition()
{
for (int i = 0; i < _height; i++)
for (int j = 0; j < _width; j++)
if (_maze[i][j] == 'E')
return Position(j, i);
return Position();
}
std::string Maze::measureAlgorithmTime(int row, int column)
{
auto start = std::chrono::high_resolution_clock::now();
generate(row , column);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
return "It takes " + std::to_string((double)duration.count() / 1000) + " sec to genarate " + std::to_string(column) + "x" + std::to_string(row) + " maze.";
}
Maze2D MyMaze2dGenerator::generate(int row, int column)
{
// select random point and open as start node
// get maze from Maze2d and initialize with walls
Maze2D maze(row , column);
char** temp_maze = maze.getMaze();
for (int i = 0; i < column; i++)
for (int j = 0; j < row; j++)
temp_maze[i][j] = '1';
Position p(0, rand() % row);
temp_maze[p.getY()][p.getX()] = '0';
std::vector<std::pair<Position, Position>> wallList;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0 || i != 0 && j != 0)
continue;
if (!(p.getY() + i >= 0 && p.getY() + i <= column && p.getX() + j >= 0 && p.getX() + j <= row))
continue;
wallList.push_back(std::make_pair(Position(p.getX() + j , p.getY() + i), p));
}
Position last;
while (!wallList.empty())
{
int rand_next = rand() % wallList.size();
std::pair<Position, Position> next_to_check(wallList[rand_next]);
wallList.erase(wallList.begin() + rand_next);
Position opposite_to_wall = next_to_check.first.opposite(next_to_check.second);
if (opposite_to_wall.getY() < 0 || opposite_to_wall.getY() >= column || opposite_to_wall.getX() < 0 || opposite_to_wall.getX() >= row)
continue;
if (temp_maze[next_to_check.first.getY()][next_to_check.first.getX()] == '1')
{
if (temp_maze[opposite_to_wall.getY()][opposite_to_wall.getX()] == '1')
{
temp_maze[next_to_check.first.getY()][next_to_check.first.getX()] = '0';
temp_maze[opposite_to_wall.getY()][opposite_to_wall.getX()] = '0';
last = opposite_to_wall;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0 || i != 0 && j != 0)
continue;
if (opposite_to_wall.getY() + i < 0 || opposite_to_wall.getY() + i >= column || opposite_to_wall.getX() + j < 0 || opposite_to_wall.getX() + j >= row)
continue;
if (temp_maze[opposite_to_wall.getY() + i][opposite_to_wall.getX() + j] == '0')
continue;
wallList.push_back(std::make_pair(Position(opposite_to_wall.getX() + j, opposite_to_wall.getY() + i), opposite_to_wall));
}
}
}
}
bool isLastColumnIsWall = 1;
for (int i = 0; i < column; i++)
if (temp_maze[i][row - 1] == '0')
{
isLastColumnIsWall = 0;
break;
}
int columnToPushRandomE = isLastColumnIsWall ? row - 2 : row - 1;
while (1)
{
int randRow = rand() % column;
if (temp_maze[randRow][columnToPushRandomE] == '1')
continue;
temp_maze[randRow][columnToPushRandomE] = 'E';
break;
}
temp_maze[p.getY()][p.getX()] = 'S';
return maze;
}
Maze2D SimpleMaze2dGenerator::generate(int row, int column)
{
Maze2D maze(row, column);
char** temp_maze = maze.getMaze();
for (int i = 0; i < column; i++)
for (int j = 0; j < row; j++)
temp_maze[i][j] = '0';
if (maze.getHeight() > maze.getWidth())
Divide(0, maze.getWidth(), 0, maze.getHeight(), Vertical, maze);
else
Divide(0, maze.getWidth(), 0, maze.getHeight(), Horizonal, maze);
Position start(0, rand() % row);
Position goal(maze.getWidth() - 1, rand() % row);
temp_maze[start.getY()][start.getX()] = 'S';
temp_maze[goal.getY()][goal.getX()] = 'E';
return maze;
}
void SimpleMaze2dGenerator::Divide(int first_column, int last_column, int first_row, int last_row, bool direction, Maze2D& maze)
{
char** temp_maze = maze.getMaze();
if (last_row - first_row <= 2 || last_column - first_column <= 2)
return;
if (direction) // Vertical - Insert a vertical wall and open a gap there , and divide horizontally.
{
int wall;
while (1)
{
wall = first_column + rand() % (last_column - first_column - 1);
wall == first_column ? wall++ : wall;
if (wall % 2 == 0 || last_column - first_column == 2)
break;
}
bool hasGap = false;
for (int i = first_row; i < last_row; i++)
{
if(first_row > 0)
if (i == first_row && temp_maze[first_row - 1][wall] == '0')
{
hasGap = true;
continue;
}
if (last_row < maze.getWidth())
if (i == last_row - 1 && temp_maze[last_row][wall] == '0')
{
hasGap = true;
continue;
}
temp_maze[i][wall] = '1';
}
int gap;
while(1)
{
gap = first_row + rand() % (last_row - first_row);
if (gap % 2 == 1)
break;
}
hasGap ? NULL : temp_maze[gap][wall] = '0';
Divide(first_column, wall, first_row, last_row, Horizonal, maze);
Divide(wall + 1, last_column, first_row, last_row, Horizonal, maze);
}
else // Horizontally - Insert a vertical wall and open a gap there , and divide Vertically.
{
int wall;
while(1)
{
wall = first_row + rand() % (last_row - first_row - 1);
wall == first_row ? wall++ : wall;
if (wall % 2 == 0 || last_row - first_row == 2)
break;
}
bool hasGap = false;
for (int i = first_column; i < last_column; i++)
{
if (first_column > 0)
if (i == first_column && temp_maze[wall][first_column - 1] == '0')
{
hasGap = true;
continue;
}
if(last_column < maze.getHeight())
if (i == last_column - 1 && temp_maze[wall][last_column] == '0')
{
hasGap = true;
continue;
}
temp_maze[wall][i] = '1';
}
int gap;
while (1)
{
gap = first_column + rand() % (last_column - first_column);
if (gap % 2 == 1)
break;
}
//temp_maze[wall][gap] = '0';
hasGap ? NULL : temp_maze[wall][gap] = '0';
Divide(first_column, last_column , first_row, wall, Vertical, maze);
Divide(first_column, last_column, wall + 1, last_row, Vertical, maze);
}
return;
}
void Maze2D::printEmptyMaze(const Maze2D& maze) const
{
for (int i = 0; i < maze._width * 2 + 2; i++)
std::cout << "$";
std::cout << std::endl;
for (int i = 0; i < maze._height; i++)
{
std::cout << "$";
for (int j = 0; j < maze._width; j++)
{
if (maze._maze[i][j] == '1')
std::cout << "##";
else if (maze._maze[i][j] == '*')
std::cout << " ";
else if (maze._maze[i][j] == 'S')
std::cout << "S ";
else if (maze._maze[i][j] == 'E')
std::cout << "E ";
else
std::cout << " ";
}
std::cout << "$" << std::endl;
}
for (int i = 0; i < maze._width * 2 + 2; i++)
std::cout << "$";
std::cout << std::endl;
}
bool Maze2D::isSolved()
{
int counter = 0;
for (int i = 0; i < _height; i++)
{
for (int j = 0; j < _width; j++)
{
if (_maze[i][j] == '*')
{
counter++;
break;
}
}
}
if (counter > 0)
return true;
return false;
}