-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
538 lines (467 loc) · 15.4 KB
/
Maze.cpp
File metadata and controls
538 lines (467 loc) · 15.4 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include "Maze.h"
#include <algorithm>
#include <chrono>
#include <queue>
/**
* Default constructor - creates a 10x10 maze
*/
Maze::Maze() : width(10), height(10), rng(std::chrono::steady_clock::now().time_since_epoch().count()) {
grid.resize(height, std::vector<Cell>(width));
// Initialize grid with coordinates
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid[y][x] = Cell(x, y);
}
}
}
/**
* Constructor with custom dimensions
*/
Maze::Maze(int w, int h) : width(w), height(h), rng(std::chrono::steady_clock::now().time_since_epoch().count()) {
grid.resize(height, std::vector<Cell>(width));
// Initialize grid with coordinates
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid[y][x] = Cell(x, y);
}
}
}
/**
* Constructor with custom dimensions and seed
*/
Maze::Maze(int w, int h, unsigned int seed) : width(w), height(h), rng(seed) {
grid.resize(height, std::vector<Cell>(width));
// Initialize grid with coordinates
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid[y][x] = Cell(x, y);
}
}
}
/**
* Get cell at specific coordinates
*/
Cell* Maze::getCell(int x, int y) {
if (x >= 0 && x < width && y >= 0 && y < height) {
return &grid[y][x];
}
return nullptr;
}
/**
* Get all unvisited neighbors of a cell
*/
std::vector<Cell*> Maze::getUnvisitedNeighbors(Cell* cell) {
std::vector<Cell*> neighbors;
if (!cell) return neighbors;
// Check all four directions
Cell* top = getCell(cell->x, cell->y - 1);
Cell* right = getCell(cell->x + 1, cell->y);
Cell* bottom = getCell(cell->x, cell->y + 1);
Cell* left = getCell(cell->x - 1, cell->y);
if (top && !top->visited) neighbors.push_back(top);
if (right && !right->visited) neighbors.push_back(right);
if (bottom && !bottom->visited) neighbors.push_back(bottom);
if (left && !left->visited) neighbors.push_back(left);
return neighbors;
}
/**
* Get direction from one cell to another
*/
Direction Maze::getDirection(Cell* from, Cell* to) {
if (to->y < from->y) return TOP;
if (to->x > from->x) return RIGHT;
if (to->y > from->y) return BOTTOM;
return LEFT;
}
/**
* Remove wall between two adjacent cells
*/
void Maze::removeWall(Cell* current, Cell* neighbor) {
if (!current || !neighbor) return;
Direction dir = getDirection(current, neighbor);
// Remove wall from current cell
current->walls[dir] = false;
// Remove corresponding wall from neighbor
switch (dir) {
case TOP:
neighbor->walls[BOTTOM] = false;
break;
case RIGHT:
neighbor->walls[LEFT] = false;
break;
case BOTTOM:
neighbor->walls[TOP] = false;
break;
case LEFT:
neighbor->walls[RIGHT] = false;
break;
}
}
/**
* Main maze generation method (uses iterative approach by default)
*/
void Maze::generateMaze() {
generateMazeIterative();
}
/**
* Iterative maze generation using stack-based depth-first search
*/
void Maze::generateMazeIterative() {
resetMaze();
// Start from top-left corner
Cell* currentCell = getCell(0, 0);
currentCell->visited = true;
cellStack.push(currentCell);
while (!cellStack.empty()) {
currentCell = cellStack.top();
// Get unvisited neighbors
std::vector<Cell*> neighbors = getUnvisitedNeighbors(currentCell);
if (!neighbors.empty()) {
// Choose random neighbor
std::uniform_int_distribution<int> dist(0, neighbors.size() - 1);
Cell* chosenNeighbor = neighbors[dist(rng)];
// Remove wall between current and chosen neighbor
removeWall(currentCell, chosenNeighbor);
// Mark chosen neighbor as visited and push to stack
chosenNeighbor->visited = true;
cellStack.push(chosenNeighbor);
} else {
// Backtrack - pop from stack
cellStack.pop();
}
}
}
/**
* Recursive maze generation algorithm
*/
void Maze::generateMazeRecursive(int x, int y) {
// Base case: if starting a new maze
static bool isNewMaze = true;
if (isNewMaze) {
resetMaze();
isNewMaze = false;
}
Cell* currentCell = getCell(x, y);
if (!currentCell) {
isNewMaze = true; // Reset for next call
return;
}
currentCell->visited = true;
// Get all unvisited neighbors
std::vector<Cell*> neighbors = getUnvisitedNeighbors(currentCell);
// Shuffle neighbors for randomness
std::shuffle(neighbors.begin(), neighbors.end(), rng);
// Recursively visit each unvisited neighbor
for (Cell* neighbor : neighbors) {
if (!neighbor->visited) {
removeWall(currentCell, neighbor);
generateMazeRecursive(neighbor->x, neighbor->y);
}
}
// Check if we're done (all cells visited)
bool allVisited = true;
for (int cy = 0; cy < height && allVisited; cy++) {
for (int cx = 0; cx < width && allVisited; cx++) {
if (!grid[cy][cx].visited) {
allVisited = false;
}
}
}
if (allVisited) {
isNewMaze = true; // Reset for next call
}
}
/**
* Reset maze to initial state
*/
void Maze::resetMaze() {
// Clear stack
while (!cellStack.empty()) {
cellStack.pop();
}
// Reset all cells
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid[y][x].visited = false;
for (int i = 0; i < 4; i++) {
grid[y][x].walls[i] = true;
}
}
}
}
/**
* Print maze in a simple format
*/
void Maze::printMaze() const {
std::cout << "\n=== MAZE (" << width << "x" << height << ") ===\n";
// Top border
std::cout << "┌";
for (int x = 0; x < width; x++) {
std::cout << "──";
if (x < width - 1) std::cout << "┬";
}
std::cout << "┐\n";
// Maze content
for (int y = 0; y < height; y++) {
// Vertical walls and spaces
std::cout << "│";
for (int x = 0; x < width; x++) {
std::cout << " ";
if (x < width - 1) {
std::cout << (grid[y][x].walls[RIGHT] ? "│" : " ");
}
}
std::cout << "│\n";
// Horizontal walls (except for last row)
if (y < height - 1) {
std::cout << "├";
for (int x = 0; x < width; x++) {
std::cout << (grid[y][x].walls[BOTTOM] ? "──" : " ");
if (x < width - 1) {
// Corner character
bool hasBottom = grid[y][x].walls[BOTTOM];
bool hasRight = grid[y][x].walls[RIGHT];
bool hasBottomRight = grid[y][x + 1].walls[BOTTOM];
bool hasBottomLeft = grid[y + 1][x].walls[RIGHT];
if (hasBottom && hasRight && hasBottomRight && hasBottomLeft) std::cout << "┼";
else if (hasBottom && hasBottomRight) std::cout << "┬";
else if (hasRight && hasBottomLeft) std::cout << "├";
else if (hasBottom || hasBottomRight) std::cout << "─";
else if (hasRight || hasBottomLeft) std::cout << "│";
else std::cout << " ";
}
}
std::cout << "┤\n";
}
}
// Bottom border
std::cout << "└";
for (int x = 0; x < width; x++) {
std::cout << "──";
if (x < width - 1) std::cout << "┴";
}
std::cout << "┘\n";
}
/**
* Print maze in ASCII format (alternative visualization)
*/
void Maze::printMazeASCII() const {
std::cout << "\n=== ASCII MAZE (" << width << "x" << height << ") ===\n";
// Top border
for (int x = 0; x < width * 2 + 1; x++) {
std::cout << "#";
}
std::cout << "\n";
for (int y = 0; y < height; y++) {
// Left border
std::cout << "#";
// Cell content and right walls
for (int x = 0; x < width; x++) {
std::cout << " "; // Cell space
std::cout << (grid[y][x].walls[RIGHT] ? "#" : " ");
}
std::cout << "\n";
// Bottom walls (except for last row)
if (y < height - 1) {
std::cout << "#";
for (int x = 0; x < width; x++) {
std::cout << (grid[y][x].walls[BOTTOM] ? "#" : " ");
std::cout << "#";
}
std::cout << "\n";
}
}
// Bottom border
for (int x = 0; x < width * 2 + 1; x++) {
std::cout << "#";
}
std::cout << "\n";
}
/**
* Print detailed maze information
*/
void Maze::printMazeDetailed() const {
std::cout << "\n=== DETAILED MAZE INFO ===\n";
std::cout << "Dimensions: " << width << "x" << height << "\n";
std::cout << "Total cells: " << width * height << "\n";
int wallCount = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int w = 0; w < 4; w++) {
if (grid[y][x].walls[w]) wallCount++;
}
}
}
std::cout << "Total walls: " << wallCount / 2 << "\n"; // Divide by 2 since walls are shared
printMaze();
}
/**
* Simple maze solver using DFS (bonus feature)
*/
bool Maze::solveMaze(int startX, int startY, int endX, int endY) {
// Set default end position if not specified
if (endX == -1) endX = width - 1;
if (endY == -1) endY = height - 1;
// Validate start and end positions
if (startX < 0 || startX >= width || startY < 0 || startY >= height) {
std::cout << "\nInvalid start position!\n";
return false;
}
if (endX < 0 || endX >= width || endY < 0 || endY >= height) {
std::cout << "\nInvalid end position!\n";
return false;
}
// Use BFS to reliably find the shortest path from start to end.
if (endX == -1) endX = width - 1;
if (endY == -1) endY = height - 1;
// Validate start and end positions
if (startX < 0 || startX >= width || startY < 0 || startY >= height) {
std::cout << "\nInvalid start position!\n";
return false;
}
if (endX < 0 || endX >= width || endY < 0 || endY >= height) {
std::cout << "\nInvalid end position!\n";
return false;
}
// Local visited and parent trackers (do not modify grid visited flags here)
std::vector<std::vector<bool>> visitedLocal(height, std::vector<bool>(width, false));
std::vector<std::vector<std::pair<int,int>>> parent(height, std::vector<std::pair<int,int>>(width, {-1,-1}));
std::queue<std::pair<int,int>> q;
q.push({startX, startY});
visitedLocal[startY][startX] = true;
bool found = false;
while (!q.empty()) {
auto [cx, cy] = q.front(); q.pop();
if (cx == endX && cy == endY) {
found = true;
break;
}
Cell* current = getCell(cx, cy);
if (!current) continue;
// Explore neighbors allowed by removed walls
// TOP
if (!current->walls[TOP]) {
int nx = cx, ny = cy - 1;
if (ny >= 0 && !visitedLocal[ny][nx]) {
visitedLocal[ny][nx] = true;
parent[ny][nx] = {cx, cy};
q.push({nx, ny});
}
}
// RIGHT
if (!current->walls[RIGHT]) {
int nx = cx + 1, ny = cy;
if (nx < width && !visitedLocal[ny][nx]) {
visitedLocal[ny][nx] = true;
parent[ny][nx] = {cx, cy};
q.push({nx, ny});
}
}
// BOTTOM
if (!current->walls[BOTTOM]) {
int nx = cx, ny = cy + 1;
if (ny < height && !visitedLocal[ny][nx]) {
visitedLocal[ny][nx] = true;
parent[ny][nx] = {cx, cy};
q.push({nx, ny});
}
}
// LEFT
if (!current->walls[LEFT]) {
int nx = cx - 1, ny = cy;
if (nx >= 0 && !visitedLocal[ny][nx]) {
visitedLocal[ny][nx] = true;
parent[ny][nx] = {cx, cy};
q.push({nx, ny});
}
}
}
if (!found) {
std::cout << "\nNo solution found: end is unreachable from start.\n";
return false;
}
// Reconstruct path length
int pathLen = 0;
int px = endX, py = endY;
while (!(px == startX && py == startY)) {
auto p = parent[py][px];
if (p.first == -1) break; // safety
px = p.first; py = p.second;
pathLen++;
}
std::cout << "\nSolution found! Shortest path length: " << pathLen << " steps\n";
return true;
}
/**
* Print solution path (placeholder implementation)
*/
void Maze::printSolution() const {
std::cout << "Solution visualization not implemented in this version.\n";
std::cout << "Use solveMaze() method to find if a solution exists.\n";
}
/**
* Check if the maze is properly connected (all cells reachable)
*/
bool Maze::isMazeConnected() {
// Reset visited flags
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
grid[y][x].visited = false;
}
}
// Use DFS to visit all reachable cells from (0,0)
std::stack<Cell*> toVisit;
Cell* start = getCell(0, 0);
if (!start) return false;
start->visited = true;
toVisit.push(start);
int visitedCount = 1;
while (!toVisit.empty()) {
Cell* current = toVisit.top();
toVisit.pop();
// Check all four directions
if (!current->walls[TOP]) {
Cell* neighbor = getCell(current->x, current->y - 1);
if (neighbor && !neighbor->visited) {
neighbor->visited = true;
toVisit.push(neighbor);
visitedCount++;
}
}
if (!current->walls[RIGHT]) {
Cell* neighbor = getCell(current->x + 1, current->y);
if (neighbor && !neighbor->visited) {
neighbor->visited = true;
toVisit.push(neighbor);
visitedCount++;
}
}
if (!current->walls[BOTTOM]) {
Cell* neighbor = getCell(current->x, current->y + 1);
if (neighbor && !neighbor->visited) {
neighbor->visited = true;
toVisit.push(neighbor);
visitedCount++;
}
}
if (!current->walls[LEFT]) {
Cell* neighbor = getCell(current->x - 1, current->y);
if (neighbor && !neighbor->visited) {
neighbor->visited = true;
toVisit.push(neighbor);
visitedCount++;
}
}
}
// All cells should be reachable in a proper maze
int totalCells = width * height;
std::cout << "\nMaze connectivity check:\n";
std::cout << "Visited cells: " << visitedCount << "/" << totalCells << "\n";
if (visitedCount == totalCells) {
std::cout << "✓ Maze is properly connected!\n";
return true;
} else {
std::cout << "✗ Maze has isolated regions! " << (totalCells - visitedCount) << " cells unreachable.\n";
return false;
}
}