Advertisement
Indivicivet

basic_chess_bot.py

Mar 4th, 2021
1,180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. import chess
  2.  
  3. class ChessBot:
  4.     name: str = "ChessBot"
  5.     author: str = "Indivicivet"
  6.  
  7.     def __init__(self):
  8.         self.board = chess.Board()
  9.  
  10.     def best_move(self):
  11.         return next(x for x in self.board.legal_moves)
  12.  
  13.     def process_command(self, input_str):
  14.         cmd, *params = input_str.split(" ")
  15.         if cmd == "uci":
  16.             print(f"id name {self.name}")
  17.             print(f"id author {self.author}")
  18.             print("uciok")
  19.         elif cmd == "isready":
  20.             print("readyok")
  21.         elif cmd == "ucinewgame":
  22.             self.board = chess.Board()
  23.         elif cmd == "position":
  24.             assert params
  25.             if params[0] == "startpos":
  26.                 self.board = chess.Board()
  27.             elif params[0] == "fen":
  28.                 self.board = chess.Board(" ".join(params[1:]))
  29.             else:
  30.                 raise Exception("bad param after `position` cmd")
  31.             if len(params) > 1:
  32.                 assert len(params) >= 3
  33.                 for move in params[2:]:
  34.                     self.board.push_uci(move)
  35.         elif cmd == "go":
  36.             move = self.best_move()
  37.             print(f"bestmove {move}")
  38.             self.board.push(move)
  39.         elif cmd in ["exit", "quit"]:
  40.             exit()
  41.  
  42. if __name__ == "__main__":
  43.     game = ChessBot()
  44.     while True:
  45.         game.process_command(input())
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement