-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_finding.py
244 lines (207 loc) · 6.61 KB
/
path_finding.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
# importing libs
try:
import pygame
import sys
import math
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
except:
import install_requirements # install packages
import pygame
import sys
import math
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
screen = pygame.display.set_mode((800, 800))
class spot:
def __init__(self, x, y):
self.i = x
self.j = y
self.f = 0
self.g = 0
self.h = 0
self.neighbors = []
self.previous = None
self.obs = False
self.closed = False
self.value = 1
def show(self, color, st):
if self.closed == False :
pygame.draw.rect(screen, color, (self.i * w, self.j * h, w, h), st)
pygame.display.update()
def path(self, color, st):
pygame.draw.rect(screen, color, (self.i * w, self.j * h, w, h), st)
pygame.display.update()
def addNeighbors(self, grid):
i = self.i
j = self.j
if i < cols-1 and grid[self.i + 1][j].obs == False:
self.neighbors.append(grid[self.i + 1][j])
if i > 0 and grid[self.i - 1][j].obs == False:
self.neighbors.append(grid[self.i - 1][j])
if j < row-1 and grid[self.i][j + 1].obs == False:
self.neighbors.append(grid[self.i][j + 1])
if j > 0 and grid[self.i][j - 1].obs == False:
self.neighbors.append(grid[self.i][j - 1])
cols = 50
grid = [0 for i in range(cols)]
row = 50
openSet = []
closedSet = []
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
grey = (220, 220, 220)
w = 800 / cols
h = 800 / row
cameFrom = []
# create 2d array
for i in range(cols):
grid[i] = [0 for i in range(row)]
# Create Spots
for i in range(cols):
for j in range(row):
grid[i][j] = spot(i, j)
# Set start and end node
start = grid[12][5]
end = grid[3][6]
# SHOW RECT
for i in range(cols):
for j in range(row):
grid[i][j].show((255, 255, 255), 1)
for i in range(0,row):
grid[0][i].show(grey, 0)
grid[0][i].obs = True
grid[cols-1][i].obs = True
grid[cols-1][i].show(grey, 0)
grid[i][row-1].show(grey, 0)
grid[i][0].show(grey, 0)
grid[i][0].obs = True
grid[i][row-1].obs = True
def onsubmit():
global start
global end
st = startBox.get().split(',')
ed = endBox.get().split(',')
start = grid[int(st[0])][int(st[1])]
end = grid[int(ed[0])][int(ed[1])]
window.quit()
window.destroy()
window = Tk()
label = Label(window, text='Start(x,y): ')
startBox = Entry(window)
label1 = Label(window, text='End(x,y): ')
endBox = Entry(window)
var = IntVar()
showPath = ttk.Checkbutton(window, text='Show Steps :', onvalue=1, offvalue=0, variable=var)
submit = Button(window, text='Submit', command=onsubmit)
showPath.grid(columnspan=2, row=2)
submit.grid(columnspan=2, row=3)
label1.grid(row=1, pady=3)
endBox.grid(row=1, column=1, pady=3)
startBox.grid(row=0, column=1, pady=3)
label.grid(row=0, pady=3)
window.update()
mainloop()
pygame.init()
openSet.append(start)
def mousePress(x):
t = x[0]
w = x[1]
g1 = t // (800 // cols)
g2 = w // (800 // row)
acess = grid[g1][g2]
if acess != start and acess != end:
if acess.obs == False:
acess.obs = True
acess.show((255, 255, 255), 0)
end.show((255, 8, 127), 0)
start.show((255, 8, 127), 0)
loop = True
while loop:
ev = pygame.event.get()
for event in ev:
if event.type == pygame.QUIT:
pygame.quit()
if pygame.mouse.get_pressed()[0]:
try:
pos = pygame.mouse.get_pos()
mousePress(pos)
except AttributeError:
pass
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
loop = False
break
for i in range(cols):
for j in range(row):
grid[i][j].addNeighbors(grid)
def heurisitic(n, e):
d = math.sqrt((n.i - e.i)**2 + (n.j - e.j)**2)
#d = abs(n.i - e.i) + abs(n.j - e.j)
return d
def main():
end.show((255, 8, 127), 0)
start.show((255, 8, 127), 0)
if len(openSet) > 0:
lowestIndex = 0
for i in range(len(openSet)):
if openSet[i].f < openSet[lowestIndex].f:
lowestIndex = i
current = openSet[lowestIndex]
if current == end:
print('done', current.f)
start.show((255,8,127),0)
temp = current.f
for i in range(round(current.f)):
current.closed = False
current.show((0,0,255), 0)
current = current.previous
end.show((255, 8, 127), 0)
Tk().wm_withdraw()
result = messagebox.askokcancel('Program Finished', ('The program finished, the shortest distance \n to the path is ' + str(temp) + ' blocks away, \n would you like to re run the program?'))
if result == True:
os.execl(sys.executable,sys.executable, *sys.argv)
else:
ag = True
while ag:
ev = pygame.event.get()
for event in ev:
if event.type == pygame.KEYDOWN:
ag = False
break
pygame.quit()
openSet.pop(lowestIndex)
closedSet.append(current)
neighbors = current.neighbors
for i in range(len(neighbors)):
neighbor = neighbors[i]
if neighbor not in closedSet:
tempG = current.g + current.value
if neighbor in openSet:
if neighbor.g > tempG:
neighbor.g = tempG
else:
neighbor.g = tempG
openSet.append(neighbor)
neighbor.h = heurisitic(neighbor, end)
neighbor.f = neighbor.g + neighbor.h
if neighbor.previous == None:
neighbor.previous = current
if var.get():
for i in range(len(openSet)):
openSet[i].show(green, 0)
for i in range(len(closedSet)):
if closedSet[i] != start:
closedSet[i].show(red, 0)
current.closed = True
while True:
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
pygame.quit()
pygame.display.update()
main()