forked from thomasahle/sunfish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uci.py
121 lines (95 loc) · 3.38 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
#!/usr/bin/env pypy -u
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import importlib
import re
import sys
import time
import tools
import sunfish
from tools import WHITE, BLACK
from xboard import Unbuffered, sunfish, input
sys.stdout = Unbuffered(sys.stdout)
def main():
pos = tools.parseFEN(tools.FEN_INITIAL)
searcher = sunfish.Searcher()
forced = False
color = WHITE
our_time, opp_time = 1000, 1000 # time in centi-seconds
show_thinking = True
# print name of chess engine
print('Sunfish')
stack = []
while True:
if stack:
smove = stack.pop()
else: smove = input()
if smove == 'quit':
break
elif smove == 'uci':
print('uciok')
elif smove == 'isready':
print('readyok')
elif smove == 'ucinewgame':
stack.append('position fen ' + tools.FEN_INITIAL)
elif smove.startswith('position'):
params = smove.split(' ', 2)
if params[1] == 'fen':
fen = params[2]
pos = tools.parseFEN(fen)
color = WHITE if fen.split()[1] == 'w' else BLACK
elif smove.startswith('go'):
# default options
depth = 1000
movetime = -1
# parse parameters
params = smove.split(' ')
if len(params) == 1: continue
i = 0
while i < len(params):
param = params[i]
if param == 'depth':
i += 1
depth = int(params[i])
if param == 'movetime':
i += 1
movetime = int(params[i])
i += 1
forced = False
moves_remain = 40
start = time.time()
ponder = None
for _ in searcher._search(pos):
moves = tools.pv(searcher, pos, include_scores=False)
if show_thinking:
entry = searcher.tp_score.get((pos, searcher.depth, True))
score = int(round((entry.lower + entry.upper)/2))
usedtime = int((time.time() - start) * 1000)
moves_str = moves if len(moves) < 15 else ''
print('info depth {} score {} time {} nodes {} {}'.format(searcher.depth, score, usedtime, searcher.nodes, moves_str))
if len(moves) > 5:
ponder = moves[1]
if movetime > 0 and (time.time() - start) * 1000 > movetime:
break
if searcher.depth >= depth:
break
entry = searcher.tp_score.get((pos, searcher.depth, 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:
print('resign')
else:
moves = moves.split(' ')
if len(moves) > 1:
print('bestmove ' + moves[0] + ' ponder ' + moves[1])
else:
print('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()