def shortest_path_to_tavern(hero_id, status): hero = status.heroes[hero_id] hero_positions = set([h.pos for h in status.heroes]) def succ(node): initial_tile = status.map[node] if initial_tile != Tile.empty: return [] neighbours = status.map.get_neighbours(node) valid_cells = [ pos for pos, tile in neighbours.items() if tile != Tile.wall ] return set(valid_cells) - hero_positions is_goal = lambda node: ( status.map[node] == Tile.tavern ) path = astar.search( hero.pos, is_goal, succ, status.map.distance_to_tavern ) return path
def shortest_path_to_mine(hero_id, status): hero = status.heroes[hero_id] hero_positions = set([h.pos for h in status.heroes]) def succ(node): initial_tile = status.map[node] if initial_tile != Tile.empty: return [] neighbours = status.map.get_neighbours(node) valid_cells = [ pos for pos, tile in neighbours.items() if tile != Tile.wall ] return set(valid_cells) - hero_positions is_goal = lambda node: ( status.map[node] == Tile.mine and status.mine_owner[node] != hero.id ) goal_mines = [ pos for pos, owner in status.mine_owner.items() if owner != hero_id ] heuristic = lambda pos: status.map.distance_to_mines(pos, goal_mines) path = astar.search( hero.pos, is_goal, succ, heuristic ) return path