-
Notifications
You must be signed in to change notification settings - Fork 0
/
uci.py
72 lines (57 loc) · 1.47 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
import sys
import chess
import argparse
from movegeneration import next_move
def talk():
'''
The main input/output loop.
This implements a slice of the UCI protocol.
'''
board = chess.Board()
depth = get_depth()
while True:
msg = input()
print(f'>>> {msg}', file=sys.stderr)
command(depth, board, msg)
def command(depth, board, msg):
'''
Accept UCI commands and respond.
The board state is also updated.
'''
if msg == 'quit':
sys.exit()
if msg == 'uci':
print("id name schach-KI") # Name of chess engine
print("id author Andreas Waider") # Name of creators
print("uciok")
return
if msg == 'isready':
print('readyok')
return
if msg == 'ucinewgame':
return
if 'position startpos moves' in msg:
moves = msg.split(' ')[3:]
board.clear()
board.set_fen(chess.STARTING_FEN)
for move in moves:
board.push(chess.Move.from_uci(move))
return
if 'position fen' in msg:
fen = ' '.join(msg.split(' ')[2:])
board.set_fen(fen)
return
if msg[0:2] == 'go':
move = next_move(board)
print(f'bestmove {move}')
return
def get_depth():
parser = argparse.ArgumentParser()
parser.add_argument(
'--depth',
default=3,
help='provide an integer (default: 3)'
)
args = parser.parse_args()
return int(args.depth)
talk()