Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1eecc0b

Browse files
committed
Pathfinding: added ability to add walls + various fixes
1 parent3d6a72f commit1eecc0b

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

‎2018/pathfinding.py‎

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
11
importheapq
22

3-
# Cardinal directions
4-
north=1j
5-
south=-1j
6-
west=-1
7-
east=1
8-
northeast=1+1j
9-
northwest=-1+1j
10-
southeast=1-1j
11-
southwest=-1-1j
12-
13-
directions_straight= [north,south,west,east]
14-
directions_diagonals=directions_straight+ [
15-
northeast,
16-
northwest,
17-
southeast,
18-
southwest,
19-
]
20-
21-
22-
defmin_real(complexes):
23-
real_values= [x.realforxincomplexes]
24-
returnmin(real_values)
25-
26-
27-
defmin_imag(complexes):
28-
real_values= [x.imagforxincomplexes]
29-
returnmin(real_values)
30-
31-
32-
defmax_real(complexes):
33-
real_values= [x.realforxincomplexes]
34-
returnmax(real_values)
35-
36-
37-
defmax_imag(complexes):
38-
real_values= [x.imagforxincomplexes]
39-
returnmax(real_values)
3+
fromcomplex_utilsimport*
404

415

426
classTargetFound(Exception):
@@ -121,21 +85,20 @@ def grid_search(self, grid, items):
12185
"""
12286
Searches the grid for some items
12387
124-
:param string grid: The grid toconvert
125-
:param Boolean items:Whether diagonal movement is allowed
88+
:param string grid: The gridin whichtosearch
89+
:param Boolean items:The items to search
12690
:return: True if the grid was converted
12791
"""
12892
items_found= {}
12993
y=0
13094

131-
forlineingrid.splitlines():
95+
fory,lineinenumerate(grid.splitlines()):
13296
forxinrange(len(line)):
13397
ifline[x]initems:
13498
ifline[x]initems_found:
13599
items_found[line[x]].append(x-y*1j)
136100
else:
137101
items_found[line[x]]= [x-y*1j]
138-
y+=1
139102

140103
returnitems_found
141104

@@ -171,18 +134,41 @@ def vertices_to_grid(self, mark_coords=[], wall="#"):
171134

172135
returngrid
173136

174-
defadd_traps(self,vertex):
137+
defadd_traps(self,vertices):
175138
"""
176139
Creates traps: places that can be reached, but not exited
177140
178-
:param Any vertex: Thevertex to consider
141+
:param Any vertex: Thevertices to consider
179142
:return: True if successful, False if no vertex found
180143
"""
181-
ifvertexinself.edges:
182-
delself.edges[vertex]
183-
returnTrue
184-
else:
185-
returnFalse
144+
changed=False
145+
forvertexinvertices:
146+
ifvertexinself.edges:
147+
delself.edges[vertex]
148+
changed=True
149+
150+
returnchanged
151+
152+
defadd_walls(self,vertices):
153+
"""
154+
Adds walls - useful for modification of map
155+
156+
:param Any vertex: The vertices to consider
157+
:return: True if successful, False if no vertex found
158+
"""
159+
changed=False
160+
forvertexinvertices:
161+
ifvertexinself.edges:
162+
delself.edges[vertex]
163+
delself.vertices[vertex]
164+
changed=True
165+
166+
self.edges= {
167+
source: [targetfortargetinself.edges[source]iftargetnotinvertices]
168+
forsourceinself.edges
169+
}
170+
171+
returnchanged
186172

187173
defdepth_first_search(self,start,end=None):
188174
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp