-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwindow.py
131 lines (110 loc) · 6.24 KB
/
window.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
from constants import width, height, square_size, blue2, rows, cols
from node import Node
import pygame
class Window:
def __init__(self):
pygame.init()
pygame.font.init()
pygame.display.set_caption("Pathfinding Visualizer by Jeffery Xie")
self.font = pygame.font.SysFont("calibri", 15)
self.win = pygame.display.set_mode((width, height))
self.selected_algorithm = None
self.speed = "Fast"
self.paused = False
self.previous_results = []
def make_grid(self):
# Make an N by N grid with each index implemented as a node
grid = []
for i in range(rows):
grid.append([])
for j in range(cols):
grid[i].append(Node(i, j))
return grid
def draw_grid(self):
for i in range(rows + 1):
pygame.draw.line(self.win, (0, 0, 0), (i * square_size, 0), (i * square_size, width), 1)
pygame.draw.line(self.win, (0, 0, 0), (0, i * square_size), (width, i * square_size), 1)
def draw_buttons(self):
# A* Algorithm
pygame.draw.rect(self.win, (0, 0, 0), (square_size, square_size * 41 + 10, square_size * 8, square_size * 2))
if self.selected_algorithm == "a_star":
pygame.draw.rect(self.win, blue2, (square_size + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
else:
pygame.draw.rect(self.win, (255, 255, 255), (square_size + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render("A* Search", True, (0, 0, 0))
self.win.blit(text, (square_size * 3, square_size * 42 + 2))
# Greedy BFS Algorithm
pygame.draw.rect(self.win, (0, 0, 0), (square_size, square_size * 44, square_size * 8, square_size * 2))
if self.selected_algorithm == "greedy_best_first_search":
pygame.draw.rect(self.win, blue2, (square_size + 1, square_size * 44 + 1, square_size * 8 - 2, square_size * 2 - 2))
else:
pygame.draw.rect(self.win, (255, 255, 255), (square_size + 1, square_size * 44 + 1, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render("Greedy Best First", True, (0, 0, 0))
self.win.blit(text, (square_size + 9, square_size * 44 + 7))
# BFS Algorithm
pygame.draw.rect(self.win, (0, 0, 0), (square_size * 19, square_size * 44, square_size * 8, square_size * 2))
if self.selected_algorithm == "breadth_first_search":
pygame.draw.rect(self.win, blue2, (square_size * 19 + 1, square_size * 44 + 1, square_size * 8 - 2, square_size * 2 - 2))
else:
pygame.draw.rect(self.win, (255, 255, 255), (square_size * 19 + 1, square_size * 44 + 1, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render("Breadth First", True, (0, 0, 0))
self.win.blit(text, (square_size * 20 + 6, square_size * 44 + 7))
# Dijkstra's Algorithm
pygame.draw.rect(self.win, (0, 0, 0), (square_size * 10, square_size * 41 + 10, square_size * 8, square_size * 2))
if self.selected_algorithm == "dijkstras":
pygame.draw.rect(self.win, blue2, (square_size * 10 + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
else:
pygame.draw.rect(self.win, (255, 255, 255), (square_size * 10 + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render("Dijkstra's Algorithm", True, (0, 0, 0))
self.win.blit(text, (square_size * 10 + 2, square_size * 42 + 2))
# DFS Algorithm
pygame.draw.rect(self.win, (0, 0, 0), (square_size * 19, square_size * 41 + 10, square_size * 8, square_size * 2))
if self.selected_algorithm == "depth_first_search":
pygame.draw.rect(self.win, blue2, (square_size * 19 + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
else:
pygame.draw.rect(self.win, (255, 255, 255), (square_size * 19 + 1, square_size * 41 + 11, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render("Depth First", True, (0, 0, 0))
self.win.blit(text, (square_size * 20 + 12, square_size * 42 + 2))
# Change Speed Button
pygame.draw.rect(self.win, (0, 0, 0), (square_size * 10, square_size * 44, square_size * 8, square_size * 2))
pygame.draw.rect(self.win, (255, 255, 255), (square_size * 10 + 1, square_size * 44 + 1, square_size * 8 - 2, square_size * 2 - 2))
text = self.font.render(self.speed, True, (0, 0, 0))
if self.speed in ("Fast", "Slow"):
self.win.blit(text, (square_size * 13 + 3, square_size * 44 + 7))
else:
self.win.blit(text, (square_size * 12 + 6, square_size * 44 + 7))
def draw_results(self):
if self.previous_results:
for i in range(len(self.previous_results)):
text = self.font.render(self.previous_results[i], True, (0, 0, 0))
self.win.blit(text, (square_size * 27 + 10, square_size * (41 + i) + 12))
def draw_solution(self, start, end, path, draw):
# Total cost (sum of the weights of all nodes from start to end) of path found
cost = 0
end.place_end()
# Backtrack from end node to start node and draw the path found
current = end
while current in path:
if current not in (start, end):
cost += current.weight
current = path[current]
current.draw_path()
draw()
start.place_start()
return cost
def draw(self, grid):
self.win.fill((255, 255, 255))
for row in grid:
for node in row:
node.draw(self.win)
if node.weight != 1:
text = self.font.render("9", True, (0, 0, 0))
self.win.blit(text, (node.row * square_size + 4, node.col * square_size))
self.draw_grid()
self.draw_buttons()
self.draw_results()
pygame.display.update()
def get_mouse_position(self, pos):
row = pos[0] // square_size
col = pos[1] // square_size
return row, col