forked from thomasahle/sunfish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuci.py
executable file
·161 lines (125 loc) · 4.71 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import importlib
import re
import sys
import time
import logging
import argparse
import tools
import sunfish_nnue as sunfish
from tools import WHITE, BLACK, Unbuffered
def main():
parser = argparse.ArgumentParser()
parser.add_argument('module', help='sunfish.py file (without .py)', type=str, default='sunfish_nnue', nargs='?')
parser.add_argument('--tables', metavar='pst', help='alternative pst table', type=str, default=None)
args = parser.parse_args()
sunfish = importlib.import_module(args.module)
if args.tables is not None:
pst_module = importlib.import_module(args.tables)
sunfish.pst = pst_module.pst
logging.basicConfig(filename='sunfish.log', level=logging.DEBUG)
out = Unbuffered(sys.stdout)
def output(line):
print(line, file=out)
logging.debug(line)
pos = tools.parseFEN(tools.FEN_INITIAL)
searcher = sunfish.Searcher()
color = WHITE
our_time, opp_time = 1000, 1000 # time in centi-seconds
show_thinking = True
stack = []
while True:
if stack:
smove = stack.pop()
else:
smove = input()
logging.debug(f'>>> {smove} ')
if smove == 'quit':
break
elif smove == 'uci':
output('id name Sunfish')
output('id author Thomas Ahle & Contributors')
output('uciok')
elif smove == 'isready':
output('readyok')
elif smove == 'ucinewgame':
stack.append('position fen ' + tools.FEN_INITIAL)
# syntax specified in UCI
# position [fen | startpos ] moves ....
elif smove.startswith('position'):
params = smove.split(' ')
idx = smove.find('moves')
if idx >= 0:
moveslist = smove[idx:].split()[1:]
else:
moveslist = []
if params[1] == 'fen':
if idx >= 0:
fenpart = smove[:idx]
else:
fenpart = smove
_, _, fen = fenpart.split(' ', 2)
elif params[1] == 'startpos':
fen = tools.FEN_INITIAL
else:
pass
pos = tools.parseFEN(fen)
color = WHITE if fen.split()[1] == 'w' else BLACK
for move in moveslist:
pos = pos.move(tools.mparse(color, move))
color = 1 - color
elif smove.startswith('go'):
# default options
depth = 1000
movetime = -1
_, *params = smove.split(' ')
for param, val in zip(*2*(iter(params),)):
if param == 'depth':
depth = int(val)
if param == 'movetime':
movetime = int(val)
if param == 'wtime':
our_time = int(val)
if param == 'btime':
opp_time = int(val)
moves_remain = 40
start = time.time()
ponder = None
for sdepth, _move, _score in searcher.search(pos):
moves = tools.pv(searcher, pos, include_scores=False)
if show_thinking:
entry = searcher.tp_score.get((pos, sdepth, True))
score = int(round((entry.lower + entry.upper)/2))
usedtime = int((time.time() - start) * 1000)
moves_str = moves if len(moves) < 15 else ''
output('info depth {} score cp {} time {} nodes {} pv {}'.format(sdepth, score, usedtime, searcher.nodes, moves_str))
if len(moves) > 5:
ponder = moves[1]
if movetime > 0 and (time.time() - start) * 1000 > movetime:
break
if (time.time() - start) * 1000 > our_time/moves_remain:
break
if sdepth >= depth:
break
entry = searcher.tp_score.get((pos, sdepth, True))
m, s = searcher.tp_move.get(pos), entry.lower
# We only resign once we are mated.. That's never?
# if s == -sunfish.MATE_UPPER:
# output('resign')
# else:
moves = moves.split(' ')
if len(moves) > 1:
output(f'bestmove {moves[0]} ponder {moves[1]}')
else:
output('bestmove ' + moves[0])
elif smove.startswith('time'):
our_time = int(smove.split()[1])
elif smove.startswith('otim'):
opp_time = int(smove.split()[1])
else:
pass
if __name__ == '__main__':
main()