-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
349 lines (313 loc) · 13.4 KB
/
main.cpp
File metadata and controls
349 lines (313 loc) · 13.4 KB
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
#define MAP_WIDTH 20
#define MAP_HEIGHT 20
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <list>
int mainWindowHeight = 800;
int mainWindowWidth = 800;
int currentMap[MAP_WIDTH][MAP_HEIGHT];
int startPosition[2] = { 0, 0};
int endPosition[2] = { 19, 19};
int startCell = (startPosition[0]*MAP_HEIGHT)+startPosition[1];
int endCell = (endPosition[0]*MAP_HEIGHT)+endPosition[1];
class Cells
{
public:
int positionX, positionY, parentCellId, cummulativeCost, cellId;
double costToGoal, totalCostGuess;
bool visited = false;
bool obstacle = false;
std::list<int> neighbours;
};
double dist(double x1, double y1, double x2, double y2)
{
double x = x1 - x2; //calculating number to square in next step
double y = y1 - y2;
double dist;
dist = pow(x, 2) + pow(y, 2); //calculating Euclidean distance
dist = sqrt(dist);
return dist;
}
Cells cellsList[MAP_HEIGHT*MAP_WIDTH];
int main()
{
bool focus = true;
bool mousePressedLeft = false;
bool stateHasChanged = true;
sf::Event event;
sf::RenderWindow window(sf::VideoMode(mainWindowHeight, mainWindowWidth), "A Star");
window.setFramerateLimit(60);
sf::RectangleShape cellShape;
cellShape.setSize(sf::Vector2f( mainWindowWidth/MAP_WIDTH, mainWindowHeight/MAP_HEIGHT));
cellShape.setFillColor(sf::Color(255,255,255));
cellShape.setOutlineThickness(-1);
cellShape.setOutlineColor(sf::Color(0,0,0));
for(int i = 0; i < MAP_WIDTH; i++)
{
for(int j = 0; j < MAP_HEIGHT; j++)
{
currentMap[i][j] = 0;
}
}
while(window.isOpen())
{
while (window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::LostFocus:
focus = false;
break;
case sf::Event::GainedFocus:
focus = true;
break;
}
}
//Mouse interaction
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && !mousePressedLeft && focus)
{
stateHasChanged = true;
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
if( currentMap[mousePosition.x/(mainWindowWidth/MAP_WIDTH)][mousePosition.y/(mainWindowHeight/MAP_HEIGHT)] == 0)
{
currentMap[mousePosition.x/(mainWindowWidth/MAP_WIDTH)][mousePosition.y/(mainWindowHeight/MAP_HEIGHT)] = 1;
}
else
{
currentMap[mousePosition.x/(mainWindowWidth/MAP_WIDTH)][mousePosition.y/(mainWindowHeight/MAP_HEIGHT)] = 0;
}
mousePressedLeft = true;
}
else if(!sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
mousePressedLeft = false;
}
if(stateHasChanged)
{
std::list<Cells*> listToCheck;
int n = 0;
for(int i = 0; i < MAP_WIDTH; i++)
{
for(int j = 0; j < MAP_HEIGHT; j++)
{
cellsList[n].positionX = i;
cellsList[n].positionY = j;
if(currentMap[i][j] == 1)
{
cellsList[n].obstacle = true;
}
else
{
cellsList[n].obstacle = false;
}
cellsList[n].costToGoal = NULL;
cellsList[n].visited = false;
cellsList[n].parentCellId = NULL;
cellsList[n].cummulativeCost = NULL;
cellsList[n].totalCostGuess = NULL;
cellsList[n].neighbours.clear();
cellsList[n].cellId = n;
n++;
}
}
for(int i = 0; i < (MAP_HEIGHT*MAP_WIDTH) ; i++)
{
if(cellsList[i].positionX > 0)
{
//de cell links kan toegevoegd worden
if(!cellsList[i-MAP_HEIGHT].obstacle)
{
cellsList[i].neighbours.push_back(i-MAP_HEIGHT);
}
}
if(cellsList[i].positionX < MAP_WIDTH-1)
{
//de cell rechts kan toegevoegd worden
if(!cellsList[i+MAP_HEIGHT].obstacle)
{
cellsList[i].neighbours.push_back(i+MAP_HEIGHT);
}
}
if(cellsList[i].positionY > 0)
{
//de cell erboven kan toegevogd worden
if(!cellsList[i-1].obstacle)
{
cellsList[i].neighbours.push_back(i-1);
}
}
if(cellsList[i].positionY != MAP_HEIGHT-1)
{
//de cell eronder kan toegevoegd worden
if(!cellsList[i+1].obstacle)
{
cellsList[i].neighbours.push_back(i+1);
}
}
//schuin gaan...
if(cellsList[i].positionY != MAP_HEIGHT-1 && cellsList[i].positionX < MAP_WIDTH-1)
{
//de cell rechtsonder kan toegevoegd worden
if(!cellsList[i+1+MAP_HEIGHT].obstacle )
{
if(cellsList[i+1].obstacle && cellsList[i+MAP_HEIGHT].obstacle)
{
//Dit hokje wordt door de twee buur hokjes geblokkeerd!
}
else
{
cellsList[i].neighbours.push_back(i+1+MAP_HEIGHT);
}
}
}
if(cellsList[i].positionY >0 && cellsList[i].positionX < MAP_WIDTH-1)
{
//de cell rechtsboven kan toegevoegd worden
if(!cellsList[i-1+MAP_HEIGHT].obstacle)
{
if(cellsList[i-1].obstacle && cellsList[i+MAP_HEIGHT].obstacle)
{
//Dit hokje wordt door de twee buur hokjes geblokkeerd!
}
else
{
cellsList[i].neighbours.push_back(i-1+MAP_HEIGHT);
}
}
}
if(cellsList[i].positionY != MAP_HEIGHT-1 && cellsList[i].positionX > 0)
{
//de cell linksonder kan toegevoegd worden
if(!cellsList[i+1-MAP_HEIGHT].obstacle)
{
if(cellsList[i+1].obstacle && cellsList[i-MAP_HEIGHT].obstacle)
{
//Dit hokje wordt door de twee buur hokjes geblokkeerd!
}
else
{
cellsList[i].neighbours.push_back(i+1-MAP_HEIGHT);
}
}
}
if(cellsList[i].positionY >0 && cellsList[i].positionX > 0)
{
//de cell rechtsboven kan toegevoegd worden
if(!cellsList[i-1-MAP_HEIGHT].obstacle)
{
if(cellsList[i-MAP_HEIGHT].obstacle && cellsList[i-1].obstacle)
{
//Dit hokje wordt door de twee buur hokjes geblokkeerd!
}
else
{
cellsList[i].neighbours.push_back(i-1-MAP_HEIGHT);
}
}
}
}
listToCheck.push_back(&cellsList[startCell]);
cellsList[startCell].visited = true;
bool pathFound = false;
while(!listToCheck.empty())
{
//sorteer de lijst en zet de cell met de laagste cost to goal bovenaan om het eerst te testen
listToCheck.sort([](const Cells* f, const Cells* s)
{
return f->totalCostGuess < s->totalCostGuess;
});
//Check of de te checken cell het doel is. Als dat zo is zijn we klaar
if((*listToCheck.front()).cellId == endCell)
{
listToCheck.clear();
pathFound = true;
}
else
{
for (std::list<int>::const_iterator iterator = (*listToCheck.front()).neighbours.begin(), end = (*listToCheck.front()).neighbours.end(); iterator != end; ++iterator)
{
//We have found neighbours!
//check if neighbours was found before
if(!cellsList[*iterator].visited)
{
//Deze cell heeft geen parent is is dus nooit eerder gevonden!
//De cell waarvan we de neighbours onderzoeken is dus automagisch tot nu toe de kortste route hiernaartoe
cellsList[*iterator].parentCellId = (*listToCheck.front()).cellId;
//Nu moeten de kosten voor de route hiernatoe uitgerekend worden (Dit zijn de kosten van naar de buurman gaan +1
cellsList[*iterator].cummulativeCost = (*listToCheck.front()).cummulativeCost+1;
//Als laatste zetten we de cell in de lijst met cellen die gecheckt moet worden
listToCheck.push_back(&cellsList[*iterator]);
//Bereken de afstand naar het doel
cellsList[*iterator].costToGoal = dist(cellsList[*iterator].positionX,cellsList[*iterator].positionY,cellsList[endCell].positionX,cellsList[endCell].positionY);
cellsList[*iterator].totalCostGuess = cellsList[*iterator].costToGoal + cellsList[*iterator].cummulativeCost;
cellsList[*iterator].visited = true;
}
else
{
//Deze cell is al eerder gevonden, staat dus als in de te checken cell lijst
if((*listToCheck.front()).cummulativeCost+1 < cellsList[*iterator].cummulativeCost)
{
//Er is een kortere route naar deze cell! Pas de parent cell dus aan en geef een nieuwe cummulative Cost;
cellsList[*iterator].parentCellId = (*listToCheck.front()).cellId;
cellsList[*iterator].cummulativeCost = (*listToCheck.front()).cummulativeCost+1;
cellsList[*iterator].totalCostGuess = cellsList[*iterator].costToGoal + cellsList[*iterator].cummulativeCost;
}
}
}
listToCheck.pop_front();
}
}
stateHasChanged = false;
}
//Draw stuff:
window.clear();
for(int i = 0; i < MAP_WIDTH; i++)
{
for(int j = 0; j < MAP_HEIGHT; j++)
{
if(currentMap[i][j] == 1)
{
cellShape.setFillColor(sf::Color(40,40,40));
cellShape.setPosition((mainWindowWidth/MAP_WIDTH)*i, (mainWindowHeight/MAP_HEIGHT)*j);
window.draw(cellShape);
cellShape.setFillColor(sf::Color(255,255,255));
}
else
{
cellShape.setPosition((mainWindowWidth/MAP_WIDTH)*i, (mainWindowHeight/MAP_HEIGHT)*j);
window.draw(cellShape);
}
}
}
//Draw path
//Create a list with the end as starting point
std::list<Cells> route;
route.push_back(cellsList[endCell]);
bool endReached = false;
while(!endReached)
{
route.push_front(cellsList[route.front().parentCellId]);
cellShape.setFillColor(sf::Color(0,0,255));
cellShape.setPosition((mainWindowWidth/MAP_WIDTH)* route.front().positionX, (mainWindowHeight/MAP_HEIGHT)*route.front().positionY);
window.draw(cellShape);
route.pop_back();
if(route.front().parentCellId == startCell)
{
endReached = true;
}
}
cellShape.setFillColor(sf::Color(0,255,0));
cellShape.setPosition((mainWindowWidth/MAP_WIDTH)*startPosition[0], (mainWindowHeight/MAP_HEIGHT)*startPosition[1]);
window.draw(cellShape);
cellShape.setFillColor(sf::Color(255,0,0));
cellShape.setPosition((mainWindowWidth/MAP_WIDTH)*endPosition[0], (mainWindowHeight/MAP_HEIGHT)*endPosition[1]);
window.draw(cellShape);
cellShape.setFillColor(sf::Color(255,255,255));
window.display();
}
return 0;
}