Exemplo n.º 1
0
    def _find_path(self, from_node: Node, to_node: Node):
        def is_free_cell(cell):
            return self._board.get_cell(cell[0], cell[1]) == PLAYER_NONE

        def finalize_path(path_to_finalize):
            beg = from_node.tuple()
            end = to_node.tuple()
            beg_p = [beg] if is_free_cell(beg) else []
            end_p = [end] if is_free_cell(end) else []
            return merge_paths(beg_p, path_to_finalize, end_p)

        shortest_path = self._astar.find_path(self._for_player, from_node.x(), from_node.y(), to_node.x(), to_node.y())
        shortest_path = [p for p in filter(lambda c: is_free_cell(c), shortest_path)]

        if len(shortest_path) >= SHORTEST_PATH_LENGTH_TO_ANALYZE:
            for i1, chain1 in self._chains:
                beg_path, n1 = self._find_path_from_node_to_chain(from_node, chain1)
                if n1 is not None:
                    for i2, chain2 in self._chains:
                        end_path, n2 = self._find_path_from_node_to_chain(to_node, chain2)
                        if n2 is not None:
                            mid_path = self._find_path_between_chains(i1, i2) if i1 != i2 else []
                            path = finalize_path(merge_paths(beg_path, mid_path, end_path))
                            if len(path) < len(shortest_path):
                                shortest_path = path

        return shortest_path
Exemplo n.º 2
0
    def choose_node(self, nodes, dst_node: Node):
        min_cost = INFINITY
        best_node = None

        for node in nodes:
            cost_start_to_node = node.get_cost()
            cost_node_to_goal = get_distance(node.x(), node.y(), dst_node.x(), dst_node.y())
            total_cost = cost_start_to_node + cost_node_to_goal

            if min_cost > total_cost:
                min_cost = total_cost
                best_node = node

        return best_node
Exemplo n.º 3
0
def build_path(to_node: Node):
    path = []
    while to_node is not None:
        path.append((to_node.x(), to_node.y()))
        to_node = to_node.get_previous()
    return path
Exemplo n.º 4
0
 def _construct_full_path(self, src: Node, dst: Node, path):
     board = self._board.copy(check_bounds=False)
     board.set_cells(path, self._for_player)
     walker = WalkerPathfinder(board)
     return walker.find_path(self._for_player, src.x(), src.y(), dst.x(), dst.y())