|
1 | 1 | import heapq |
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 | | -def min_real(complexes): |
23 | | - real_values = [x.real for x in complexes] |
24 | | - return min(real_values) |
25 | | - |
26 | | - |
27 | | -def min_imag(complexes): |
28 | | - real_values = [x.imag for x in complexes] |
29 | | - return min(real_values) |
30 | | - |
31 | | - |
32 | | -def max_real(complexes): |
33 | | - real_values = [x.real for x in complexes] |
34 | | - return max(real_values) |
35 | | - |
36 | | - |
37 | | -def max_imag(complexes): |
38 | | - real_values = [x.imag for x in complexes] |
39 | | - return max(real_values) |
| 3 | +from complex_utils import * |
40 | 4 |
|
41 | 5 |
|
42 | 6 | class TargetFound(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 to convert |
125 | | - :param Boolean items: Whether diagonal movement is allowed |
| 88 | + :param string grid: The grid in which to search |
| 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 | | - for line in grid.splitlines(): |
| 95 | + for y, line in enumerate(grid.splitlines()): |
132 | 96 | for x in range(len(line)): |
133 | 97 | if line[x] in items: |
134 | 98 | if line[x] in items_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 | return items_found |
141 | 104 |
|
@@ -171,18 +134,41 @@ def vertices_to_grid(self, mark_coords=[], wall="#"): |
171 | 134 |
|
172 | 135 | return grid |
173 | 136 |
|
174 | | - def add_traps(self, vertex): |
| 137 | + def add_traps(self, vertices): |
175 | 138 | """ |
176 | 139 | Creates traps: places that can be reached, but not exited |
177 | 140 |
|
178 | | - :param Any vertex: The vertex to consider |
| 141 | + :param Any vertex: The vertices to consider |
179 | 142 | :return: True if successful, False if no vertex found |
180 | 143 | """ |
181 | | - if vertex in self.edges: |
182 | | - del self.edges[vertex] |
183 | | - return True |
184 | | - else: |
185 | | - return False |
| 144 | + changed = False |
| 145 | + for vertex in vertices: |
| 146 | + if vertex in self.edges: |
| 147 | + del self.edges[vertex] |
| 148 | + changed = True |
| 149 | + |
| 150 | + return changed |
| 151 | + |
| 152 | + def add_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 | + for vertex in vertices: |
| 161 | + if vertex in self.edges: |
| 162 | + del self.edges[vertex] |
| 163 | + del self.vertices[vertex] |
| 164 | + changed = True |
| 165 | + |
| 166 | + self.edges = { |
| 167 | + source: [target for target in self.edges[source] if target not in vertices] |
| 168 | + for source in self.edges |
| 169 | + } |
| 170 | + |
| 171 | + return changed |
186 | 172 |
|
187 | 173 | def depth_first_search(self, start, end=None): |
188 | 174 | """ |
|
0 commit comments