-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pathfinder.py
220 lines (154 loc) · 5.99 KB
/
Pathfinder.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
import heapq
import numpy as np
import pygame
from pygame.locals import (
K_BACKSPACE,
K_RETURN,
KEYDOWN,
MOUSEBUTTONUP,
MOUSEBUTTONDOWN,
MOUSEMOTION,
QUIT,
)
class Cell:
def __init__(self, figure, is_wall, distance_from_start, coords, visited=False, predecessor=None):
self.figure = figure
self.is_wall = is_wall
self.distance_from_start = distance_from_start
self.coords = coords
self.visited = visited
self.predecessor = predecessor
def __lt__(self, other_cell):
return self.distance_from_start < other_cell.distance_from_start
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
NUMBER_OF_ROWS = WINDOW_HEIGHT // 10
NUMBER_OF_COLUMNS = WINDOW_WIDTH // 10
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
def setup():
screen.fill(BLACK)
pygame.display.set_caption("Pathfinder")
initialize_matrix()
draw_grid()
def initialize_matrix():
global matrix
matrix = np.empty((NUMBER_OF_ROWS, NUMBER_OF_COLUMNS), dtype=Cell)
def draw_grid():
global start_coords, end_coords
MARGIN = 1
DIMENSION = (WINDOW_WIDTH - MARGIN * NUMBER_OF_COLUMNS) // NUMBER_OF_COLUMNS
for x in range(NUMBER_OF_ROWS):
for y in range(NUMBER_OF_COLUMNS):
rect = pygame.Rect((MARGIN + DIMENSION) * y + MARGIN, (MARGIN + DIMENSION) * x + MARGIN, DIMENSION, DIMENSION)
pygame.draw.rect(screen, WHITE, rect)
matrix[x][y] = Cell(rect.copy(), False, np.inf, (x, y))
start_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS))
end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS))
while start_coords == end_coords:
end_coords = (np.random.randint(0, NUMBER_OF_ROWS), np.random.randint(0, NUMBER_OF_COLUMNS))
start_cell = matrix[start_coords[0]][start_coords[1]]
end_cell = matrix[end_coords[0]][end_coords[1]]
start_rect = start_cell.figure
end_rect = end_cell.figure
pygame.draw.rect(screen, BLUE, start_rect)
pygame.draw.rect(screen, RED, end_rect)
matrix[start_coords[0]][start_coords[1]] = Cell(start_rect.copy(), True, 0, start_coords, visited=True)
matrix[end_coords[0]][end_coords[1]] = Cell(end_rect.copy(), False, np.inf, end_coords)
pygame.display.update()
def mark_as_wall():
position = pygame.mouse.get_pos()
col = position[0] // 10
row = position[1] // 10
start_cell = matrix[start_coords[0]][start_coords[1]]
end_cell = matrix[end_coords[0]][end_coords[1]]
if matrix[row][col] != start_cell and matrix[row][col] != end_cell:
matrix[row][col].is_wall = True
rect = matrix[row][col].figure
pygame.draw.rect(screen, BLACK, rect)
pygame.display.update()
def mark_as_visited(cell, distance, predecessor):
end_cell = matrix[end_coords[0]][end_coords[1]]
if distance < cell.distance_from_start:
cell.distance_from_start = distance
cell.visited = True
cell.predecessor = predecessor
if cell != end_cell:
rect = cell.figure
pygame.draw.rect(screen, YELLOW, rect)
pygame.display.update()
return False
return True
def highlight_path():
end_cell = matrix[end_coords[0]][end_coords[1]]
current = end_cell.predecessor
start = matrix[start_coords[0]][start_coords[1]]
while current != start:
rect = current.figure
pygame.draw.rect(screen, GREEN, rect)
pygame.display.update()
current = current.predecessor
def find_path():
start = matrix[start_coords[0]][start_coords[1]]
queue = [start]
found = True
while queue:
most_near = heapq.heappop(queue)
if most_near.distance_from_start == np.inf:
found = False
break
i, j = most_near.coords
new_distance = most_near.distance_from_start + 1
above = matrix[i-1][j] if 0 <= i-1 < NUMBER_OF_ROWS else None
below = matrix[i+1][j] if 0 <= i+1 < NUMBER_OF_ROWS else None
right = matrix[i][j+1] if 0 <= j+1 < NUMBER_OF_COLUMNS else None
left = matrix[i][j-1] if 0 <= j-1 < NUMBER_OF_COLUMNS else None
if above and not above.is_wall and not above.visited:
if mark_as_visited(above, new_distance, most_near):
break
heapq.heappush(queue, above)
if below and not below.is_wall and not below.visited:
if mark_as_visited(below, new_distance, most_near):
break
heapq.heappush(queue, below)
if right and not right.is_wall and not right.visited:
if mark_as_visited(right, new_distance, most_near):
break
heapq.heappush(queue, right)
if left and not left.is_wall and not left.visited:
if mark_as_visited(left, new_distance, most_near):
break
heapq.heappush(queue, left)
if all(value for value in [above != None, below != None, right != None, left != None]):
if all(value for value in [above.is_wall, below.is_wall, right.is_wall, left.is_wall]):
return
if found:
highlight_path()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
matrix = None
start_coords = None
end_coords = None
setup()
pygame.init()
running = True
dragging = False
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
dragging = True
if dragging and event.type == MOUSEMOTION:
mark_as_wall()
if event.type == MOUSEBUTTONUP:
dragging = False
if event.type == KEYDOWN:
if event.key == K_RETURN:
find_path()
if event.key == K_BACKSPACE:
setup()
pygame.quit()