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
|
import random
import chess
import chess.engine
ENGINE_A = "./engines/chessV0"
ENGINE_B = "./engines/chessV1"
POSITIONS_FILE = "positions.txt"
MAX_GAMES = 100
class UCIEngine:
def __init__(self, path, name, depth):
self.name = name
self.depth = depth
print(f"Starting {name}: {path}")
self.engine = chess.engine.SimpleEngine.popen_uci(path)
print(f"{name} ready")
def get_move(self, board):
result = self.engine.play(board, chess.engine.Limit(depth=self.depth))
return result.move
def quit(self):
self.engine.quit()
def play_game(engine_white, engine_black, fen, game_number):
board = chess.Board(fen)
print("\n" + "=" * 60)
print(f"GAME {game_number}")
print("=" * 60)
print("Starting FEN:")
print(fen)
move_number = 1
while not board.is_game_over() and move_number <= 200:
if board.turn == chess.WHITE:
engine = engine_white
color = "White"
else:
engine = engine_black
color = "Black"
move = engine.get_move(board)
print(f"{move_number}. {color} ({engine.name}) plays {move}")
board.push(move)
if board.turn == chess.WHITE:
move_number += 1
result = board.result()
print("Game finished:", result)
print("Moves played:", board.fullmove_number)
return result
def main():
depth = int(input("Search depth: "))
with open(POSITIONS_FILE) as f:
positions = [line.strip() for line in f if line.strip()]
random.shuffle(positions)
engine_a = UCIEngine(ENGINE_A, "chessV0", depth)
engine_b = UCIEngine(ENGINE_B, "chessV1", depth)
results = {"chessV0 wins": 0, "chessV1 wins": 0, "draws": 0}
try:
for i, fen in enumerate(positions[:MAX_GAMES], 1):
# alternate colors
if i % 2 == 0:
white = engine_a
black = engine_b
a_color = chess.BLACK
else:
white = engine_b
black = engine_a
a_color = chess.WHITE
result = play_game(white, black, fen, i)
if result == "1-0":
if a_color == chess.WHITE:
results["chessV0 wins"] += 1
else:
results["chessV1 wins"] += 1
elif result == "0-1":
if a_color == chess.BLACK:
results["chessV0 wins"] += 1
else:
results["chessV1 wins"] += 1
else:
results["draws"] += 1
print("\nCurrent score:")
print(results)
finally:
engine_a.quit()
engine_b.quit()
print("\n" + "=" * 60)
print("FINAL RESULTS")
print("=" * 60)
print(results)
if __name__ == "__main__":
main()
|