From 367f8a5da8640257a93a8a63c48e6c0ab87ed5cc Mon Sep 17 00:00:00 2001 From: Terry Hearst Date: Sun, 8 Aug 2021 23:53:45 -0400 Subject: [PATCH 1/2] Add "SearchDepth" UCI option --- communication.py | 29 +++++++++++++++++++++++++---- test/test_engine.py | 6 +++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/communication.py b/communication.py index c6fd594..a9aa7fc 100644 --- a/communication.py +++ b/communication.py @@ -9,12 +9,13 @@ def talk(): The main input/output loop. This implements a slice of the UCI protocol. """ + global search_depth board = chess.Board() - depth = get_depth() + search_depth = get_depth() while True: msg = input() - command(depth, board, msg) + command(search_depth, board, msg) def command(depth: int, board: chess.Board, msg: str): @@ -26,6 +27,8 @@ def command(depth: int, board: chess.Board, msg: str): tokens = msg.split(" ") while "" in tokens: tokens.remove("") + if len(tokens) <= 0: + return if msg == "quit": sys.exit() @@ -33,6 +36,7 @@ def command(depth: int, board: chess.Board, msg: str): if msg == "uci": print("id name Andoma") # Andrew/Roma -> And/oma print("id author Andrew Healey & Roma Parramore") + print("option name SearchDepth type spin default 3 min 1 max 20") print("uciok") return @@ -43,7 +47,7 @@ def command(depth: int, board: chess.Board, msg: str): if msg == "ucinewgame": return - if msg.startswith("position"): + if tokens[0] == "position": if len(tokens) < 2: return @@ -69,12 +73,29 @@ def command(depth: int, board: chess.Board, msg: str): # Non-standard command, but supported by Stockfish and helps debugging print(board) print(board.fen()) + print(f"Depth: {depth}") - if msg[0:2] == "go": + if tokens[0] == "go": _move = next_move(depth, board) print(f"bestmove {_move}") return + if tokens[0] == "setoption": + if len(tokens) < 3 or tokens[1] != "name": + return + + if tokens[2] == "SearchDepth": + if len(tokens) < 5 or tokens[3] != "value": + return + new_depth = int(tokens[4]) + if new_depth < 1 or new_depth > 20: + return + global search_depth + search_depth = new_depth + else: + # "SearchDepth" is the only option we support currently + return + def get_depth() -> int: parser = argparse.ArgumentParser() diff --git a/test/test_engine.py b/test/test_engine.py index 2d10c7c..a42a67f 100644 --- a/test/test_engine.py +++ b/test/test_engine.py @@ -13,9 +13,9 @@ def test_uci_command(self): board = chess.Board() with patch("sys.stdout", new=StringIO()) as patched_output: command(3, board, "uci") - lines = patched_output.getvalue().strip().split("\n") - self.assertEqual(len(lines), 3) - self.assertEqual(lines[2], "uciok") + lines = patched_output.getvalue().strip().splitlines() + self.assertEqual(len(lines), 4) + self.assertEqual(lines[-1], "uciok") def test_position_startpos_command(self): """ From dbe10ec0529e536263c9890a21379fd978443d8e Mon Sep 17 00:00:00 2001 From: Terry Hearst Date: Mon, 9 Aug 2021 00:18:09 -0400 Subject: [PATCH 2/2] Fix mypy issue --- communication.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/communication.py b/communication.py index a9aa7fc..8f515d9 100644 --- a/communication.py +++ b/communication.py @@ -4,6 +4,9 @@ from movegeneration import next_move +search_depth: int + + def talk(): """ The main input/output loop.