|
1 | 1 | importheapq |
2 | 2 |
|
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* |
40 | 4 |
|
41 | 5 |
|
42 | 6 | classTargetFound(Exception): |
@@ -121,21 +85,20 @@ def grid_search(self, grid, items): |
121 | 85 | """ |
122 | 86 | Searches the grid for some items |
123 | 87 |
|
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 |
126 | 90 | :return: True if the grid was converted |
127 | 91 | """ |
128 | 92 | items_found= {} |
129 | 93 | y=0 |
130 | 94 |
|
131 | | -forlineingrid.splitlines(): |
| 95 | +fory,lineinenumerate(grid.splitlines()): |
132 | 96 | forxinrange(len(line)): |
133 | 97 | ifline[x]initems: |
134 | 98 | ifline[x]initems_found: |
135 | 99 | items_found[line[x]].append(x-y*1j) |
136 | 100 | else: |
137 | 101 | items_found[line[x]]= [x-y*1j] |
138 | | -y+=1 |
139 | 102 |
|
140 | 103 | returnitems_found |
141 | 104 |
|
@@ -171,18 +134,41 @@ def vertices_to_grid(self, mark_coords=[], wall="#"): |
171 | 134 |
|
172 | 135 | returngrid |
173 | 136 |
|
174 | | -defadd_traps(self,vertex): |
| 137 | +defadd_traps(self,vertices): |
175 | 138 | """ |
176 | 139 | Creates traps: places that can be reached, but not exited |
177 | 140 |
|
178 | | - :param Any vertex: Thevertex to consider |
| 141 | + :param Any vertex: Thevertices to consider |
179 | 142 | :return: True if successful, False if no vertex found |
180 | 143 | """ |
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 |
186 | 172 |
|
187 | 173 | defdepth_first_search(self,start,end=None): |
188 | 174 | """ |
|