-
Notifications
You must be signed in to change notification settings - Fork 0
/
uci.py
103 lines (95 loc) · 3.43 KB
/
uci.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Not Magnus
Classical chess engine by Devin Zhang
UCI communication file by Disservin
"""
from sys import stdout
from threading import Thread
from search import *
def main():
stack = []
out = stdout
stop_threads = False
def output(s):
out.write(str(s + "\n"))
out.flush()
while True:
if stack:
command = stack.pop()
else:
command = input()
if command == "quit":
break
elif command == "stop":
stop_threads = True
try:
thread_main.join()
except:
pass
elif command == "uci":
output("id name Not Magnus")
output("id auther Devin Zhang")
output("")
output("option name openingbook type check default false")
output("option name tablebase type check default false")
output("uciok")
elif command == "isready":
output("readyok")
elif command == "ucinewgame":
board = chess.Board()
fen = board.fen()
elif command.startswith("position"):
stop_threads = False
parameters = command.split(" ")
fen_or_startpos = parameters[1]
index_moves = command.find("moves")
try:
if fen_or_startpos == "fen":
board = chess.Board()
index_fen = command.find("fen")
fen = command[(index_fen + 4):index_moves]
board.set_fen(fen)
elif fen_or_startpos == "startpos":
board = chess.Board()
else:
output("Invalid position command")
if "moves" in parameters:
moveslist = command[index_moves:].split()[1:]
for move in moveslist:
board.push_uci(move)
except UnboundLocalError:
output("Error: No board initialized")
elif command.startswith("go"):
parameters = command.split(" ")
depth = 255
stop_threads = False
movetime = 5000 # TODO time manager
if "infinite" in parameters:
pass
elif "depth" in parameters:
depth = int(parameters[2])
elif "movetime" in parameters:
movetime = int(parameters[2])
elif "nodes" in parameters:
nodes = int(parameters[2])
elif "wtime" in parameters:
index_time = parameters.index("wtime")
wtime = int(parameters[index_time + 1])
elif "btime" in parameters:
index_time = parameters.index("btime")
btime = int(parameters[index_time + 1])
elif "winc" in parameters:
index_inc = parameters.index("winc")
winc = int(parameters[index_time + 1])
elif "binc" in parameters:
index_inc = parameters.index("binc")
binc = int(parameters[index_time + 1])
else:
depth = 255
try:
thread_main = Thread(target = cpu_move, args = (board, depth, movetime, lambda: stop_threads))
thread_main.start()
except UnboundLocalError:
output("Error: No board initialized")
if __name__ == "__main__":
main()