-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
368 lines (299 loc) · 12.8 KB
/
runner.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
import sys
import time
import pygame
import numpy as np
from settings import *
from pathfinder import Grid
def main():
# Initialise algorithm variables
algorithm = None
# Check usage
if len(sys.argv) in [2, 3]:
# Parse the command line arguments:
algorithm = sys.argv[1]
if algorithm.lower() not in algorithms:
sys.exit("This algorithm has not been implemented yet")
else:
# Use a* as default algorithm but print usage
algorithm = "asearch"
print("------------------------------------------------")
print("Suggested Usage: python runner.py algorithm maze")
print("------------------------------------------------")
# Create game
pygame.init()
pygame.display.set_caption("Pathfinder")
icon = pygame.image.load("assets/images/icon.png")
pygame.display.set_icon(icon)
screen = pygame.display.set_mode(size)
# Fonts
WALKWAY = "assets/fonts/Walkway_UltraBold.ttf"
small_font = pygame.font.Font(WALKWAY, 20)
medium_font = pygame.font.Font(WALKWAY, 28)
large_font = pygame.font.Font(WALKWAY, 40)
# Add the flag (end) and pin (start) image
pin = pygame.image.load("assets/images/pin.jpeg")
pin = pygame.transform.scale(pin, (cell_size - 2, cell_size - 2))
flag = pygame.image.load("assets/images/flag.png")
flag = pygame.transform.scale(flag, (cell_size - 2, cell_size - 2))
# Initialise the grid
grid = Grid(HEIGHT, WIDTH, board_origin, cell_size, pin, flag)
cells = grid.cells
mask = None
# Generate maze if asked
try:
if sys.argv[2]:
mask = np.random.randint(0, 2, (HEIGHT, WIDTH))
grid.generate_maze(mask)
except IndexError:
pass
# Set logical barriers
instructions = True
start = True
end = True
barriers = True
search = True
found = False
path = False
# Create draw board function
def draw_board(cells, board_origin, cell_size, screen, grid) -> None:
"""Draw the rects for the nodes."""
for row in cells:
for node in row:
node.draw(board_origin, cell_size, screen)
if node.obstruction:
node.fill(screen, WHITE)
elif node.start:
screen.blit(pin, node.rect)
elif node.end:
screen.blit(flag, node.rect)
elif node.path:
node.fill(screen, BLUE)
elif node in grid.closed:
node.fill(screen, RED)
elif node in grid.open:
node.fill(screen, GREEN)
while True:
# Check if game is quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(BLACK)
# Show visualiser instructions
if instructions:
# Title
title = large_font.render("Pathfinding Visualiser", True, WHITE)
title_rect = title.get_rect()
title_rect.center = ((width / 2), 100)
screen.blit(title, title_rect)
# Description
description = [
"You get to choose the start and end point of the pathfinder",
f"Using the {algorithms[algorithm]} pathfinding algorithm,",
"the shortest path will be calculated",
]
for i, sentence in enumerate(description):
line = small_font.render(sentence, True, WHITE)
line_rect = line.get_rect()
line_rect.center = ((width / 2), 215 + 30 * i)
screen.blit(line, line_rect)
# Start button
button_rect = pygame.Rect((width / 4), (3 / 4) * height, width / 2, 50)
button_text = medium_font.render("Start", True, BLACK)
button_text_rect = button_text.get_rect()
button_text_rect.center = button_rect.center
pygame.draw.rect(screen, WHITE, button_rect)
screen.blit(button_text, button_text_rect)
# Check button input
click, _, _ = pygame.mouse.get_pressed()
if click == 1:
mouse = pygame.mouse.get_pos()
if button_rect.collidepoint(mouse):
instructions = False
time.sleep(0.3)
# Show instructions to place start node
elif start:
# Write instructions on screen
instruction = medium_font.render("Place start node", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 2), 50)
screen.blit(instruction, instruction_rect)
# Draw the board
grid.draw_board()
# Add start node
left, _, _ = pygame.mouse.get_pressed()
if left == 1:
mouse = pygame.mouse.get_pos()
for i in range(HEIGHT):
for j in range(WIDTH):
node = cells[i][j]
if node.rect.collidepoint(mouse) and not node.obstruction:
# Marks the node as start
node.start = True
grid.start = node
start = False
time.sleep(0.3)
# Show instructions to place end node
elif end:
# Write instructions
instruction = medium_font.render("Place end node", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 2), 50)
screen.blit(instruction, instruction_rect)
# Draw the board
draw_board(cells, board_origin, cell_size, screen, grid)
# Add end node
left, _, _ = pygame.mouse.get_pressed()
if left == 1:
mouse = pygame.mouse.get_pos()
for i in range(HEIGHT):
for j in range(WIDTH):
node = cells[i][j]
if node.rect.collidepoint(mouse) and not node.obstruction:
# Marks the node as start
if not node.start:
node.end = True
grid.end = node
end = False
time.sleep(0.3)
# Show instructions to draw barriers
elif barriers:
# Write instructions
instruction = medium_font.render("Draw barriers", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 3), 50)
screen.blit(instruction, instruction_rect)
# Search button
search_button = pygame.Rect((width * (1 / 2)) + BOARD_PADDING, 30, 100, 40)
search_button_text = medium_font.render("Search", True, BLACK)
search_button_rect = search_button_text.get_rect()
search_button_rect.center = search_button.center
pygame.draw.rect(screen, WHITE, search_button)
screen.blit(search_button_text, search_button_rect)
# Reset button
reset_button = pygame.Rect(
(width * (1 / 2)) + BOARD_PADDING + 115, 30, 100, 40
)
reset_button_text = medium_font.render("Reset", True, BLACK)
reset_button_rect = reset_button_text.get_rect()
reset_button_rect.center = reset_button.center
pygame.draw.rect(screen, WHITE, reset_button)
screen.blit(reset_button_text, reset_button_rect)
# Draw the board
draw_board(cells, board_origin, cell_size, screen, grid)
# Check buttons or grid pressed
left, _, _ = pygame.mouse.get_pressed()
if left == 1:
mouse = pygame.mouse.get_pos()
# If search button is clicked, start the search
if search_button.collidepoint(mouse):
barriers = False
time.sleep(0.3)
elif reset_button.collidepoint(mouse):
# Re-initialise all the variables
grid = Grid(HEIGHT, WIDTH, board_origin, cell_size, pin, flag)
if mask is not None:
grid.generate_maze(mask)
cells = grid.cells
start = True
end = True
barriers = True
else:
for i in range(HEIGHT):
for j in range(WIDTH):
node = cells[i][j]
if node.rect.collidepoint(mouse):
if node.start or node.end:
continue
else:
# Marks the node as an obstruction
node.obstruction = True
# Show search
elif search:
# Write instructions
instruction = medium_font.render("Searching...", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 2), 50)
screen.blit(instruction, instruction_rect)
algos = {
"A* search": grid.asearch,
"Djikstra's": grid.djikstra,
"Greedy": grid.greedy,
"Breadth First Search": grid.bfs,
"Depth First Search": grid.dfs,
}
# Start algorithm search depending on input
algo = algorithms[algorithm.lower()]
if algos[algo]():
found = True
path = True
search = False
else:
search = False
# Once the node has been found
elif found:
# Write instructions
instruction = medium_font.render("Path Found!", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 3), 50)
screen.blit(instruction, instruction_rect)
# Reset button
reset_button = pygame.Rect(
(width * (1 / 2)) + BOARD_PADDING + 30, 30, 100, 40
)
reset_button_text = medium_font.render("Reset", True, BLACK)
reset_button_rect = reset_button_text.get_rect()
reset_button_rect.center = reset_button.center
pygame.draw.rect(screen, WHITE, reset_button)
screen.blit(reset_button_text, reset_button_rect)
# Draw the path
grid.find_path()
# Draw the board
draw_board(cells, board_origin, cell_size, screen, grid)
# Check reset button pressed
left, _, _ = pygame.mouse.get_pressed()
if left == 1:
mouse = pygame.mouse.get_pos()
if reset_button.collidepoint(mouse):
# Re-initialise all the variables
grid = Grid(HEIGHT, WIDTH, board_origin, cell_size, pin, flag)
if mask is not None:
grid.generate_maze(mask)
cells = grid.cells
start = True
end = True
barriers = True
search = True
else:
# Write instructions
instruction = medium_font.render("No path found...", True, WHITE)
instruction_rect = instruction.get_rect()
instruction_rect.center = ((width / 3), 50)
screen.blit(instruction, instruction_rect)
# Reset button
reset_button = pygame.Rect(
(width * (1 / 2)) + BOARD_PADDING + 30, 30, 100, 40
)
reset_button_text = medium_font.render("Reset", True, BLACK)
reset_button_rect = reset_button_text.get_rect()
reset_button_rect.center = reset_button.center
pygame.draw.rect(screen, WHITE, reset_button)
screen.blit(reset_button_text, reset_button_rect)
# Draw the board
draw_board(cells, board_origin, cell_size, screen, grid)
# Check reset button pressed
left, _, _ = pygame.mouse.get_pressed()
if left == 1:
mouse = pygame.mouse.get_pos()
if reset_button.collidepoint(mouse):
# Re-initialise all the variables
grid = Grid(HEIGHT, WIDTH, board_origin, cell_size, pin, flag)
cells = grid.cells
if mask is not None:
grid.generate_maze(mask)
start = True
end = True
barriers = True
search = True
pygame.display.flip()
if __name__ == "__main__":
main()