-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuci_driver.py
executable file
·73 lines (68 loc) · 2.1 KB
/
uci_driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Runs a chess engine over UCI."""
import math
import threading
import time
import logging
from game import Position, Move, make_move
from testengine import TestEngine
from eniacengine import EniacEngine
def _uci_driver(engine):
logging.basicConfig(level=logging.DEBUG,
format="%(process)d %(asctime)s %(name)-12s %(levelname)-8s %(message)s",
filename="/tmp/chess.log",
filemode="w")
engine.start()
next_position = None
logger = logging.getLogger("driver")
logger.info("starting up!")
while True:
command = input()
logger.debug(command)
if command == "uci":
print(f"id name {engine.name}")
print(f"id author {engine.author}")
print("uciok")
elif command == "ucinewgame":
engine.stop.set()
elif command == "isready":
print("readyok")
elif command.startswith("position "):
args = command[len("position "):]
if args.startswith("startpos"):
args = args[len("startpos"):]
next_position = Position.initial()
if args.startswith(" moves "):
args = args[len(" moves "):]
moves = [Move.lan(x) for x in args.split()]
for move in moves:
next_position = make_move(next_position, move)
elif args.startswith("fen "):
args = args[len("fen "):]
next_position = Position.fen(args)
logger.debug(f"next position: {str(next_position)}")
elif command == "go" or command.startswith("go "):
engine.position = next_position
engine.wait_for_stop = "infinite" in command
engine.go.set()
elif command == "stop":
engine.stop.set()
elif command.startswith("debug "):
pass
elif command.startswith("setoption "):
pass
elif command == "register":
pass
elif command == "quit":
break
logger.info("quitting!")
engine.stop.set()
engine.go.set()
engine.quit.set()
engine.join()
logger.info("shutdown cleanly")
if __name__ == "__main__":
#engine = TestEngine()
engine = EniacEngine()
_uci_driver(engine)