|
|
@ -81,19 +81,19 @@ class Field: |
|
|
|
def __hash__(self) -> int: |
|
|
|
return hash((self.i, self.j)) |
|
|
|
|
|
|
|
def get_neighbour_field(self, action: Direction) -> Field: |
|
|
|
def get_neighbour_field(self, direction: Direction) -> Field: |
|
|
|
"""Get the next field in the given direction.""" |
|
|
|
|
|
|
|
if action == Direction.UP: |
|
|
|
if direction == Direction.UP: |
|
|
|
neighbour_i = self.i - 1 |
|
|
|
neighbour_j = self.j |
|
|
|
elif action == Direction.DOWN: |
|
|
|
elif direction == Direction.DOWN: |
|
|
|
neighbour_i = self.i + 1 |
|
|
|
neighbour_j = self.j |
|
|
|
elif action == Direction.LEFT: |
|
|
|
elif direction == Direction.LEFT: |
|
|
|
neighbour_i = self.i |
|
|
|
neighbour_j = self.j - 1 |
|
|
|
elif action == Direction.RIGHT: |
|
|
|
elif direction == Direction.RIGHT: |
|
|
|
neighbour_i = self.i |
|
|
|
neighbour_j = self.j + 1 |
|
|
|
else: |
|
|
@ -201,8 +201,8 @@ def find_fields(predicate: Callable[[Field], bool]) -> List[Field]: |
|
|
|
def pathfind(member: Member, goal: Field) -> Direction: |
|
|
|
"""Find the shortest path from member position to goal. |
|
|
|
|
|
|
|
Returns the first action to take. |
|
|
|
If there is no path, returns Action.STAY. |
|
|
|
Returns the first direction to go in. |
|
|
|
If there is no path, returns STAY. |
|
|
|
""" |
|
|
|
|
|
|
|
# BFS from goal to member |
|
|
@ -211,13 +211,13 @@ def pathfind(member: Member, goal: Field) -> Direction: |
|
|
|
queue = collections.deque([goal]) |
|
|
|
while queue: |
|
|
|
field = queue.popleft() |
|
|
|
for action in Direction: |
|
|
|
neighbour = field.get_neighbour_field(action) |
|
|
|
for direction in Direction: |
|
|
|
neighbour = field.get_neighbour_field(direction) |
|
|
|
if (neighbour in explored or |
|
|
|
not neighbour.is_accessible(member.team)): |
|
|
|
continue |
|
|
|
if neighbour == member.field: |
|
|
|
return action.invert() |
|
|
|
return direction.invert() |
|
|
|
queue.append(neighbour) |
|
|
|
explored.add(neighbour) |
|
|
|
return Direction.STAY |
|
|
|