-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath Finding.py
More file actions
223 lines (207 loc) · 8.05 KB
/
Copy pathPath Finding.py
File metadata and controls
223 lines (207 loc) · 8.05 KB
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
import pygame as pg
vec = pg.math.Vector2
TILESIZE = 12
GRIDWIDTH = 48
GRIDHEIGHT = 36
WIDTH = TILESIZE * GRIDWIDTH
HEIGHT = TILESIZE * GRIDHEIGHT
FPS = 30
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
DARKGRAY = (40, 40, 40)
LIGHTGRAY = (140, 140, 140)
EMPTY = []
pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()
class SquareGrid:
def __init__(self, width, height):
self.width = width
self.height = height
self.walls = []
self.start = []
self.finish = []
self.exploring = []
self.explored = []
self.to_explore = []
self.solution = []
self.path = []
self.connections = [vec(1, 0), vec(-1, 0), vec(0, 1), vec(0, -1)]
def in_bounds(self, node):
return 0 <= node.x < self.width and 0 <= node.y < self.height
def passable(self, node):
return node not in self.walls
def find_neighbors(self, node):
neighbors = [node + connection for connection in self.connections]
neighbors = filter(self.in_bounds, neighbors)
neighbors = filter(self.passable, neighbors)
return neighbors
def draw(self):
for wall in self.walls:
rect = pg.Rect(wall * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, LIGHTGRAY, rect)
for start in self.start:
rect = pg.Rect(start * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, RED, rect)
for finish in self.finish:
rect = pg.Rect(finish * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, GREEN, rect)
for exploring in self.exploring:
rect = pg.Rect(exploring * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, YELLOW, rect)
for explored in self.explored:
rect = pg.Rect(explored * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, CYAN, rect)
for to_explore in self.to_explore:
rect = pg.Rect(to_explore * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, MAGENTA, rect)
for solution in self.solution:
rect = pg.Rect(solution * TILESIZE, (TILESIZE, TILESIZE))
pg.draw.rect(screen, RED, rect)
def draw_grid():
for x in range(0, WIDTH, TILESIZE):
pg.draw.line(screen, LIGHTGRAY, (x, 0), (x, HEIGHT))
for y in range(0, HEIGHT, TILESIZE):
pg.draw.line(screen, LIGHTGRAY, (0, y), (WIDTH, y))
g = SquareGrid(GRIDWIDTH, GRIDHEIGHT)
def path_finding():
g.exploring.append(g.start[0])
path_counter = 1
path=[g.start]
Found=False
while(not Found):
for node in g.exploring:
next_node1 = vec(int(node[0]) + 1, int(node[1]))
next_node2 = vec(int(node[0]), int(node[1]) + 1)
next_node3 = vec(int(node[0]) - 1, int(node[1]))
next_node4 = vec(int(node[0]), int(node[1]) - 1)
if next_node1 in g.finish:
for solution in path:
if solution[-1]==node:
g.solution = solution
Found=True
elif next_node2 in g.finish:
for solution in path:
if solution[-1]==node:
g.solution = solution
Found=True
elif next_node3 in g.finish:
for solution in path:
if solution[-1]==node:
g.solution = solution
Found=True
elif next_node4 in g.finish:
for solution in path:
if solution[-1]==node:
g.solution = solution
Found=True
else:
g.explored.append(node)
if next_node1 not in g.to_explore:
if next_node1 not in g.walls and next_node1 not in g.explored:
g.to_explore.append(next_node1)
for i in path:
if i[-1]==node:
j=[]
for item in i:
j.append(item)
j.append(next_node1)
path.append(j)
break
if next_node2 not in g.to_explore:
if next_node2 not in g.walls and next_node2 not in g.explored:
g.to_explore.append(next_node2)
for i in path:
if i[-1]==node:
j = []
for item in i:
j.append(item)
j.append(next_node2)
path.append(j)
break
if next_node3 not in g.to_explore:
if next_node3 not in g.walls and next_node3 not in g.explored:
g.to_explore.append(next_node3)
for i in path:
if i[-1]==node:
j = []
for item in i:
j.append(item)
j.append(next_node3)
path.append(j)
break
if next_node4 not in g.to_explore:
if next_node4 not in g.walls and next_node4 not in g.explored:
g.to_explore.append(next_node4)
for i in path:
if i[-1]==node:
j = []
for item in i:
j.append(item)
j.append(next_node4)
path.append(j)
break
g.exploring = g.to_explore
g.to_explore=[]
path_counter += 1
temp_path = []
for paths in path:
if len(paths)==path_counter:
temp_path.append(paths)
path = temp_path
print(path_counter)
walls = []
for i in range(GRIDWIDTH):
walls.append((i,0))
walls.append((i,GRIDHEIGHT-1))
for i in range(GRIDHEIGHT-2):
walls.append((0,i+1))
walls.append((GRIDWIDTH-1,i+1))
startingPos = []
for wall in walls:
g.walls.append(vec(wall))
running = True
while running:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
running = False
if event.key == pg.K_m:
# dump the wall list for saving
print([(int(loc.x), int(loc.y)) for loc in g.walls])
if event.key == pg.K_s:
mpos = vec(pg.mouse.get_pos()) // TILESIZE
if mpos in g.walls:
g.walls.remove(mpos)
if g.start!=EMPTY:
g.start.pop()
g.start.append(mpos)
if event.key == pg.K_f:
mpos = vec(pg.mouse.get_pos()) // TILESIZE
if mpos in g.walls:
g.walls.remove(mpos)
if g.finish!=EMPTY:
g.finish.pop()
g.finish.append(mpos)
if event.key == pg.K_g:
path_finding()
if event.type == pg.MOUSEBUTTONDOWN:
mpos = vec(pg.mouse.get_pos()) // TILESIZE
if event.button == 1:
if mpos in g.walls:
g.walls.remove(mpos)
else:
g.walls.append(mpos)
pg.display.set_caption("{:.2f}".format(clock.get_fps()))
screen.fill(DARKGRAY)
draw_grid()
g.draw()
pg.display.flip()