Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import chess
- class ChessBot:
- name: str = "ChessBot"
- author: str = "Indivicivet"
- def __init__(self):
- self.board = chess.Board()
- def best_move(self):
- return next(x for x in self.board.legal_moves)
- def process_command(self, input_str):
- cmd, *params = input_str.split(" ")
- if cmd == "uci":
- print(f"id name {self.name}")
- print(f"id author {self.author}")
- print("uciok")
- elif cmd == "isready":
- print("readyok")
- elif cmd == "ucinewgame":
- self.board = chess.Board()
- elif cmd == "position":
- assert params
- if params[0] == "startpos":
- self.board = chess.Board()
- elif params[0] == "fen":
- self.board = chess.Board(" ".join(params[1:]))
- else:
- raise Exception("bad param after `position` cmd")
- if len(params) > 1:
- assert len(params) >= 3
- for move in params[2:]:
- self.board.push_uci(move)
- elif cmd == "go":
- move = self.best_move()
- print(f"bestmove {move}")
- self.board.push(move)
- elif cmd in ["exit", "quit"]:
- exit()
- if __name__ == "__main__":
- game = ChessBot()
- while True:
- game.process_command(input())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement