Exemplo n.º 1
0
class Kidpawn():
    def __init__(self, board_fen: str = None):
        if board_fen:
            self.b = Board(board_fen)
        else:
            # Not sure why this is needed.
            self.b = Board()

    def svg(self):
        return self.b._repr_svg_()

    def fen(self):
        """Returns board state as a FEN string
        """
        return self.b.fen()

    def move(self, move: str) -> [bool, str]:
        """Returns true if the move succeeded
        """
        try:
            self.b.push_uci(move)
            return True, f"you moved: {move}"
        except ValueError as e:
            msg = str(e)
            if msg.startswith("illegal uci"):
                return False, f"illegal move: {move}.  Try something like d2d4 or h7h8q."
            else:
                return False, f"other error {msg}"
        except Exception as e:
            traceback.print_exc()
            return False, f"unknown error {e}"

    def bot_move(self):
        """Computer makes a move herself, and updates the board
        """
        m = lookahead1_move(self.b)
        if m:
            return self.move(m.uci())
        else:
            return False, "No valid moves"

    def game_over_msg(self) -> str:
        """If the game is over, returns a string why.
        If game is not over, return blank
        """
        if self.b.is_game_over():
            return f"Game over: {self.b.result()}"
        else:
            return None