Example #1
0
def board_details(board_id: str) -> Response:
    try:
        board = Board.from_id(board_id)
    except ValueError:
        return make_response("invalid board id", 400)

    return jsonify(board_dict(board))
Example #2
0
    def lookup(self, board: Board) -> Optional[Board]:
        board_id = board.get_normalized_id()
        openings = self.data["openings"]

        if board_id not in openings:
            return None

        return Board.from_id(openings[board_id]["best_child"])
Example #3
0
def test_board_from_id_ok(id_str: str, expected_board: Board) -> None:
    board = Board.from_id(id_str)

    if id_str == "xot":
        assert BLACK == board.turn
        assert 12 == board.count(WHITE) + board.count(BLACK)
        return

    assert expected_board == board
Example #4
0
    def validate(self) -> None:
        for board_id, board_data in self.data["openings"].items():
            try:
                board = Board.from_id(board_id)
            except ValueError as e:
                raise OpeningsTreeValidationError(
                    f"board {board_id}: invalid ID") from e

            child_id = board_data["best_child"]

            try:
                Board.from_id(child_id)
            except ValueError as e:
                raise OpeningsTreeValidationError(
                    f"board {board_id}: invalid best_child ID") from e

            if child_id not in board.get_normalized_children_ids():
                raise OpeningsTreeValidationError(
                    f"board {board_id}: best_child is not a valid child")
Example #5
0
    def _get_openings(
            self, color: int, board: Board,
            prefix: List[Tuple[Board, int]]) -> List[List[Tuple[Board, int]]]:

        assert board.turn == color

        try:
            best_child_id: str = self.data["openings"][
                board.get_normalized_id()]["best_child"]
        except KeyError:
            return [prefix]

        best_child = board.denormalize_child(Board.from_id(best_child_id))
        assert best_child in board.get_children()
        assert best_child.turn == opponent(color)
        best_move = board.get_move(best_child)

        openings = []

        for grand_child in best_child.get_children():
            openings += self._get_openings(color, grand_child,
                                           prefix + [(board, best_move)])

        return openings
Example #6
0
def show(board_id: str) -> None:
    board = Board.from_id(board_id)
    board.show()
Example #7
0
def test_board_from_id_fail(id_str: str, expected_error_message: str) -> None:
    with pytest.raises(ValueError) as exc_info:
        Board.from_id(id_str)

    assert expected_error_message == exc_info.value.args[0]