-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
99 lines (86 loc) · 2.65 KB
/
Maze.cpp
File metadata and controls
99 lines (86 loc) · 2.65 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
//Relates to the recursive function
#define LEFT c, r - 1, l
#define RIGHT c, r + 1, l
#define UP c - 1, r, l
#define DOWN c + 1, r, l
#define OUT c, r, l - 1
#define IN c, r, l + 1
#include "Maze.h"
/** Output maze (same order as input maze)
@return string of 2D layers
*/
std::string Maze::toString() const {
std::ostringstream os;
os << "Solve Maze:" << std::endl;
os << printPlainMaze() << std::endl;
if (maze[height - 1][width - 1][numLayers - 1] != EXIT) {
os << "No Solution Exists!";
}
else {
os << printSolvedMaze();
}
return os.str();
}
/** Recursive function to find maze path */
bool Maze::find_maze_path(int c, int r, int l) {
if (c < 0 || c >= height || r < 0 || r >= width || l < 0 || l >= numLayers ) { return false; }
if (maze[c][r][l] != OPEN) { return false; }
if ((c == height - 1) && (r == width - 1) && (l == numLayers - 1)) {
maze[c][r][l] = EXIT;
return true;
}
maze[c][r][l] = L;
if (find_maze_path(LEFT)) { return true; }
maze[c][r][l] = R;
if (find_maze_path(RIGHT)) { return true; }
maze[c][r][l] = U;
if (find_maze_path(UP)) { return true; }
maze[c][r][l] = D;
if (find_maze_path(DOWN)) { return true; }
maze[c][r][l] = O;
if (find_maze_path(OUT)) { return true; }
maze[c][r][l] = I;
if (find_maze_path(IN)) { return true; }
maze[c][r][l] = VISITED;
return false;
}
/** Returns original maze */
std::string Maze::printPlainMaze() const {
std::ostringstream os;
for (int k = 0; k < numLayers; k++) {
os << "Layer " << k + 1 << std::endl;
for (int i = 0; i < height; i++) {
os << " ";
for (int j = 0; j < width; j++) {
(maze[i][j][k] != BLOCKED) ? os << '_' << " " : os << 'X' << " ";
}
os << std::endl;
}
}
os << std::endl;
return os.str();
}
/** Returns solved maze */
std::string Maze::printSolvedMaze() const {
std::ostringstream os;
os << "Solution:" << std::endl;
for (int k = 0; k < numLayers; k++) {
os << "Layer " << k + 1 << std::endl;
for (int i = 0; i < height; i++) {
os << " ";
for (int j = 0; j < width; j++) {
if (maze[i][j][k] == BLOCKED) {
os << 'X' << " ";
}
else if (maze[i][j][k] == VISITED || maze[i][j][k] == OPEN) {
os << '_' << " ";
}
else {
os << (char)maze[i][j][k] << " ";
}
}
os << std::endl;
}
}
return os.str();
}