-
Notifications
You must be signed in to change notification settings - Fork 1
/
maze.h
193 lines (160 loc) · 5.99 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef MAZE_H
#define MAZE_H
//#include <queue>
#include "queue.h"
#include "cell.h"
class Maze {
private:
Cell** maze;
Cell* goal;
int rows;
int cols;
queue cellQueue;
public:
// Constructor
Maze(int rows, int cols, int goalX, int goalY) : rows(rows), cols(cols), goal(nullptr) {
maze = new Cell*[rows];
for (int x = 0; x < rows; ++x) {
maze[x] = new Cell[cols];
for (int y = 0; y < cols; ++y) {
maze[x][y].setX(x);
maze[x][y].setY(y);
// Set outer walls
if (x == 0) { maze[x][y].setWestWall(true); }
if (y == 0) { maze[x][y].setSouthWall(true); }
if (x == rows-1) { maze[x][y].setEastWall(true); }
if (y == cols-1) { maze[x][y].setNorthWall(true); }
}
}
goal = &maze[goalX][goalY];
}
void floodfill() {
// 1. Set all cells except goal to “blank state”
for (int x = 0; x < rows; ++x) {
for (int y = 0; y < cols; ++y) {
maze[x][y].reset();
}
}
// 2. Set goal cell(s) value to 0 and add to queue
goal->setValue(0);
queue cellQueue;
cellQueue.push(goal);
while (!cellQueue.empty()) {
// 3.1 Take front cell in a queue "out of line" for consideration
Cell* temp = cellQueue.front();
cellQueue.pop();
// 3.2 set all blank and accessible neighbours to front cell's value + 1
if (!temp->hasNorthWall() && temp->getY() + 1 < cols) {
Cell* currentCell = &maze[temp->getX()][temp->getY() + 1];
// Check if it's blank
if (currentCell->getValue() == -1) {
currentCell->setValue(temp->getValue() + 1);
// 3.3a Add new cell to queue
cellQueue.push(currentCell);
}
}
if (!temp->hasEastWall() && temp->getX() + 1 < rows) {
Cell* currentCell = &maze[temp->getX() + 1][temp->getY()];
if (currentCell->getValue() == -1) {
currentCell->setValue(temp->getValue() + 1);
// 3.3b Add new cell to queue
cellQueue.push(currentCell);
}
}
if (!temp->hasSouthWall() && temp->getY() - 1 >= 0) {
Cell* currentCell = &maze[temp->getX()][temp->getY() - 1];
if (currentCell->getValue() == -1) {
currentCell->setValue(temp->getValue() + 1);
// 3.3c Add new cell to queue
cellQueue.push(currentCell);
}
}
if (!temp->hasWestWall() && temp->getX() - 1 >= 0) {
Cell* currentCell = &maze[temp->getX() - 1][temp->getY()];
if (currentCell->getValue() == -1) {
currentCell->setValue(temp->getValue() + 1);
// 3.3d Add new cell to queue
cellQueue.push(currentCell);
}
}
// Serial.println(temp->getValue() + temp->getX() + temp->getY());
}
}
// Add a wall to the maze and to the corrresponding adjacent cell
void recordWall(int currentX, int currentY, int dir) {
// Record north wall
if (dir == 1) {
maze[currentX][currentY].setNorthWall(true);
// Set adajent wall
if (currentY + 1 < rows) {
maze[currentX][currentY + 1].setSouthWall(true);
}
}
// Record east wall
if (dir == 2) {
maze[currentX][currentY].setEastWall(true);
// Set adajent wall
if (currentX + 1 < cols) {
maze[currentX + 1][currentY].setWestWall(true);
}
}
// Record south wall
if (dir == 3) {
maze[currentX][currentY].setSouthWall(true);
// Set adajent wall
if (currentY - 1 >= 0) {
maze[currentX][currentY - 1].setNorthWall(true);
}
}
// Record west wall
if (dir == 4) {
maze[currentX][currentY].setWestWall(true);
// Set adajent wall
if (currentX - 1 >= 0) {
maze[currentX - 1][currentY].setEastWall(true);
}
}
}
Cell getNorthCell(int currentX, int currentY) {
if (currentY + 1 < cols) {
return maze[currentX][currentY + 1];
}
// Return default cell (value = -1) if out of range
return Cell();
}
Cell getEastCell(int currentX, int currentY) {
if (currentX + 1 < rows) {
return maze[currentX + 1][currentY];
}
// Return default cell (value = -1) if out of range
return Cell();
}
Cell getSouthCell(int currentX, int currentY) {
if (currentY - 1 >= 0) {
return maze[currentX][currentY - 1];
}
// Return default cell (value = -1) if out of range
return Cell();
}
Cell getWestCell(int currentX, int currentY) {
if (currentX - 1 >= 0) {
return maze[currentX - 1][currentY];
}
// Return default cell (value = -1) if out of range
return Cell();
}
Cell getCurrentCell(int currentX, int currentY) {
return maze[currentX][currentY];
}
// Print out all values in maze map
// North is to the east but coordinates still make sense
void printMaze() {
for (int x = 0; x < rows; ++x) {
Serial.println("\n");
for (int y = 0; y < cols; ++y) {
Serial.print(maze[x][y].getValue() + " ");
}
}
}
};
#endif