-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
284 lines (239 loc) · 9.89 KB
/
main.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
#include <iostream>
#include <stack>
#include <cstdlib> // atoi
#include <iomanip>
#include "Maze.h"
#include "MazeDefinitions.h"
#include "PathFinder.h"
/**
* Demo of a PathFinder implementation.
*
* Do not use a left/right wall following algorithm, as most
* Micromouse mazes are designed for such algorithms to fail.
*/
class LeftWallFollower : public PathFinder {
public:
LeftWallFollower(bool shouldPause = false) : pause(shouldPause) {
shouldGoForward = false;
visitedStart = false;
}
MouseMovement nextMovement(unsigned x, unsigned y, const Maze &maze) {
const bool frontWall = maze.wallInFront();
const bool leftWall = maze.wallOnLeft();
// Pause at each cell if the user requests it.
// It allows for better viewing on command line.
if(pause) {
std::cout << "Hit enter to continue..." << std::endl;
std::cin.ignore(10000, '\n');
std::cin.clear();
}
std::cout << maze.draw(5) << std::endl << std::endl;
// If we somehow miraculously hit the center
// of the maze, just terminate and celebrate!
if(isAtCenter(x, y)) {
std::cout << "Found center! Good enough for the demo, won't try to get back." << std::endl;
return Finish;
}
// If we hit the start of the maze a second time, then
// we couldn't find the center and never will...
if(x == 0 && y == 0) {
if(visitedStart) {
std::cout << "Unable to find center, giving up." << std::endl;
return Finish;
} else {
visitedStart = true;
}
}
// If we have just turned left, we should take that path!
if(!frontWall && shouldGoForward) {
shouldGoForward = false;
return MoveForward;
}
// As long as nothing is in front and we have
// a wall to our left, keep going forward!
if(!frontWall && leftWall) {
shouldGoForward = false;
return MoveForward;
}
// If our forward and left paths are blocked
// we should try going to the right!
if(frontWall && leftWall) {
shouldGoForward = false;
return TurnClockwise;
}
// Lastly, if there is no left wall we should take that path!
if(!leftWall) {
shouldGoForward = true;
return TurnCounterClockwise;
}
// If we get stuck somehow, just terminate.
std::cout << "Got stuck..." << std::endl;
return Finish;
}
protected:
// Helps us determine that we should go forward if we have just turned left.
bool shouldGoForward;
// Helps us determine if we've made a loop around the maze without finding the center.
bool visitedStart;
// Indicates we should pause before moving to next cell.
// Useful for command line usage.
const bool pause;
bool isAtCenter(unsigned x, unsigned y) const {
unsigned midpoint = MazeDefinitions::MAZE_LEN / 2;
if(MazeDefinitions::MAZE_LEN % 2 != 0) {
return x == midpoint && y == midpoint;
}
return (x == midpoint && y == midpoint ) ||
(x == midpoint - 1 && y == midpoint ) ||
(x == midpoint && y == midpoint - 1) ||
(x == midpoint - 1 && y == midpoint - 1);
}
};
struct Coord {
Coord(unsigned x, unsigned y, unsigned M) {
this->x = x;
this->y = y;
this->M = M;
}
unsigned x;
unsigned y;
unsigned M;
};
class FloodFill : public PathFinder {
public:
FloodFill(bool shouldPause = false) : pause(shouldPause) {
shouldGoForward = false;
visitedStart = false;
int len = MazeDefinitions::MAZE_LEN;
int mid = (len-1)/2;
bool isOdd = len % 2 ? true : false;
for(int col = 0; col < len; col++) {
for(int row = 0; row < len; row++) {
if (isOdd) manhattan[row][col] = abs(row-mid) + abs(col-mid);
else {
if (row <= mid) manhattan[row][col] += abs(row-mid);
else manhattan[row][col] += abs(row-mid-1);
if (col <= mid) manhattan[row][col] += abs(col-mid);
else manhattan[row][col] += abs(col-mid-1);
}
}
}
}
MouseMovement nextMovement(unsigned x, unsigned y, const Maze &maze) {
const bool frontWall = maze.wallInFront();
const bool leftWall = maze.wallOnLeft();
//When none of the neighboring cells has a lower distance than the current cell.
std::stack<Coord> s;//Stack of points to be processed (can also use queue)
s.push(Coord(x, y, manhattan[x][y]));
// while (!s.empty()) {
// Coord cur = s.top();
// if cur.distance == 0, continue //don’t want to process the end goal
// min_distance = infinity //placeholder/”invalid” distance
// for each neighboring cell of cur:
// if no wall between cur and neighbor:
// if neighbor.distance < min_distance:
// min_distance = neighbor.distance
// if min_distance == infinity: //something went wrong. terminate
// continue
// if cur.distance > min_distance: //everything is fine, move on
// Continue
// //if cur.distance <= min_distance we reach this point
// cur.distance = min_distance + 1 //new minimum distance
// push every neighbor onto stack
// }
// Pause at each cell if the user requests it.
// It allows for better viewing on command line.
if(pause) {
std::cout << "Hit enter to continue..." << std::endl;
std::cin.ignore(10000, '\n');
std::cin.clear();
}
std::cout << maze.draw(5) << std::endl << std::endl;
// If we somehow miraculously hit the center
// of the maze, just terminate and celebrate!
if(isAtCenter(x, y)) {
std::cout << "Found center! Good enough for the demo, won't try to get back." << std::endl;
return Finish;
}
// If we hit the start of the maze a second time, then
// we couldn't find the center and never will...
if(x == 0 && y == 0) {
if(visitedStart) {
std::cout << "Unable to find center, giving up." << std::endl;
return Finish;
} else {
visitedStart = true;
}
}
// If we have just turned left, we should take that path!
if(!frontWall && shouldGoForward) {
shouldGoForward = false;
return MoveForward;
}
// As long as nothing is in front and we have
// a wall to our left, keep going forward!
if(!frontWall && leftWall) {
shouldGoForward = false;
return MoveForward;
}
// If our forward and left paths are blocked
// we should try going to the right!
if(frontWall && leftWall) {
shouldGoForward = false;
return TurnClockwise;
}
// Lastly, if there is no left wall we should take that path!
if(!leftWall) {
shouldGoForward = true;
return TurnCounterClockwise;
}
// If we get stuck somehow, just terminate.
std::cout << "Got stuck..." << std::endl;
return Finish;
}
protected:
// Helps us determine that we should go forward if we have just turned left.
bool shouldGoForward;
// Helps us determine if we've made a loop around the maze without finding the center.
bool visitedStart;
// Indicates we should pause before moving to next cell.
// Useful for command line usage.
const bool pause;
int manhattan[MazeDefinitions::MAZE_LEN][MazeDefinitions::MAZE_LEN] = {0};
bool isAtCenter(unsigned x, unsigned y) const {
unsigned midpoint = MazeDefinitions::MAZE_LEN / 2;
if(MazeDefinitions::MAZE_LEN % 2 != 0) {
return x == midpoint && y == midpoint;
}
return (x == midpoint && y == midpoint ) ||
(x == midpoint - 1 && y == midpoint ) ||
(x == midpoint && y == midpoint - 1) ||
(x == midpoint - 1 && y == midpoint - 1);
}
};
int main(int argc, char * argv[]) {
MazeDefinitions::MazeEncodingName mazeName = MazeDefinitions::MAZE_CAMM_2012;
bool pause = true;
// Since Windows does not support getopt directly, we will
// have to parse the command line arguments ourselves.
// Skip the program name, start with argument index 1
for(int i = 1; i < argc; i++) {
if(strcmp(argv[i], "-m") == 0 && i+1 < argc) {
int mazeOption = atoi(argv[++i]);
if(mazeOption < MazeDefinitions::MAZE_NAME_MAX && mazeOption > 0) {
mazeName = (MazeDefinitions::MazeEncodingName)mazeOption;
}
} else if(strcmp(argv[i], "-p") == 0) {
pause = true;
} else {
std::cout << "Usage: " << argv[0] << " [-m N] [-p]" << std::endl;
std::cout << "\t-m N will load the maze corresponding to N, or 0 if invalid N or missing option" << std::endl;
std::cout << "\t-p will wait for a newline in between cell traversals" << std::endl;
return -1;
}
}
FloodFill leftWallFollower(pause);
Maze maze(mazeName, &leftWallFollower);
std::cout << maze.draw(5) << std::endl << std::endl;
maze.start();
}