-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
308 lines (247 loc) · 8.45 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <SDL.h>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <time.h>
//Window size
const int WIDTH = 800, HEIGHT = 600;
//Grid Structure this holds all the information on each point
struct GridPoint {
//Cordinates
int x;
int y;
//Bool for if the point is solid etc a wall 0 for false 1 for true, solid points are shown in gray
int solid = 1;
//Bool for if the algorithm has picked this point as the best point this is shown in red
int pickedPoint;
//Bool for if this point has already been calculated, this is used so there arent repated calculated points in the calculated vectors, tiles that have been calculated but not picked are shown in blue
int calculated;
//Bool to check if this tile has been picked as the shortest path this is shown as dark red
int truePath = 0;
//So you dont have to check the frontier list to see if this Cell is already in the frontierCells list
int frontier = 0;
//H cost is calculated by how far this point is from the end point
int hCost;
//G cost is calculated by how far this point is from the start point
int gCost;
//F = gCost + hCost
int fCost;
//the last node that was chosen as the best node
GridPoint* parent;
GridPoint* parentCell = NULL;
};
//Starting and ending point
const int startingX = 20;
const int startingY = 20;
const int endX = 60;
const int endY = 50;
//2D Grid of all the points
struct GridPoint grid[80][60];
//This vector stores all Calculated points
std::vector<GridPoint*> CalculatedPoints;
//this is a vector that holds all the caculated neighbours of a cells.
std::vector<GridPoint*> frontierCells;
void Draw(SDL_Renderer* renderer) {
//sets the background colour and then clears the window
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for (int y = 0; y < 60; y++) {
for (int x = 0; x < 80; x++) {
SDL_Rect rect = { x * 10,y * 10,9,9 };
if (x == startingX && y == startingY)
{
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
else if (x == endX && y == endY) {
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
}
else if (grid[x][y].truePath == 1) {
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
}
else if (grid[x][y].pickedPoint == 1) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
else if (grid[x][y].solid == 1) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
}
else {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
SDL_RenderFillRect(renderer, &rect);
}
}
//updates the screen with the new drawings in the renderer
SDL_RenderPresent(renderer);
}
void Neighbours(int x, int y) {
if (x > 1 && grid[x - 2][y].frontier == 0 && grid[x - 2][y].solid == 1) {
frontierCells.push_back(&grid[x - 2][y]);
grid[x - 2][y].frontier = 1;
grid[x - 2][y].parentCell = &grid[x][y];
}
if (x < 79 && grid[x + 2][y].frontier == 0 && grid[x + 2][y].solid == 1) {
frontierCells.push_back(&grid[x + 2][y]);
grid[x + 2][y].frontier = 1;
grid[x + 2][y].parentCell = &grid[x][y];
}
if (y > 1 && grid[x][y - 2].frontier == 0 && grid[x][y - 2].solid == 1) {
frontierCells.push_back(&grid[x][y - 2]);
grid[x][y - 2].frontier = 1;
grid[x][y - 2].parentCell = &grid[x][y];
}
if (y < 59 && grid[x][y + 2].frontier == 0 && grid[x][y + 2].solid == 1) {
frontierCells.push_back(&grid[x][y + 2]);
grid[x][y + 2].frontier = 1;
grid[x][y + 2].parentCell = &grid[x][y];
}
}
GridPoint* NextCell(int x, int y) {
//Checks to see if there is not more frontier cells to choce from if so the maze is complete
if (frontierCells.size() <= 0)
return NULL;
GridPoint* nextCell = frontierCells[rand() % frontierCells.size()];
nextCell->solid = 0;
grid[(nextCell->parentCell->x + nextCell->x) / 2][(nextCell->parentCell->y + nextCell->y) / 2].solid = 0;
Neighbours(nextCell->x, nextCell->y);
frontierCells.erase(std::find(frontierCells.begin(), frontierCells.end(), nextCell));
return nextCell;
}
//A*
//This function calculates the H, G and F cost of each poiunt
void CalculateCost(struct GridPoint* point, int x, int y) {
if (point->solid == 1)
return;
point->gCost = std::sqrt((startingX - point->x) * (startingX - point->x) + (startingY - point->y) * (startingY - point->y)) * 10;
point->hCost = std::sqrt((endX - point->x) * (endX - point->x) + (endY - point->y) * (endY - point->y)) * 10;
point->fCost = point->hCost + point->gCost;
//This changes the parent point for any calculated node to its most recently picked neighbour.
if (point->pickedPoint == 0) {
point->parent = &grid[x][y];
}
//If the point is newly calculated add the point to the CalculatedPoints vector
if (point->pickedPoint == 0 && point->calculated == 0)
{
point->calculated = 1;
CalculatedPoints.push_back(point);
}
}
//This function runs after the end point has been found and finds the shortest path between the start and end by going through all the parentpoints starting from the end point
//This function can be modified to store the parent points in a vector giving a vector of all points someone would have to follow to go from the start to the end
void FindPath() {
GridPoint* currentTIle = &grid[endX][endY];
while (currentTIle->x != startingX || currentTIle->y != startingY) {
currentTIle->truePath = 1;
currentTIle = currentTIle->parent;
}
}
//This function calculates all neighbouring points of the inputed point (x,y) and then selects the next point by checking the CalculatedPoints vector and choosing the point with the lowest F value, if there are multiple lowest F values then it picks the one with the Lowest H value
struct GridPoint* NextPoint(int x, int y) {
//Calculate all the points next to current point
if (x > 1) {
CalculateCost(&grid[x - 1][y], x, y);
}
if (x < 79) {
CalculateCost(&grid[x + 1][y], x, y);
}
if (x > 1 && y > 1) {
CalculateCost(&grid[x - 1][y - 1], x, y);
}
if (x > 1 && y > 59) {
CalculateCost(&grid[x - 1][y + 1], x, y);
}
if (x < 79 && y > 1) {
CalculateCost(&grid[x + 1][y - 1], x, y);
}
if (x < 79 && y < 59) {
CalculateCost(&grid[x + 1][y + 1], x, y);
}
if (y > 1) {
CalculateCost(&grid[x][y - 1], x, y);
}
if (y < 79) {
CalculateCost(&grid[x][y + 1], x, y);
}
if (CalculatedPoints.size() <= 0)
return NULL;
GridPoint* lowestCostPoint = CalculatedPoints[0];
for (int point = 0; point < CalculatedPoints.size(); point++) {
if (CalculatedPoints[point]->fCost < lowestCostPoint->fCost) {
lowestCostPoint = CalculatedPoints[point];
}
else if (CalculatedPoints[point]->x == endX && CalculatedPoints[point]->y == endY) {
lowestCostPoint = CalculatedPoints[point];
break;
}
else if (CalculatedPoints[point]->fCost == lowestCostPoint->fCost && CalculatedPoints[point]->hCost < lowestCostPoint->hCost) {
lowestCostPoint = CalculatedPoints[point];
}
}
CalculatedPoints.erase(std::find(CalculatedPoints.begin(), CalculatedPoints.end(), lowestCostPoint));
lowestCostPoint->pickedPoint = 1;
return lowestCostPoint;
}
int main(int argc, char* args[]) {
int CreateMaze = 1;
int solve = 0;
//randomzies the seed by setting it to the time
srand(time(NULL));
//adding structs to grid
for (int y = 0; y < 60; y++) {
for (int x = 0; x < 80; x++) {
struct GridPoint newPoint;
newPoint.x = x;
newPoint.y = y;
newPoint.calculated = 0;
newPoint.solid = 1;
newPoint.pickedPoint = 0;
grid[x][y] = newPoint;
}
}
//SDL
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Maze", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Event event;
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
grid[endX][endY].solid = 1;
int currentX = startingX;
int currentY = startingY;
GridPoint* currentCell = &grid[currentX][currentY];
currentCell->solid = 0;
Neighbours(currentCell->x, currentCell->y);
while (true) {
if (SDL_PollEvent(&event)) { if (SDL_QUIT == event.type) break; }
if (CreateMaze == 1)
{
currentCell = NextCell(currentCell->x, currentCell->y);
if (currentCell == NULL)
{
CreateMaze = 0;
solve = 1;
}
}
if (solve == 1)
{
struct GridPoint* lastGrid = NextPoint(currentX, currentY);
if (lastGrid == NULL)
{
solve = 0;
}
else {
currentX = lastGrid->x;
currentY = lastGrid->y;
if (currentX == endX && currentY == endY)
{
solve = 0;
std::cout << "solved";
FindPath();
}
}
}
Draw(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}