def test_board_field_index_conversion() -> None: pairs = [(MOVE_PASS, "--")] for x, col in enumerate("abcdefgh"): for y, row in enumerate("12345678"): index = 8 * y + x field = f"{col}{row}" pairs.append((index, field)) for index, field in pairs: assert index == Board.field_to_index(field) assert index == Board.field_to_index(field.upper()) assert field == Board.index_to_field(index)
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)
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
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