aboutsummaryrefslogtreecommitdiff
path: root/match-manager/match.py
diff options
context:
space:
mode:
Diffstat (limited to 'match-manager/match.py')
-rw-r--r--match-manager/match.py127
1 files changed, 127 insertions, 0 deletions
diff --git a/match-manager/match.py b/match-manager/match.py
new file mode 100644
index 0000000..1e3aa08
--- /dev/null
+++ b/match-manager/match.py
@@ -0,0 +1,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()