-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathfinding-simulator.py
369 lines (272 loc) · 11.6 KB
/
pathfinding-simulator.py
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
import pygame
import random
class Gui():
FPS = 60
WIDTH = 800
def __init__(self, coords):
self.grid_size = 20
self.box_width = self.WIDTH/self.grid_size
self.coords = coords
self.placing_walls = False
self.removing_walls = False
self.animation_speed = 10
self.coords.maze = [
[0 for x in range(self.grid_size)] for y in range(self.grid_size)]
pygame.init()
self.win = pygame.display.set_mode((self.WIDTH, self.WIDTH))
self.clock = pygame.time.Clock()
pygame.display.set_caption("Pathfinding Simulator")
def main(self, running=False):
self.clock.tick(self.FPS)
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
if not running:
if self.placing_walls == True:
self.place_wall()
elif self.removing_walls == True:
self.remove()
self.event_handle(running)
self.redraw()
pygame.display.update()
def event_handle(self, running):
run_keys = {"q", "w", "e"}
checkpoint_keys = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
key = chr(event.key)
if running == False:
if key in run_keys:
self.run_algorithm(key)
elif key == "x":
self.coords.remove_all()
elif key == "z":
self.coords.remove_last()
elif key in checkpoint_keys:
self.place_check_point(key)
elif event.type == pygame.MOUSEBUTTONDOWN:
if running == False:
if event.button == 1:
self.placing_walls = True
elif event.button == 3:
self.removing_walls = True
if event.button == 4:
self.grid_size -= 1
self.box_width = self.WIDTH/self.grid_size
elif event.button == 5:
self.grid_size += 1
self.box_width = self.WIDTH/self.grid_size
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.placing_walls = False
elif event.button == 3:
self.removing_walls = False
def redraw(self):
self.win.fill((255,255,255))
self.draw_points()
self.draw_grid()
def draw_grid(self):
for i in range(self.grid_size-1):
pygame.draw.rect(self.win, (0, 0, 0),
(((i+1)*self.box_width)-2, 0, 4, self.WIDTH))
pygame.draw.rect(self.win, (0, 0, 0),
(0,((i+1)*self.box_width)-2, self.WIDTH, 4))
def draw_points(self):
for node in self.coords.open_list:
self.draw_box(node.position, (0, 255, 0))
for node in self.coords.closed_list:
self.draw_box(node.position, (0, 0, 255))
for wall in self.coords.final_path:
self.draw_box(wall, (255, 0, 255))
for wall in self.coords.walls:
self.draw_box(wall, (0, 0, 0))
for i,point in enumerate(self.coords.check_points):
if point != "None":
self.draw_box(point, (255, 30, 30))
self.display_text(str(i+1), (255, 255, 255),
self.box_center(point), int(self.box_width))
if self.coords.start != None:
self.draw_box(self.coords.start, (255, 0, 0))
self.display_text("S", (255, 255, 255),
self.box_center(self.coords.start), int(self.box_width))
if self.coords.end != None:
self.draw_box(self.coords.end, (255, 0, 0))
self.display_text("E", (255, 255, 255),
self.box_center(self.coords.end), int(self.box_width))
def box_center(self, box):
boxX, boxY = box
center = ((boxX*self.box_width+(self.box_width/2)),
(boxY*self.box_width+(self.box_width/2)))
return center
def draw_box(self, box, colour):
boxX, boxY = box
pygame.draw.rect(self.win, colour,
(boxX*self.box_width, boxY*self.box_width,
self.box_width, self.box_width))
def get_box_coords(self):
boxX = int((self.mouse_x + 2) / self.box_width)
boxY = int((self.mouse_y + 2) / self.box_width)
return (boxX, boxY)
def place_check_point(self, index):
coords = self.get_box_coords()
if (coords != self.coords.start and coords != self.coords.end
and coords not in self.coords.walls and coords
not in self.coords.check_points):
while len(self.coords.check_points) <= int(index)-1:
self.coords.check_points.append("None")
self.coords.check_points[int(index)-1] = coords
def place_wall(self):
coords = self.get_box_coords()
if (coords != self.coords.start and coords != self.coords.end
and coords not in self.coords.walls and coords
not in self.coords.check_points):
self.coords.walls.append(coords)
def remove(self):
coords = self.get_box_coords()
if coords in self.coords.walls:
self.coords.walls.remove(coords)
elif coords in self.coords.check_points:
self.coords.check_points.remove(coords)
elif coords == self.coords.start:
self.coords.start = None
elif coords == self.coords.end:
self.coords.end = None
def run_algorithm(self, key):
self.placing_walls == False
self.removing_walls == False
self.coords.remove_last()
if len(self.coords.check_points) > 1:
self.coords.create_maze(gui)
check_points = self.coords.check_points[:]
check_points = [point for point in check_points if point != "None"]
for i,point in enumerate(check_points):
if i != len(check_points)-1:
start = point
end = check_points[i+1]
new_path = pathfind(self.coords.maze, start, end,
self, self.coords, key)
if new_path == None:
new_path = []
self.coords.final_path.extend(new_path)
def display_text(self, txt, colour, center, size):
font = pygame.font.Font(None, size)
text_surf = font.render(txt, True, colour)
text_rect = text_surf.get_rect()
text_rect.center = (center)
self.win.blit(text_surf, text_rect)
class CoOrdinates():
def __init__(self):
self.remove_all()
def remove_all(self):
self.start = None
self.end = None
self.walls = []
self.maze = []
self.open_list = []
self.closed_list = []
self.final_path = []
self.check_points = []
def remove_last(self):
self.maze = []
self.open_list = []
self.closed_list = []
self.final_path = []
def largest_distance(self):
largest = 0
for wall in self.walls:
if wall[0] > largest: largest = wall[0]
if wall[1] > largest: largest = wall[1]
for point in self.check_points:
if point[0] > largest: largest = point[0]
if point[1] > largest: largest = point[1]
return largest + 1
def create_maze(self, giu):
largest_distance = self.largest_distance()
if gui.grid_size > largest_distance:
largest = gui.grid_size
else:
largest = largest_distance
self.maze = [[0 for x in range(largest)] for y in range(largest)]
for wall in self.walls:
try:
wall_x, wall_y = wall
self.maze[wall_x][wall_y] = 1
except:
pass
def pathfind(maze, start, end, gui, coords, key):
start_node = Node(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = Node(None, end)
end_node.g = end_node.h = end_node.f = 0
open_list = []
closed_list = []
open_list.append(start_node)
count = 0
while len(open_list) > 0:
if count >= gui.animation_speed:
count = 0
if key == "q": # dfs
current_node = open_list[-1]
current_index = len(open_list)-1
elif key == "w": # bfs
current_node = open_list[0]
current_index = 0
elif key == "e": # dijkstra
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.g < current_node.g:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
coords.open_list = open_list
coords.closed_list = closed_list
return path
for new_pos in [(-1, 0), (0, 1), (1, 0), (0, -1)]:
node_pos = (current_node.position[0] + new_pos[0],
current_node.position[1] + new_pos[1])
if (node_pos[0] > (len(maze) - 1) or node_pos[0] < 0
or node_pos[1] > (len(maze[len(maze)-1]) -1)
or node_pos[1] < 0):
continue
if maze[node_pos[0]][node_pos[1]] != 0:
continue
if Node(current_node, node_pos) in closed_list:
continue
child = Node(current_node, node_pos)
passList = [False for closed_child in closed_list if child == closed_child]
if False in passList:
continue
if key == "e": # dijkstra,
child.g = current_node.g + 1
for open_node in open_list:
if child == open_node and child.g >= open_node.g:
break
else:
open_list.append(child)
else:
coords.open_list = open_list
coords.closed_list = closed_list
gui.main(True)
count += 1
class Node():
def __init__(self, parent, position):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
if __name__ == "__main__":
gui = Gui(CoOrdinates())
while True:
gui.main()