-
Notifications
You must be signed in to change notification settings - Fork 1
/
mazesearcher.cpp
205 lines (181 loc) · 5.55 KB
/
mazesearcher.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
/************************************************************************
*
* Copyright (C) 2017 by Peter Harrison. www.micromouseonline.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without l> imitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************/
#include "mazesearcher.h"
#include <cassert>
#include "maze.h"
#include "mazeprinter.h"
MazeSearcher::MazeSearcher()
: mLocation(0), mHeading(NORTH), mMap(nullptr), mRealMaze(nullptr), mVerbose(false), mSearchMethod(SEARCH_NORMAL) {
mMap = new Maze(16);
mMap->setFloodType(Maze::MANHATTAN_FLOOD);
}
MazeSearcher::~MazeSearcher() {
delete mMap;
}
bool MazeSearcher::isVerbose() const {
return mVerbose;
}
void MazeSearcher::setVerbose(bool mVerbose) {
MazeSearcher::mVerbose = mVerbose;
}
uint16_t MazeSearcher::location() const {
return mLocation;
}
void MazeSearcher::setLocation(uint16_t location) {
MazeSearcher::mLocation = location;
}
uint8_t MazeSearcher::heading() const {
return mHeading;
}
void MazeSearcher::setHeading(uint8_t heading) {
MazeSearcher::mHeading = heading;
}
void MazeSearcher::move() {
mLocation = mMap->neighbour(mLocation, mHeading);
}
void MazeSearcher::setMapFromFileData(const uint8_t *mazeWalls, uint16_t cellCount) {
mMap->copyMazeFromFileData(mazeWalls, cellCount);
}
Maze *MazeSearcher::map() {
return mMap;
}
const Maze *MazeSearcher::realMaze() const {
return mRealMaze;
}
void MazeSearcher::setRealMaze(const Maze *maze) {
mRealMaze = maze;
mMap->setWidth(maze->width());
mMap->resetToEmptyMaze();
mMap->setGoal(maze->goal());
}
int MazeSearcher::runTo(uint16_t target) {
int steps = 0;
mMap->flood(target, OPEN_MAZE);
while (mLocation != target) {
uint8_t heading = mMap->directionToSmallest(mLocation);
if (heading == INVALID_DIRECTION) {
steps = -1;
break;
}
setHeading(heading);
move();
steps++;
if (steps > 256) {
steps = -2;
break;
}
if (isVerbose()) {
MazePrinter::printVisitedDirs(mMap);
}
}
return steps;
}
// TODO: this needs looking at.
// the returned number of steps may be inconsistent
int MazeSearcher::searchTo(uint16_t target) {
int stepCount = 0;
while (mLocation != target) {
mMap->updateMap(mLocation, mRealMaze->walls(mLocation));
uint8_t newHeading;
switch (mSearchMethod) {
case SEARCH_LEFT_WALL:
newHeading = followLeftWall();
break;
case SEARCH_RIGHT_WALL:
newHeading = followRightWall();
break;
case SEARCH_ALTERNATE:
newHeading = followAlternateWall();
break;
case SEARCH_NORMAL:
mMap->flood(target, OPEN_MAZE);
newHeading = mMap->directionToSmallest(mLocation);
break;
default:
newHeading = INVALID_DIRECTION;
break;
}
if (newHeading == INVALID_DIRECTION) {
stepCount = E_NO_ROUTE;
break;
}
setHeading(newHeading);
stepCount++;
if (stepCount > mMap->numCells()) {
stepCount = E_ROUTE_TOO_LONG;
break;
}
move();
if (isVerbose()) {
MazePrinter::printVisitedDirs(mMap);
}
}
return stepCount;
}
uint8_t MazeSearcher::followLeftWall() const {
uint8_t newHeading;
if (mMap->hasOpenExit(mLocation, Maze::leftOf(mHeading))) {
newHeading = Maze::leftOf(mHeading);
} else if (mMap->hasOpenExit(mLocation, Maze::ahead(mHeading))) {
newHeading = Maze::ahead(mHeading);
} else if (mMap->hasOpenExit(mLocation, Maze::rightOf(mHeading))) {
newHeading = Maze::rightOf(mHeading);
} else {
newHeading = Maze::behind(mHeading);
}
return newHeading;
}
uint8_t MazeSearcher::followRightWall() const {
uint8_t newHeading;
if (mMap->hasOpenExit(mLocation, Maze::rightOf(mHeading))) {
newHeading = Maze::rightOf(mHeading);
} else if (mMap->hasOpenExit(mLocation, Maze::ahead(mHeading))) {
newHeading = Maze::ahead(mHeading);
} else if (mMap->hasOpenExit(mLocation, Maze::leftOf(mHeading))) {
newHeading = Maze::leftOf(mHeading);
} else {
newHeading = Maze::behind(mHeading);
}
return newHeading;
}
uint8_t MazeSearcher::followAlternateWall() const {
static bool useLeft = true;
uint8_t newHeading;
newHeading = useLeft ? followLeftWall() : followRightWall();
useLeft = !useLeft;
return newHeading;
}
void MazeSearcher::setSearchMethod(int mSearchMethod) {
MazeSearcher::mSearchMethod = mSearchMethod;
}
void MazeSearcher::turnRight() {
mHeading = Maze::rightOf(mHeading);
}
void MazeSearcher::turnLeft() {
mHeading = Maze::leftOf(mHeading);
}
void MazeSearcher::turnAround() {
mHeading = Maze::behind(mHeading);
}