Ejemplo n.º 1
0
def generate_tree(
    dot: Digraph,
    board: Board,
    node: Union[str, Dict[str, Union[Dict[str, Any], str]]],
    move_sequence: str = "",
) -> None:
    board_name = board.get_image_file_name()
    board_name = board.write_image()
    dot.node(board_name, label="", shape="plaintext", image=board_name)

    if isinstance(node, str):

        if node == "transposition":
            return

        child_name = board_name + node
        dot.node(child_name, node)
        dot.edge(board_name, child_name)
        return

    for move, subtree in node.items():
        child = board.do_move(Board.field_to_index(move))
        child_name = child.write_image()
        dot.node(child_name, label="", shape="plaintext", image=child_name)
        dot.edge(board_name, child_name)

        move_sequence_prefix = move_sequence + " " + move
        try:
            generate_tree(dot, child, subtree, move_sequence + " " + move)
        except ValueError as e:
            print(f"at {move_sequence_prefix}: {e}")
            exit(1)
Ejemplo n.º 2
0
    def from_pgn(cls, filename: str) -> "Game":
        with open(filename, "r") as file:
            contents = file.read()

        game = Game()

        lines = contents.split("\n")
        for offset, line in enumerate(lines):
            if not line.startswith("["):
                break

            split_line = line.split(" ")
            key = split_line[0][1:]
            value = split_line[1][1:-2]
            game.metadata[key] = value

        board = Board()
        game.boards.append(copy(board))

        for line in lines[offset:]:

            if line == "":
                continue

            for word in line.split(" "):
                if word[0].isdigit():
                    continue

                game.moves.append(word)
                board = board.do_move(board.field_to_index(word))
                game.boards.append(copy(board))

        return game
Ejemplo n.º 3
0
def board_details_children(board: Board) -> Dict[str, Dict[str, Any]]:
    children: Dict[str, Dict[str, Any]] = {}

    for index, field in enumerate(board.get_fields()):
        if field != VALID_MOVE:
            continue

        child = board.do_move(index)

        # make sure we pass if there are no moves
        if not child.has_moves():
            child = child.do_move(MOVE_PASS)

        children[str(index)] = {
            "id": child.to_id(),
        }

    return children
Ejemplo n.º 4
0
    def add_board_interactive(self, board: Board, game: Game,
                              move_offset: int) -> Board:
        board.show()
        print()

        move_sequence = " ".join(game.moves[:move_offset])
        print(f"Replay: {move_sequence}")

        move_fields = board.get_move_fields()

        print("Enter correct move:")
        while True:
            field = input("> ")

            if field in move_fields:
                break

        best_move = Board.field_to_index(field)
        best_child = board.do_move(best_move).normalized()[0]
        board_normalized = board.normalized()[0]
        self.upsert(board_normalized, best_child)
        return best_child