-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeSolver.cpp
202 lines (182 loc) · 6.14 KB
/
MazeSolver.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
//
// Created by Marius on 25.02.2018.
//
#include "MazeSolver.h"
#include <algorithm>
#include <stdlib.h>
#include <math.h>
MazeSolver::MazeSolver(MazeGenerator* mgen, SDL_Renderer* r){
this->renderer = r;
this->scale = mgen->getScale();
this->start = mgen->getStart();
this->end = mgen->getEnd();
this->matrix = mgen->getMatrix();
this->matrixSize = mgen->getSize();
this->debug = false;
this->dragStart = false;
this->dragEnd = false;
}
bool MazeSolver::astar_solve(){
vector<Cell*> closedSet;
vector<Cell*> openSet;
openSet.push_back(this->start);
//reset matrix
for(int x=0; x < matrixSize;x++){
for(int y=0; y < matrixSize;y++) {
this->matrix[x][y].h = 0;
this->matrix[x][y].f = 0;
this->matrix[x][y].g = 0;
this->matrix[x][y].setVisited(false);
this->matrix[x][y].cameFrom = nullptr;
}
}
while(openSet.size() > 0){
if(this->debug) {
for (auto cell : closedSet) {
if (!cell->highlighted) {
cell->highlighted = true;
cell->highlight({100, 0, 0, 0});
}
}
for (auto cell : openSet) {
cell->highlight({0, 100, 0, 0});
}
}
Cell* current = openSet.at(0);
//get cell with lowest f in openSet
for (auto n : openSet) {
if(n->f < current->f){
current = n;
}
}
//path found when current = end
if(current == this->end){
//reconstruct path from linked list
this->path.push_back(current);
while(current->cameFrom != nullptr){
this->path.push_back(current->cameFrom);
current = current->cameFrom;
}
this->drawPath({255,0,0,0});
return true;
}
//remove current from openSet
std::vector<Cell*>::iterator pos = std::find(openSet.begin(), openSet.end(), current);
openSet.erase(pos);
//add current to closedSet
closedSet.push_back(current);
for(auto neighbour : this->getNeighbours(current)){
if(std::find(closedSet.begin(), closedSet.end(), neighbour) != closedSet.end()) {
continue;
}
if(!(std::find(openSet.begin(), openSet.end(), neighbour) != openSet.end())){
openSet.push_back(neighbour);
}
int tempG = current->g + 1;
if(std::find(openSet.begin(), openSet.end(), neighbour) != openSet.end()) {
if(tempG < neighbour->g)
neighbour->g = tempG;
}else{
neighbour->g = tempG;
}
neighbour->h = heuristic(neighbour,this->end);
neighbour->f = neighbour->g + neighbour->h;
neighbour->cameFrom = current;
}
}
return false;
}
std::vector<Cell*> MazeSolver::getNeighbours(Cell* current){
std::vector<Cell*> neighbours;
map<string,bool> walls = current->getWalls();
int x,y;
std::tie(x,y) = current->getIndex();
if(current->getY() != 0 && !walls["top"]){
neighbours.push_back(this->getCell(x,y-1));
}
//Bottom Neighbour
if(current->getY() != this->matrixSize-1 && !walls["bottom"]){
neighbours.push_back(this->getCell(x,y+1));
}
//Left Neighbour
if(current->getX() != 0 && !walls["left"]){
neighbours.push_back(this->getCell(x-1,y));
}
//Right Neighbour
if(current->getX() != this->matrixSize-1 && !walls["right"]){
neighbours.push_back(this->getCell(x+1,y));
}
return neighbours;
}
int MazeSolver::heuristic(Cell* start, Cell* end){
return abs(start->getX() - end->getX()) + abs(start->getY() - end->getY());
}
void MazeSolver::drawPath(SDL_Color color) {
if(this->path.size() <= 0){
return;
}
SDL_SetRenderDrawColor(this->renderer, color.r, color.g, color.b, color.a);
for (size_t i = 0; i < this->path.size() - 1; i++) {
SDL_RenderDrawLine(this->renderer,
this->path[i]->getX() + scale / 2,
this->path[i]->getY() + scale / 2,
this->path[i + 1]->getX() + scale / 2,
this->path[i + 1]->getY() + scale / 2
);
}
SDL_RenderPresent(this->renderer);
}
Cell* MazeSolver::getCell(int x, int y){
return &this->matrix[x][y];
}
void MazeSolver::dragndrop(SDL_Event e){
int x,y;
SDL_GetMouseState(&x,&y);
if(this->dragStart){
x = x / scale * scale;
y = y / scale * scale;
Cell* oldStart = start;
this->start = &this->matrix[x / scale][y / scale];
if(this->start != oldStart){
oldStart->highlight({0,0,0,255});
this->clearPath();
this->astar_solve();
this->drawPath({255, 0, 0, 255});
}
}
if(this->dragEnd) {
x = x / scale * scale;
y = y / scale * scale;
Cell* oldEnd = end;
this->end = &this->matrix[x / scale][y / scale];
if (this->end != oldEnd) {
oldEnd->highlight({0, 0, 0, 255});
this->clearPath();
this->astar_solve();
this->drawPath({255, 0, 0, 255});
}
}
//highlight start and end point
this->end->highlight({255,0,0,255});
this->start->highlight({255,0,0,255});
if(e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT) {
if(this->dragStart){
this->dragStart = false;
}
if(this->dragEnd){
this->dragEnd = false;
}
//check if endpoint is clicked
if(x > this->end->getX() && x < this->end->getX() + this->scale && y > this->end->getY() && y < this->end->getY() + this->scale){
this->dragEnd = true;
}
//check if startpoint is clicked
if(x > this->start->getX() && x < this->start->getX() + this->scale && y > this->start->getY() && y < this->start->getY() + this->scale){
this->dragStart = true;
}
}
}
void MazeSolver::clearPath(){
drawPath({0,0,0,255});
this->path.clear();
}