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