-
Notifications
You must be signed in to change notification settings - Fork 2
/
maze.cpp
581 lines (493 loc) · 14.3 KB
/
maze.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "maze.h"
////--------------CONSTRUCTORS--------------////
// Default Maze Constructor
// PURPOSE: This will initialize all borders walls to true and middle walls
// false Every box will have a cost = max and explored = false
Maze::Maze() { clear(); }
////--------------ACCESSOR FUNCTIONS--------------////
bool Maze::inBounds(const coord &n) {
return (n.x >= 0 && n.x < 16 && n.y >= 0 && n.y < 16);
}
bool Maze::inBounds(const vector<coord> &n) {
for (int i = 0; i < n.size(); i++) {
if (!inBounds(n[i]))
return false;
}
return true;
}
grid Maze::getGrid() { return mazeWalls; }
/// @returns Wall on South edge of box
bool Maze::getSWall(const coord &n) {
if (inBounds(n))
return mazeWalls.horzWalls[n.x][n.y];
else
throw(std::range_error("error in range1"));
}
/// @returns Wall on North edge of box
bool Maze::getNWall(const coord &n) {
if (inBounds(n))
return mazeWalls.horzWalls[n.x][n.y + 1];
else
throw(std::range_error("error in range2"));
}
/// @returns Wall on West edge of box
bool Maze::getWWall(const coord &n) {
if (inBounds(n))
return mazeWalls.vertWalls[n.x][n.y];
else
throw(std::range_error("error in range3"));
}
/// @returns Wall on East edge of box
bool Maze::getEWall(const coord &n) {
if (inBounds(n))
return mazeWalls.vertWalls[n.x + 1][n.y];
else
throw(std::range_error("error in range4"));
}
/// @returns NESW walls in struct
bw Maze::getWalls(const coord &n) {
if (!inBounds(n))
throw(std::range_error("error in range5"));
bw wall;
wall.nWall = getNWall(n);
wall.sWall = getSWall(n);
wall.wWall = getWWall(n);
wall.eWall = getEWall(n);
return wall;
}
/// @returns cost and explored of box in struct
box Maze::getBox(const coord &n) {
if (inBounds(n))
return mazeBoxes[n.x][n.y];
else
throw(std::range_error("error in range6"));
}
/// @returns cost from box n to finish
uint8_t Maze::getCost(const coord &n) {
if (inBounds(n))
return mazeBoxes[n.x][n.y].cost;
else
throw(std::range_error("error in range7"));
}
/// @returns if explored before
bool Maze::getExplored(const coord &n) {
if (inBounds(n))
return mazeBoxes[n.x][n.y].explored;
else
throw(std::range_error("error in range8"));
}
bool Maze::getFinished() { return finished; }
////--------------MUTATOR FUNCTIONS--------------////
////----Set Walls----////
void Maze::setSWall(const coord &n) {
if (inBounds(n))
mazeWalls.horzWalls[n.x][n.y] = true;
else
throw(std::range_error("error in range9"));
}
void Maze::setNWall(const coord &n) {
if (inBounds(n))
mazeWalls.horzWalls[n.x][n.y + 1] = true;
else
throw(std::range_error("error in range10"));
}
void Maze::setWWall(const coord &n) {
if (inBounds(n))
mazeWalls.vertWalls[n.x][n.y] = true;
else
throw(std::range_error("error in range11"));
}
void Maze::setEWall(const coord &n) {
if (inBounds(n))
mazeWalls.vertWalls[n.x + 1][n.y] = true;
else
throw(std::range_error("error in range12"));
}
void Maze::setBorder() {
// Set all borders walls = true
for (uint8_t i = 0; i < 16; i++) {
setWWall({0, i});
setEWall({15, i});
setSWall({i, 0});
setNWall({i, 15});
}
}
////----Remove Walls----////
void Maze::removeSWall(const coord &n) {
if (inBounds(n))
mazeWalls.horzWalls[n.x][n.y] = false;
else
throw(std::range_error("error in range13"));
}
void Maze::removeNWall(const coord &n) {
if (inBounds(n))
mazeWalls.horzWalls[n.x][n.y + 1] = false;
else
throw(std::range_error("error in range14"));
}
void Maze::removeWWall(const coord &n) {
if (inBounds(n))
mazeWalls.vertWalls[n.x][n.y] = false;
else
throw(std::range_error("error in range15"));
}
void Maze::removeEWall(const coord &n) {
if (inBounds(n))
mazeWalls.vertWalls[n.x + 1][n.y] = false;
else
throw(std::range_error("error in range16"));
}
/// @return Only the border should be left for the walls
void Maze::clearWalls() {
for (uint8_t i = 0; i < 15; i++) {
for (uint8_t j = 0; j < 15; j++) {
// Will remove horzWalls 1-15, leaving 0 and 16
removeNWall(coord{i, j});
// Will remove vertWalls 1-15, leaving 0 and 16
removeEWall(coord{i, j});
}
}
// If border didn't already exist, make one
setBorder();
}
////----Set Boxes----////
/// @brief Set box to the parameter cost
void Maze::setCost(const coord &n, const uint8_t &cost) {
if (inBounds(n)){
mazeBoxes[n.x][n.y].cost = cost;
API::setText(n.x, n.y, to_string(cost));
}else
throw(std::range_error("error in range17"));
}
/// @brief Set explored to true
void Maze::setExplored(const coord &n) {
if (inBounds(n)){
mazeBoxes[n.x][n.y].explored = true;
API::setColor(n.x, n.y, 'b');
}else
throw(std::range_error("error in range18"));
}
////----Remove Boxes----////
/// @brief Set cost to max value
void Maze::removeCost(const coord &n) {
if (inBounds(n)){
mazeBoxes[n.x][n.y].cost = UINT8_MAX;
API::setText(n.x, n.y, to_string(mazeBoxes[n.x][n.y].cost));
}else
throw(std::range_error("error in range19"));
}
/// @brief Set explored to false
void Maze::removeExplored(const coord &n) {
if (inBounds(n)){
mazeBoxes[n.x][n.y].explored = false;
API::setColor(n.x, n.y, 'r');
}else
throw(std::range_error("error in range20"));
}
////----General Functions----////
// If finished, this will make private member variable finished=true
// If not finished, finished=false
///***Later want to add functionality for diagonal travel
////CHANGE!!!!!!////
void Maze::setFinished() {
queue<coord> coordQueue;
coord n;
// If currently at corner
if (mazeBoxes[0][0].cost == 0) {
for (uint8_t i = 7; i < 9; i++) {
for (uint8_t j = 7; j < 9; j++) {
n = coord{i, j};
coordQueue.push(n);
}
}
}
// If currently at center
else if (mazeBoxes[7][7].cost == 0 || mazeBoxes[7][8].cost == 0 ||
mazeBoxes[8][7].cost == 0 || mazeBoxes[8][8].cost == 0) {
n = coord{0, 0};
coordQueue.push(n);
}
// If not from start to end
else
return;
finished = true;
coord n2;
uint8_t x, y;
// While queue is not empty
while (!coordQueue.empty()) {
n = coordQueue.front();
x = n.x;
y = n.y;
coordQueue.pop();
uint8_t currCost = getCost(n);
// If we made it to the finish
if (!currCost)
return;
// Queue checks all surrounding squares to see if they are on the best path
// If they are, add it to the queue
n2 = coord{static_cast<uint8_t>(x - 1), y};
// If north box can be traveled to
if (!getNWall(n)) {
// If box is on best path
if (currCost - 1 == getCost(n2)) {
// If box is explored
if (getExplored(n2))
coordQueue.push(n2);
}
}
n2 = coord{x, static_cast<uint8_t>(y - 1)};
// If south box can be traveled to
if (!getSWall(n)) {
if (currCost - 1 == getCost(n2)) {
if (getExplored(n2))
coordQueue.push(n2);
}
}
n2 = coord{static_cast<uint8_t>(x + 1), y};
// If east box can be traveled to
if (!getEWall(n)) {
if (currCost - 1 == getCost(n2)) {
if (getExplored(n2))
coordQueue.push(n2);
}
}
n2 = coord{x, static_cast<uint8_t>(y + 1)};
// If west box can be traveled to
if (!getWWall(n)) {
if (currCost - 1 == getCost(n2)) {
if (getExplored(n2))
coordQueue.push(n2);
}
}
}
// Only get here if there wasn't a fully explored path to the finish
finished = false;
}
////----Clear Functions----////
/// @brief Set all costs to max
void Maze::clearCost() {
for (uint8_t i = 0; i < 16; i++)
for (uint8_t j = 0; j < 16; j++)
// reset cost to MAX
removeCost(coord{i, j});
}
void Maze::clearExplored() {
for (uint8_t i = 0; i < 16; i++)
for (uint8_t j = 0; j < 16; j++)
removeExplored(coord{i, j});
}
void Maze::clearBoxes() {
clearCost();
clearExplored();
}
void Maze::clear() {
clearBoxes();
clearWalls();
finished = false;
}
////--------------FLOODFILL FUNCTIONS--------------////
bool Maze::checkAdjacent(const coord &n, box (&b)[16][16]) {
bool explored = true;
coord n2;
uint8_t x = n.x;
uint8_t y = n.y;
n2 = coord{static_cast<uint8_t>(x - 1), y};
if (inBounds(n2)) {
explored = explored & getExplored(n2);
b[n2.x][n2.y].explored = true;
}
n2 = coord{static_cast<uint8_t>(x + 1), y};
if (inBounds(n2)) {
explored = explored & getExplored(n2);
b[n2.x][n2.y].explored = true;
}
n2 = coord{x, static_cast<uint8_t>(y - 1)};
if (inBounds(n2)) {
explored = explored & getExplored(n2);
b[n2.x][n2.y].explored = true;
}
n2 = coord{x, static_cast<uint8_t>(y + 1)};
if (inBounds(n2)) {
explored = explored & getExplored(n2);
b[n2.x][n2.y].explored = true;
}
return explored;
}
// If an unexplored box has all adjacent boxes = explored, then that box =
// explored
// adjacent, take them out. Whatever's left should be made explored
void Maze::autoExploreBoxes() {
box copy[16][16];
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 16; ++j) {
copy[i][j] = mazeBoxes[i][j];
}
}
for (uint8_t i = 0; i < 16; i++) {
for (uint8_t j = 0; j < 16; j++) {
if (copy[i][j].explored) {
if (checkAdjacent(coord{i, j}, copy))
setExplored(coord{i, j});
}
}
}
}
////NOTHING STOPPING US FROM QUEUING THE SAME SQUARE TWICE????
// Performs floodFill to fill boxes with the cost to get to the center
void Maze::floodFill(coord start, vector<coord> finish) {
// get boxes ready for flood fill
////autoExploreBoxes();
// clearing cost makes all costs MAX=
clearCost();
// priority queue is sorted by cost
// uint8_t holds the cost
// coord holds the box
priority_queue<q> pq;
// The finish can be 1 square or all 4 center squares
// We start at the place we want to go
for (uint8_t i = 0; i < finish.size(); i++) {
pq.push(q{0, finish[i]});
setCost(finish[i], 0);
}
// Curr stores the box we're currently on
q curr;
coord n, n2;
uint8_t x, y;
uint8_t cost, change = 1;
// While there are unexplored boxes and we haven't found the exit
while (!pq.empty()) {
curr = pq.top();
n = curr.n;
cost = curr.cost;
x = n.x;
y = n.y;
pq.pop();
// Cost of adjacent boxes
cost++;
// If we're at the our current square//
if (x == start.x && y == start.y) {
setFinished();
return;
}
// Queueing Boxes //
// If north box can be traveled to and hasn't been queued yet, queue it
if (!getNWall(n)) {
n2 = coord{x, static_cast<uint8_t>(y + 1)};
if(inBounds(n2)){
// If the box hasn't gotten a cost yet
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If south box can be traveled to and hasn't been queued yet
if (!getSWall(n)) {
n2 = coord{x, static_cast<uint8_t>(y - 1)};
if(inBounds(n2)){
// If the box hasn't gotten a cost yet
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If east box can be traveled to and hasn't been queued yet
if (!getEWall(n)) {
n2 = coord{static_cast<uint8_t>(x + 1), y};
if(inBounds(n2)){
// If the box hasn't gotten a cost yet
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If west box can be traveled to and hasn't been queued yet
if (!getWWall(n)) {
n2 = coord{static_cast<uint8_t>(x - 1), y};
if(inBounds(n2)){
// If the box hasn't gotten a cost yet
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
}
// Only get here if we didn't find the finish
throw(std::invalid_argument("No path found"));
}
// Performs FloodFill to every box in the maze. Does not stop when reaching
// the target boxes. Used for
void Maze::floodFill(vector<coord> start) {
// get boxes ready for flood fill
autoExploreBoxes();
// clearing cost makes all costs MAX=
clearCost();
// priority queue is sorted by cost
// uint8_t holds the cost
// coord holds the box
priority_queue<q> pq;
// Pushing the four center squares to represent the finish :(
for (int i = 0; i < start.size(); i++) {
pq.push(q{0, start[i]});
setCost(start[i], 0);
}
// Curr stores the box we're currently on
q curr;
coord n, n2;
uint8_t x, y;
uint8_t cost;
// While there are unexplored boxes and we haven't found the exit
while (!pq.empty()) {
curr = pq.top();
n = curr.n;
cost = curr.cost;
x = n.x;
y = n.y;
pq.pop();
cost++;
// Queueing Boxes //
// If north box can be traveled to and hasn't been queued yet, queue it
if (!getNWall(n)) {
n2 = coord{x, static_cast<uint8_t>(y + 1)};
if(inBounds(n2)){
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If south box can be traveled to and hasn't been queued yet
if (!getSWall(n)) {
n2 = coord{x, static_cast<uint8_t>(y - 1)};
if(inBounds(n2)){
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If east box can be traveled to and hasn't been queued yet
if (!getEWall(n)) {
n2 = coord{static_cast<uint8_t>(x + 1), y};
if(inBounds(n2)){
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
// If west box can be traveled to and hasn't been queued yet
if (!getWWall(n)) {
n2 = coord{static_cast<uint8_t>(x - 1), y};
if(inBounds(n2)){
if (getCost(n2) == UINT8_MAX) {
pq.push(q{cost, n2});
setCost(n2, cost);
}
}
}
}
}