Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "SearchDepth" UCI option #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
from movegeneration import next_move


search_depth: int


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):
Expand All @@ -26,13 +30,16 @@ 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()

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

Expand All @@ -43,7 +50,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

Expand All @@ -69,12 +76,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()
Expand Down
6 changes: 3 additions & 3 deletions test/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down