-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathengine_test.py
More file actions
60 lines (47 loc) · 2.19 KB
/
engine_test.py
File metadata and controls
60 lines (47 loc) · 2.19 KB
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
import engine_main # UCI engine
import chess # chess
import chess.engine # chess engine
import time # time module
import engine # engine
import sys # system
if True:
"""Testing engine"""
_engine = chess.engine.SimpleEngine.popen_uci("./stockfish")
board = chess.Board()
movetime = -1
command = 'go depth 20' # test command
# parse values
_, *params = command.split(' ') # parameters
for param, val in zip(*2 * (iter(params),)): # cycle of params and values
if param == 'depth': # depth param
depth = int(val) # depth value
if param == 'movetime': # movetime param
movetime = int(val) # movetime value
if param == 'wtime': # wtime param
our_time = int(val) # wtime value
if param == 'btime': # btime param
opp_time = int(val) # btime value
moves_remain = 40 # remaining moves
start = time.time() # start time
ponder = None # ponder move
for sdepth in range(0, depth + 1): # cycle of self-depth and depth
if True: # if showing thinking
if board.turn: # if white to move
score = engine.analyze(_engine, board, sdepth) # score
else: # if black to move
score = engine.analyze(_engine, board, sdepth) # score
usedtime = int((time.time() - start) * 1000) # time used
print(
'info depth {} score cp {} time {}'.format(sdepth, score, usedtime)) # about thinking
for m in range(0, 2): # generate moves. 2 for ponder
best_move = engine.best_move(_engine, board, sdepth) # append moves to moves list
print("bestmove " + str(best_move)) # send response about best move
# checks for no-bugs
if movetime > 0 and (time.time() - start) * 1000 > movetime:
break
if sdepth >= depth:
break
print("bestmove " + str(best_move))
del best_move # delete best move
_engine.quit() # quit from engine
sys.exit() # exiting