-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathfinding_classes.py
More file actions
374 lines (269 loc) · 11.7 KB
/
Pathfinding_classes.py
File metadata and controls
374 lines (269 loc) · 11.7 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
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
from __future__ import annotations
from pydantic import BaseModel
from Settings import *
from typing import Any, List, Optional, Tuple, Union
import heapq
import numpy as np
from collections import deque
from typing import Tuple, Callable
import pygame
class Visualizer(BaseModel):
en_menu: Optional[int] = 0
alg: Optional[str] = None
def __init__(__pydantic_self__, **data: Any) -> None:
pygame.display.set_caption('Pathfinfing Visualizer')
super().__init__(**data)
@staticmethod
def make_grid(cls: Union[AStar, Dijkstra]) -> np.ndarray:
grid = []
for i in range(GRIDWIDTH):
grid.append([])
for j in range(GRIDHEIGHT):
grid[i].append(cls(row=j, col=i, parent=None))
return np.array(grid)
@staticmethod
def draw(win: pygame.Surface, grid: Union[list, np.ndarray]) -> None:
win.fill(MEDGRAY)
[spot.draw(win) for row in grid for spot in row]
for x in range(0, WIDTH, TILESIZE):
pygame.draw.line(win, WHITE, (x, 0), (x, HEIGHT))
pygame.draw.line(win, WHITE, (0, x), (WIDTH, x))
pygame.display.update()
@staticmethod
def H(p1: Tuple[float, float], p2: Tuple[float, float]) -> float:
x1, y1 = p1
x2, y2 = p2
ret = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5
return ret
@staticmethod
def vec2int(node: AStar) -> Tuple[int, int]:
return (int(node.row), int(node.col))
def return_path(self, currentNode: Union[AStar, Dijkstra],
start: Union[AStar, Dijkstra],
draw: Callable) -> None:
while currentNode.parent != start:
currentNode = currentNode.parent
currentNode.set_path()
draw()
def menu(self, win: pygame.Surface) -> str:
# white color
color = (255, 255, 255)
# light shade of the button
color_light = (170, 170, 170)
# dark shade of the button
color_dark = (100, 100, 100)
# stores the width of the
# screen into a variable
width = win.get_width()
# stores the height of the
# screen into a variable
height = win.get_height()
# defining a font
smallfont = pygame.font.SysFont('Corbel', 35)
# rendering a text written in
# this font
texts = [(smallfont.render('Quit' , True , color),
(width / 2 - 30, height / 2 + 83)),
(smallfont.render('A*' , True , color),
(width / 2 - 15, height / 2 + 5)),
(smallfont.render('Dikjstra' , True , color),
(width / 2 - 50, height / 2 - 78))]
buttons = [((width / 2 - 150, width / 2 + 150),
(height / 2 + 80, height / 2 + 120), 'Quit'),
((width / 2 - 150, width / 2 + 150),
(height / 2, height / 2 + 40), 'A*'),
((width / 2 - 150, width / 2 + 150),
(height / 2 - 80, height / 2 - 40), 'Dijkstra')]
while True:
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT or \
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
self.alg = 'Quit'
return
#checks if a mouse is clicked
if event.type == pygame.MOUSEBUTTONDOWN:
#if the mouse is clicked on the
# button the game is terminated
for button in buttons:
if button[0][0] <= mouse[0] <= button[0][1] and \
button[1][0] <= mouse[1] <= button[1][1]:
self.alg = button[2]
return
# fills the screen with a color
win.fill((60,25,60))
# stores the (x,y) coordinates into
# the variable as a tuple
for button, text in zip(buttons, texts):
if button[0][0] <= mouse[0] <= button[0][1] and \
button[1][0] <= mouse[1] <= button[1][1]:
c = color_dark
else: c = color_light
pygame.draw.rect(win, c,
[button[0][0], button[1][0], 300, 40],
0, 30)
# superimposing the text onto our button
win.blit(*text)
# updates the frames of the game
pygame.display.update()
def DijkstraAlg(self, draw: Callable, start: Dijkstra,
end: Dijkstra) -> None:
openList = deque([start])
visited = {self.vec2int(start)}
while len(openList):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN and \
event.key == pygame.K_ESCAPE:
self.en_menu = 1
return
current = openList.popleft()
if current == end:
end.set_end()
self.return_path(current, start, draw)
break
for neighbour in current.neighbours:
pos = self.vec2int(neighbour)
if pos not in visited:
neighbour.parent = current
visited.add(pos)
openList.append(neighbour)
if not neighbour.is_end(): neighbour.set_open()
draw()
if current != start: current.set_close()
return
def AStarAlg(self, draw: Callable, grid: Union[list, np.ndarray],
start: AStar, end: AStar) -> None:
openList = PriorityQueue()
cost = {}
for row in grid:
for spot in row:
spot.G = float('inf')
spot.F = float('inf')
start.F = self.H(start.get_pos(), end.get_pos())
start.G = 0
openList.put(start, start.G)
cost[self.vec2int(start)] = 0
while not openList.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN and \
event.key == pygame.K_ESCAPE:
self.en_menu = 1
return
current = openList.get()
if current == end:
end.set_end()
self.return_path(current, start, draw)
break
for neighbour in current.neighbours:
G = current.G + 1
if G < neighbour.G:
neighbour.parent = current
neighbour.G = G
neighbour.F = G + self.H(neighbour.get_pos(), end.get_pos())
pos = self.vec2int(neighbour)
if pos not in cost:
openList.put(neighbour, neighbour.F)
cost[pos] = neighbour.F
if not neighbour.is_end(): neighbour.set_open()
draw()
if current != start: current.set_close()
return
class Pathfinding(BaseModel):
row: int
col: int
x: int = 0
y: int = 0
color: Tuple[int, int, int] = MEDGRAY
neighbours: List[AStar] = []
width: int = TILESIZE
total_rows = GRIDHEIGHT
total_cols = GRIDWIDTH
def __init__(__pydantic_self__, **data: Any) -> None:
data['x'] = data['row'] * TILESIZE
data['y'] = data['col'] * TILESIZE
super().__init__(**data)
def get_neighbours(self, grid: Union[list, np.ndarray]):
self.neighbours = []
if self.col > 0 and not grid[self.col - 1][self.row].is_wall(): #LEFT
self.neighbours.append(grid[self.col - 1][self.row])
if self.col < self.total_cols - 1 and not grid[self.col + 1][self.row].is_wall(): #RIGHT
self.neighbours.append(grid[self.col + 1][self.row])
if self.row > 0 and not grid[self.col][self.row - 1].is_wall(): #UP
self.neighbours.append(grid[self.col][self.row - 1])
if self.row < self.total_rows - 1 and not grid[self.col][self.row + 1].is_wall(): #DOWN
self.neighbours.append(grid[self.col][self.row + 1])
# Diagonals
if issubclass(type(self), Dijkstra): return
if self.col < self.total_cols - 1 and self.row > 0 and not grid[self.col + 1][self.row - 1].is_wall(): #TOP-RIGHT
if not (grid[self.col][self.row - 1].is_wall() and grid[self.col + 1][self.row].is_wall()):
self.neighbours.append(grid[self.col + 1][self.row - 1])
if self.col > 0 and self.row > 0 and not grid[self.col - 1][self.row - 1].is_wall(): #TOP-LEFT
if not (grid[self.col][self.row - 1].is_wall() and grid[self.col - 1][self.row].is_wall()):
self.neighbours.append(grid[self.col - 1][self.row - 1])
if self.col > 0 and self.row < self.total_rows - 1 and not grid[self.col - 1][self.row + 1].is_wall(): #BOTTOM-LEFT
if not (grid[self.col][self.row + 1].is_wall() and grid[self.col - 1][self.row].is_wall()):
self.neighbours.append(grid[self.col - 1][self.row + 1])
if self.col < self.total_cols - 1 and self.row < self.total_rows - 1 and not grid[self.col + 1][self.row + 1].is_wall(): #BOTTOM-RIGHT
if not (grid[self.col][self.row + 1].is_wall() and grid[self.col + 1][self.row].is_wall()):
self.neighbours.append(grid[self.col + 1][self.row + 1])
def get_pos(self):
return self.row, self.col
def is_open(self):
return self.color == GREEN
def is_wall(self):
return self.color == BLACK
def is_start(self):
return self.color == ORANGE
def is_end(self):
return self.color == BLUE
def reset(self):
self.color = MEDGRAY
def set_start(self):
self.color = ORANGE
def set_close(self):
self.color = RED
def set_open(self):
self.color = GREEN
def set_wall(self):
self.color = BLACK
def set_end(self):
self.color = BLUE
def set_path(self):
self.color = YELLOW
def draw(self, win: pygame):
if not self.is_open():
pygame.draw.rect(win, self.color,
(self.y, self.x,
self.width, self.width))
else:
pygame.draw.circle(win, self.color,
(self.y + self.width // 2,
self.x + self.width // 2),
self.width // 3)
class AStar(Pathfinding):
parent: Optional[AStar] = None
F: float = 0.
G: int = 0
def __lt__(self, other: AStar):
return self.F < other.F
def __eq__(self, other: AStar):
return other and self.row == other.row and \
self.col == other.col
class Dijkstra(Pathfinding):
parent: Optional[Dijkstra] = None
def __eq__(self, other: Dijkstra):
return other and self.row == other.row and \
self.col == other.col
class PriorityQueue:
def __init__(self):
self.nodes = []
def put(self, node: Union[AStar, Dijkstra], cost: float):
heapq.heappush(self.nodes, (cost, node))
def get(self):
return heapq.heappop(self.nodes)[1]
def empty(self):
return len(self.nodes) == 0