-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cpp
521 lines (492 loc) · 11.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* Gregoire DUCHARME */
/* Yixuan Zhang */
/* 23/10/2016 */
#include "Maze.hh"
/* check if Cell is not and edge */
bool Maze::checkCell(Cell cell)
{
int xA, xB, yA, yB;
/* Get the minimum value for each position so that the "if" condition is easier */
for (unsigned int i = 0; i < _countEdges; i++) {
if (_edges[i].getxA() < _edges[i].getxB()) {
xA = _edges[i].getxA();
xB = _edges[i].getxB();
}
else {
xA = _edges[i].getxB();
xB = _edges[i].getxA();
}
if (_edges[i].getyA() < _edges[i].getyB()) {
yA = _edges[i].getyA();
yB = _edges[i].getyB();
}
else {
yA = _edges[i].getyB();
yB = _edges[i].getyA();
}
if (xA * 2 <= cell.getX() && xB * 2 >= cell.getX() &&
yA * 2 <= cell.getY() && yB * 2 >= cell.getY())
return false;
}
return true;
}
bool Maze::getCells(std::vector<Cell> &cells, std::vector<Cell> path)
{
int index = 0;
int deepness = path.back().getDeepness() + 1;
Cell tmp;
/* Check if the surrounding cells are walls and have been visited in the current path */
if (path.back().getX() > 0) {
tmp = Cell(path.back().getX() - 1, path.back().getY(), index, deepness);
if (tmp.isNotIn(path) && checkCell(tmp)) {
tmp.setIndex(index++);
cells.push_back(tmp);
}
}
if (path.back().getX() < _width * 2 - 1) {
tmp = Cell(path.back().getX() + 1, path.back().getY(), index, deepness);
if (tmp.isNotIn(path) && checkCell(tmp)) {
tmp.setIndex(index++);
cells.push_back(tmp);
}
}
if (path.back().getY() > 0) {
tmp = Cell(path.back().getX(), path.back().getY() - 1, index, deepness);
if (tmp.isNotIn(path) && checkCell(tmp)) {
tmp.setIndex(index++);
cells.push_back(tmp);
}
}
if (path.back().getY() < _height * 2 - 1) {
tmp = Cell(path.back().getX(), path.back().getY() + 1, index, deepness);
if (tmp.isNotIn(path) && checkCell(tmp)) {
tmp.setIndex(index++);
cells.push_back(tmp);
}
}
/* Dead end */
if (cells.size() == 0)
return false;
return true;
}
/* Copy the maze svg file and add the solution in red line */
void Maze::saveSolvedToSvg(std::vector<Cell> path)
{
std::ifstream file;
std::ofstream file_solved;
std::string line;
file.open(_filename.c_str());
file_solved.open("solved_" + _filename);
if (file.is_open() && file_solved.is_open()) {
while (getline(file, line)) {
if (line.compare("</svg>"))
file_solved << line << std::endl;
}
for (unsigned int i; i + 1 < path.size(); i++) {
file_solved << &path[i] << std::endl;
}
file_solved << "</svg>" << std::endl;
file_solved.close();
file.close();
}
}
bool Maze::solvePM()
{
return solvePB();
}
bool Maze::solvePE()
{
return solvePB();
}
/* Solving breadth first */
bool Maze::solvePB()
{
std::vector<std::vector<Cell>> pathes;
std::vector<std::vector<Cell>> cells;
std::vector<Cell> tmp, tmp_to_push;
unsigned int i = 0, k = 0;
int j = -1;
bool check = true;
pathes.push_back(std::vector<Cell>());
pathes.back().push_back(Cell(1, 1));
while (check) {
check = false;
i = 0;
/* Checking possible path for each path instanciated */
while (i < pathes.size()) {
cells.push_back(std::vector<Cell>());
getCells(cells[++j], pathes[i]);
k = 0;
tmp = pathes[i];
/* Then instanciated all possible path */
while (k < cells[j].size()) {
tmp_to_push = tmp;
if (k == 0)
pathes[i].push_back(cells[j][k]);
else {
tmp_to_push.push_back(cells[j][k]);
pathes.push_back(tmp_to_push);
}
/* Bottom right corner */
if (cells[j][k].getX() == _width * 2 - 1 &&
cells[j][k].getY() == _height * 2 - 1) {
if (_filename.compare("")) {
tmp.push_back(cells[j][k]);
saveSolvedToSvg(tmp);
}
return true;
}
check = true;
k++;
}
i++;
}
}
return false;
}
/* Solving depth first */
bool Maze::solvePD()
{
std::vector<Cell> path;
std::vector<std::vector<Cell>> cells;
int i = 0, j = -1;
bool check = true;
unsigned int index = 0;
path.push_back(Cell(1, 1));
/* infinite loop */
/* Stop if all path have been explored and couldn't reach to bottom right corner */
while (1) {
/* Check the cells around current position */
if (check) {
cells.push_back(std::vector<Cell>());
getCells(cells[++j], path);
index = 0;
}
if (cells[j].size() > index) {
path.push_back(cells[j][index]);
check = true;
i++;
}
else if (i >= 0) {
index = path[i--].getIndex() + 1;
cells.pop_back();
j--;
path.pop_back();
check = false;
}
else
return false;
/* Bottom right corner */
if (path.back().getX() == _width * 2 - 1 && path.back().getY() == _height * 2 - 1) {
if (_filename.compare("")) {
saveSolvedToSvg(path);
}
return true;
}
if (j == -1 || i == -1)
return false;
}
return false;
}
/* Dispatch solving methog depending on solving type */
bool Maze::solveMaze(solving_type solving)
{
switch (solving) {
case PM:
return solvePM();
break;
case PE:
return solvePE();
break;
case PB:
return solvePB();
break;
case PD:
return solvePD();
break;
default:
return false;
break;
}
}
/* Check if the 2D map of the maze still contains unexplored parts */
bool Maze::checkMap(std::vector<std::vector<int>> map) const
{
int x = 0, y = 0;
while (y < _height) {
x = 0;
while (x < _width) {
if (map[x][y] == false) {
return false;
}
x++;
}
y++;
}
return true;
}
/* Initialize the values of the maze class and check potential error */
bool Maze::initValues(int width, int height, unsigned int countEdges)
{
_width = width;
_height = height;
_countEdges = countEdges;
if (!_width || !_height) {
std::cerr << "Wrong size for new maze" << std::endl;
return false;
}
return true;
}
void Maze::createEdgeEller(int y, bool way, std::vector<std::vector<int>> map)
{
int x = 0;
if (way == HORIZONTAL) {
int value = map[x][y];
/* Line to process */
while (x < _width) {
if (map[x][y] != value) {
value = map[x][y];
_countEdges++;
_edges.push_back(Edge(x, y, x, y + 1));
}
if (y > 0 && map[x][y] != map[x][y - 1]) {
_countEdges++;
_edges.push_back(Edge(x, y, x + 1, y));
}
x++;
}
}
else /* way == VERTICAL */ {
/*Column to process */
while (x < _width) {
if (map[x][y] == 0) {
_countEdges++;
_edges.push_back(Edge(x, y, x + 1, y));
}
x++;
}
}
}
/* Filling the 2D map and instanciating the edges */
void Maze::fillMapEller(std::vector<std::vector<int>> map,
std::mt19937 generator)
{
int x = 0, y = 0, val = 0, previousValue = 0, minValue = 0;
bool check = false;
while (y < _height) {
/* Init each cell to a value */
x = 0;
checkMap(map);
minValue = val;
while (x < _width) {
if (!map[x][y])
map[x][y] = ++val;
x++;
}
checkMap(map);
/* Merge randomly cells */
x = 1;
while (x < _width) {
if (generator() % 2 == 0) {
if (map[x][y] > minValue)
map[x][y] = map[x - 1][y];
else { /* Merging a cell already set */
previousValue = map[x - 1][y];
for (int tmpX = x - 1; tmpX >= 0 && map[tmpX][y] == previousValue ; tmpX--) {
map[tmpX][y] = map[x][y];
}
}
}
x++;
}
checkMap(map);
createEdgeEller(y, HORIZONTAL, map);
y++;
check = false;
/* Add vertical connexion*/
if (y < _height) {
while (!check) {
x = 0;
while (x < _width) {
if ((x == 0 || map[x - 1][y] != map[x][y - 1]) &&
generator() % 2 == 0) {
map[x][y] = map[x][y - 1];
}
x++;
}
x = 0;
check = false;
previousValue = map[x][y - 1];
while (x < _width) {
if (map[x][y] == previousValue)
check = true;
if (map[x][y - 1] != previousValue && check == false)
break;
else if (map[x][y - 1] != previousValue && check == true)
{
previousValue = map[x][y - 1];
if (x + 1 < _width)
check = false;
}
else
x++;
}
checkMap(map);
}
createEdgeEller(y, VERTICAL, map);
}
}
}
void Maze::fillMapAldousBroder(std::vector<std::vector<int>> map,
std::mt19937 generator)
{
int x = generator() % _width, y = generator() % _height, previousX, previousY, way;
map[x][y] = true;
while (!checkMap(map)) {
previousX = x;
previousY = y;
way = generator() % 4;
/* Randomize the way to explore */
switch (way) {
case LEFT:
if (x > 0)
x--;
break;
case RIGHT:
if (x < _width)
x++;
break;
case UP:
if (y > 0)
y--;
break;
case DOWN:
if (y < _height)
y++;
break;
}
if (map[x][y] == false) {
_countEdges++;
_edges.push_back(Edge(previousX, previousY, x, y));
map[x][y] = true;
}
}
}
/* If no maze to load is specified then create a new one */
bool Maze::loadNewMaze(int seed, int width, int height, algorithm_type type)
{
if (initValues(width, height) == false) {
return false;
}
std::vector<std::vector<int>> map;
map.resize(_width + 1);
int x = 0, y = 0;
while (x <= _width) {
y = 0;
map[x].resize(_height + 1);
while (y <= _height) {
map[x][y] = 0;
y++;
}
x++;
}
std::mt19937 generator(seed);
switch (type)
{
case ALDOUS_BRODER:
fillMapAldousBroder(map, generator);
break;
case ELLER:
fillMapEller(map, generator);
break;
}
return true;
}
bool Maze::loadFromBinaryFile(std::string filename)
{
std::ifstream file(filename);
int val;
std::vector<int> bytes;
if (file.is_open())
{
while (file.read((char*)&val, sizeof(int))) {
bytes.push_back(val);
}
if ((bytes.size() + 1) % 4 != 0) {
std::cerr << "File length incorrect" << std::endl;
return false;
}
}
else {
std::cerr << "Couldn't open file" << std::endl;
return false;
}
file.close();
if (initValues(bytes[0], bytes[1], bytes[2]) == false) {
return false;
}
for(unsigned int i = 3; i < bytes.size(); i += 4) {
_edges.push_back(Edge(bytes[i], bytes[i+1], bytes[i+2], bytes[i+3]));
}
if (_countEdges != _edges.size()) {
std::cerr << "Binary file incorrect" << std::endl;
}
return true;
}
bool Maze::saveToBinaryFile(std::string filename) const
{
std::ofstream file;
file.open(filename.c_str());
if (file.is_open()) {
file.write((char*)&_width, sizeof(int));
file.write((char*)&_height, sizeof(int));
file.write((char*)&_countEdges, sizeof(int));
int xA, yA, xB, yB;
for (unsigned int i = 0; i < _edges.size(); i++) {
xA = _edges[i].getxA();
yA = _edges[i].getyA();
xB = _edges[i].getxB();
yB = _edges[i].getyB();
file.write((char*)&xA, sizeof(int));
file.write((char*)&yA, sizeof(int));
file.write((char*)&xB, sizeof(int));
file.write((char*)&yB, sizeof(int));
}
file.close();
return true;
}
std::cerr << "Couldn't open file" << std::endl;
return false;
}
bool Maze::saveToSvgFile(std::string filename)
{
std::ofstream file;
file.open(filename.c_str());
if (file.is_open()) {
file << "<svg width=\""<< _width * SIZE_RATIO;
file <<"\" height=\""<< _height * SIZE_RATIO;
file << "\" xmlns=\"http://www.w3.org/2000/svg\">" << std::endl;
file << "<rect x=\"0\" y =\"0\" width=\""<< _width * SIZE_RATIO;
file << "\" height=\"" << _height * SIZE_RATIO;
file << "\" style=\"fill:black\" />" << std::endl;
for (unsigned int i = 0; i < _edges.size(); i++)
{
/* Operator oveload */
file << _edges[i] << std::endl;
}
file << "</svg>" << std::endl;
file.close();
_filename = filename;
return true;
}
std::cerr << "Couldn't open file" << std::endl;
return false;
}
/* Checking value from the maze class */
bool Maze::isValid() const
{
if (!_width || !_height || !_countEdges) {
return false;
}
return true;
}