-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.h
172 lines (160 loc) · 3.65 KB
/
Maze.h
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
#ifndef MAZE_H
#define MAZE_H
#include <sstream>
#include <iomanip>
#include <iostream>
#include "MazeInterface.h"
enum cells { OPEN, BLOCKED, VISITED, EXIT, LEFTWARDS, RIGHTWARDS, UPWARDS, DOWNWARDS, OUTWARDS, INWARDS };
////////POSSIBLY CHANGE NAMES/////////
#define LEFT h, w - 1, d
#define RIGHT h, w + 1, d
#define UP h - 1, w, d
#define DOWN h + 1, w, d
#define OUT h, w, d - 1
#define IN h, w, d + 1
class Maze :
public MazeInterface {
private:
size_t height = 0;
size_t width = 0;
size_t depth = 0;
size_t*** maze = 0;
size_t hCounter = 0;
size_t wCounter = 0;
size_t dCounter = 0;
public:
Maze(size_t h, size_t w, size_t d, std::string s) {
height = h;
width = w;
depth = d;
std::stringstream ss(s);
maze = new size_t**[depth];
for (size_t i = 0; i < depth; i++) {
maze[i] = new size_t*[height];
for (size_t j = 0; j < height; j++) {
maze[i][j] = new size_t[width];
for (size_t k = 0; k < width; k++) {
ss >> maze[i][j][k];
}
}
}
}
~Maze() {
for (size_t i = 0; i < depth; i++) {
for (size_t j = 0; j < height; j++) {
delete[] maze[i][j];
}
delete[] maze[i];
}
delete[] maze;
}
/** Set maze value
@parm height
@parm width
@parm layer
@parm value
*/
void setValue(int height, int width, int layer, int value) {
maze[layer][height][width] = value;
}
/** Solve maze
@return true if solveable, else false
*/
bool find_maze_path() {
return solveMaze();
}
bool solveMaze(size_t h = 0, size_t w = 0, size_t d = 0) {
//3 base cases
if ((h < 0) || (h >= height) || (w < 0) || (w >= width) || (d < 0) || (d >= depth)) {
return false;
}
if (maze[d][h][w] != OPEN) {
return false;
}
if ((h == height - 1) && (w == width - 1) && (d == depth - 1)) {
maze[d][h][w] = EXIT;
return true;
}
maze[d][h][w] = VISITED;
//Recursive cases
if (solveMaze(LEFT)) {
maze[d][h][w] = LEFTWARDS;
return true;
}
if (solveMaze(RIGHT)) {
maze[d][h][w] = RIGHTWARDS;
return true;
}
if (solveMaze(UP)) {
maze[d][h][w] = UPWARDS;
return true;
}
if (solveMaze(DOWN)) {
maze[d][h][w] = DOWNWARDS;
return true;
}
if (solveMaze(OUT)) {
maze[d][h][w] = OUTWARDS;
return true;
}
if (solveMaze(IN)) {
maze[d][h][w] = INWARDS;
return true;
}
maze[d][h][w] = VISITED;
return false;
}
/** Output maze (same order as input maze)
@return string of 3D layers
*/
std::string toString() const {
std::ostringstream oss;
for (size_t i = 0; i < depth; i++) {
oss << "Layer " << i + 1;
for (size_t j = 0; j < height; j++) {
oss << std::endl;
for (size_t k = 0; k < width; k++) {
switch (maze[i][j][k]) {
case OPEN:
oss << std::setw(2) << '_';
break;
case BLOCKED:
oss << std::setw(2) << 'X';
break;
case VISITED:
oss << std::setw(2) << '*';
break;
case EXIT:
oss << std::setw(2) << 'E';
break;
case LEFTWARDS:
oss << std::setw(2) << 'L';
break;
case RIGHTWARDS:
oss << std::setw(2) << 'R';
break;
case UPWARDS:
oss << std::setw(2) << 'U';
break;
case DOWNWARDS:
oss << std::setw(2) << 'D';
break;
case OUTWARDS:
oss << std::setw(2) << 'O';
break;
case INWARDS:
oss << std::setw(2) << 'I';
break;
}
}
}
oss << std::endl;
}
return oss.str();
}
friend std::ostream& operator<<(std::ostream& oss, const Maze& maze) {
oss << maze.toString();
return oss;
}
};
#endif // MAZE_H