forked from nas-programmer/path-finding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.py
156 lines (130 loc) · 4.62 KB
/
bfs.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
"""Breadth First Search"""
import pygame, sys, random, math
from collections import deque
from tkinter import messagebox, Tk
size = (width, height) = 640, 480
pygame.init()
win = pygame.display.set_mode(size)
pygame.display.set_caption('Breadth First Search')
clock = pygame.time.Clock()
cols, rows = 64, 48
w = width//cols
h = height//rows
grid = []
queue, visited = deque(), []
path = []
class Spot:
def __init__(self, i, j):
self.x, self.y = i, j
self.f, self.g, self.h = 0, 0, 0
self.neighbors = []
self.prev = None
self.wall = False
self.visited = False
# if random.randint(0, 100) < 20:
# self.wall = True
def show(self, win, col):
if self.wall == True:
col = (0, 0, 0)
pygame.draw.rect(win, col, (self.x*w, self.y*h, w-1, h-1))
def add_neighbors(self, grid):
if self.x < cols - 1:
self.neighbors.append(grid[self.x+1][self.y])
if self.x > 0:
self.neighbors.append(grid[self.x-1][self.y])
if self.y < rows - 1:
self.neighbors.append(grid[self.x][self.y+1])
if self.y > 0:
self.neighbors.append(grid[self.x][self.y-1])
#Add Diagonals
# if self.x < cols - 1 and self.y < rows - 1:
# self.neighbors.append(grid[self.x+1][self.y+1])
# if self.x < cols - 1 and self.y > 0:
# self.neighbors.append(grid[self.x+1][self.y-1])
# if self.x > 0 and self.y < rows - 1:
# self.neighbors.append(grid[self.x-1][self.y+1])
# if self.x > 0 and self.y > 0:
# self.neighbors.append(grid[self.x-1][self.y-1])
def clickWall(pos, state):
i = pos[0] // w
j = pos[1] // h
grid[i][j].wall = state
def place(pos):
i = pos[0] // w
j = pos[1] // h
return w, h
for i in range(cols):
arr = []
for j in range(rows):
arr.append(Spot(i, j))
grid.append(arr)
for i in range(cols):
for j in range(rows):
grid[i][j].add_neighbors(grid)
start = grid[cols//2][rows//2]
end = grid[cols-1][rows - cols//2]
start.wall = False
end.wall = False
queue.append(start)
start.visited = True
def main():
flag = False
noflag = True
startflag = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
if pygame.mouse.get_pressed(0):
clickWall(pygame.mouse.get_pos(), True)
if pygame.mouse.get_pressed(2):
clickWall(pygame.mouse.get_pos(), False)
if event.type == pygame.MOUSEMOTION:
if pygame.mouse.get_pressed()[0]:
clickWall(pygame.mouse.get_pos(), True)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
startflag = True
if startflag:
if len(queue) > 0:
current = queue.popleft()
if current == end:
temp = current
while temp.prev:
path.append(temp.prev)
temp = temp.prev
if not flag:
flag = True
print("Done")
elif flag:
continue
if flag == False:
for i in current.neighbors:
if not i.visited and not i.wall:
i.visited = True
i.prev = current
queue.append(i)
else:
if noflag and not flag:
Tk().wm_withdraw()
messagebox.showinfo("No Solution", "There was no solution" )
noflag = False
else:
continue
win.fill((0, 20, 20))
for i in range(cols):
for j in range(rows):
spot = grid[i][j]
spot.show(win, (255, 255, 255))
if spot in path:
spot.show(win, (25, 120, 250))
elif spot.visited:
spot.show(win, (255, 0, 0))
if spot in queue:
spot.show(win, (0, 255, 0))
if spot == end:
spot.show(win, (0, 120, 255))
pygame.display.flip()
main()