Advertisement
alkkofficial

Untitled

Jun 24th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. # https://github.com/Disservin/python-chess-engine/blob/master/src/uci.py
  2. # imports
  3. from sys import stdout
  4. from threading import Thread
  5. import chess
  6. import aiucieval
  7.  
  8.  
  9. class UCI:
  10. def __init__(self) -> None:
  11. self.out = stdout
  12. self.board = chess.Board()
  13. self.thread = None
  14.  
  15. def output(self, s) -> None:
  16. self.out.write(str(s) + "\n")
  17. self.out.flush()
  18.  
  19. def stop(self) -> None:
  20. try:
  21. self.thread.join()
  22. except:
  23. pass
  24.  
  25. def quit(self) -> None:
  26. try:
  27. self.thread.join()
  28. except:
  29. pass
  30.  
  31. def uci(self) -> None:
  32. self.output("id name Zan1Ling4")
  33. self.output("id author ALKK")
  34. self.output("")
  35. self.output("option name Move Overhead type spin default 5 min 0 max 5000")
  36. self.output("option name Ponder type check default false")
  37. self.output("uciok")
  38.  
  39. def isready(self) -> None:
  40. self.output("readyok")
  41.  
  42. def ucinewgame(self) -> None:
  43. pass
  44.  
  45. def eval(self) -> None:
  46. e = aiucieval.static_eval(self.board)
  47. self.output(e)
  48.  
  49. def processCommand(self, input: str) -> None:
  50. splitted = input.split(" ")
  51. match splitted[0]:
  52. case "quit":
  53. self.quit()
  54. case "stop":
  55. self.stop()
  56. case "ucinewgame":
  57. self.ucinewgame()
  58. case "uci":
  59. self.uci()
  60. case "isready":
  61. self.isready()
  62. case "setoption":
  63. pass
  64. case "position":
  65. fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
  66. movelist = []
  67.  
  68. move_idx = input.find("moves")
  69. if move_idx >= 0:
  70. movelist = input[move_idx:].split()[1:]
  71.  
  72. if splitted[1] == "fen":
  73. position_idx = input.find("fen") + len("fen ")
  74.  
  75. if move_idx >= 0:
  76. fen = input[position_idx:move_idx]
  77. else:
  78. fen = input[position_idx:]
  79.  
  80. self.board.set_fen(fen)
  81.  
  82. for move in movelist:
  83. self.board.push_uci(move)
  84.  
  85. case "print":
  86. print(self.board)
  87. case "go":
  88. l = ["depth", "nodes"]
  89.  
  90. for limit in l:
  91. if limit in splitted:
  92. if limit == "depth":
  93. depth = int(splitted[splitted.index(limit) + 1])
  94. x = aiucieval.analyse_move(self.board, True, depth)
  95. self.output(x)
  96. elif limit == "nodes":
  97. nodes = int(splitted[splitted.index(limit) + 1])
  98. x = aiucieval.analyse_move_nodes(self.board, True, nodes)
  99. self.output("bestmove "+ x)
  100.  
  101. ourTimeStr = "wtime" if self.board.turn == chess.WHITE else "btime"
  102. ourTimeIncStr = "winc" if self.board.turn == chess.WHITE else "binc"
  103.  
  104. if ourTimeStr in input:
  105. pass
  106.  
  107. if ourTimeIncStr in input:
  108. pass
  109.  
  110. case "eval":
  111. return self.eval()
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement