forked from nas-programmer/path-finding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.py
189 lines (146 loc) · 5.27 KB
/
astar.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
import pygame, sys, random, math
from tkinter import messagebox, Tk
size = (width, height) = 600, 600
pygame.init()
win = pygame.display.set_mode(size)
clock = pygame.time.Clock()
cols, rows = 50, 50
grid = []
openSet, closeSet = [], []
path = []
w = width//cols
h = height//rows
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
# 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
def heuristics(a, b):
return math.sqrt((a.x - b.x)**2 + abs(a.y - b.y)**2)
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[0][0]
end = grid[cols - cols//2][rows - cols//4]
openSet.append(start)
def close():
pygame.quit()
sys.exit()
def main():
flag = False
noflag = True
startflag = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
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(openSet) > 0:
winner = 0
for i in range(len(openSet)):
if openSet[i].f < openSet[winner].f:
winner = i
current = openSet[winner]
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:
openSet.remove(current)
closeSet.append(current)
for neighbor in current.neighbors:
if neighbor in closeSet or neighbor.wall:
continue
tempG = current.g + 1
newPath = False
if neighbor in openSet:
if tempG < neighbor.g:
neighbor.g = tempG
newPath = True
else:
neighbor.g = tempG
newPath = True
openSet.append(neighbor)
if newPath:
neighbor.h = heuristics(neighbor, end)
neighbor.f = neighbor.g + neighbor.h
neighbor.prev = current
else:
if noflag:
Tk().wm_withdraw()
messagebox.showinfo("No Solution", "There was no solution" )
noflag = False
win.fill((0, 20, 20))
for i in range(cols):
for j in range(rows):
spot = grid[j][i]
spot.show(win, (255, 255, 255))
if flag and spot in path:
spot.show(win, (25, 120, 250))
elif spot in closeSet:
spot.show(win, (255, 0, 0))
elif spot in openSet:
spot.show(win, (0, 255, 0))
try:
if spot == end:
spot.show(win, (0, 120, 255))
except Exception:
pass
pygame.display.flip()
main()