-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_katyusha.py
72 lines (63 loc) · 2.35 KB
/
test_katyusha.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 numpy as np
import chess
from chess import uci
import argparse
import json
def parse_engine_json(jsonfile):
obj = json.load(jsonfile)
engine = uci.popen_engine("/home/benjamin/Katyusha/"+obj["binary"])
engine.setoption(obj["options"])
engine.description = obj["description"]
return engine
def play_game(engine1, engine2):
board = chess.Board()
info1 = uci.InfoHandler()
info2 = uci.InfoHandler()
engine1.info_handlers.append(info1)
engine2.info_handlers.append(info2)
while not board.is_game_over():
engine1.position(board)
bestInfo = engine1.go(movetime=100)
# print bestInfo
board.push(bestInfo.bestmove)
if board.is_game_over(): break
engine2.position(board)
bestInfo = engine2.go(movetime=100)
# print bestInfo
board.push(bestInfo.bestmove)
# print board
# with info1:
# cp = info1.info["score"][1].cp
# mate = info1.info["score"][1].mate
# print cp, mate
# with info2:
# cp, mate = info2.info["score"][1].cp, info2.info["score"][1].mate
# print cp, mate
if board.is_checkmate():
#White's turn
if board.turn:
return 0.
else: return 1.
else: return .5
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = 'Play two chess engines against each other.')
json_help = "A json file containing the path to the engine binary and engine options."
parser.add_argument('engine1_json',type=argparse.FileType("r"), help= json_help)
parser.add_argument('engine2_json', type = argparse.FileType("r"), help = json_help)
parser.add_argument('--num_games', type=int, default=1)
args = parser.parse_args()
engine1 = parse_engine_json(args.engine1_json)
engine2 = parse_engine_json(args.engine2_json)
print "Engine 1 Description: "+engine1.description
print "Engine 2 Description: "+engine2.description
for i in xrange(args.num_games):
#randomly pick a side
if np.random.randint(2) % 2:
colors = ["White","Black"]
result = play_game(engine1, engine2)
else:
colors = ["Black", "White"]
result = play_game(engine2, engine1)
for i in xrange(2):
print colors[i]+" Engine "+str(i+1)
print "Result: " + str(result)