-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
299 lines (246 loc) · 9.17 KB
/
Copy pathengine.py
File metadata and controls
299 lines (246 loc) · 9.17 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
"""
Gomoku Game Engine
A simple Python engine for running Gomoku games between player bots.
Usage: python engine.py /path/to/player1.py /path/to/player2.py -r NUM_ROUNDS
"""
import argparse
import importlib.util
import json
import os
import random
import sys
from dataclasses import dataclass, field
from datetime import datetime
from typing import Callable
@dataclass
class GomokuGame:
"""Represents a Gomoku game state."""
board_size: int = 15
board: list = field(default_factory=list)
current_player: str = "black"
winner: str | None = None
history: list = field(default_factory=list)
def __post_init__(self):
if not self.board:
self.board = [[0] * self.board_size for _ in range(self.board_size)]
def make_move(self, x: int, y: int) -> bool:
"""Make a move. Returns True if valid, False otherwise."""
if not (0 <= x < self.board_size and 0 <= y < self.board_size):
return False
if self.board[x][y] != 0:
return False
if self.winner is not None:
return False
stone = 1 if self.current_player == "black" else 2
self.board[x][y] = stone
self.history.append((self.current_player, x, y))
if self._check_win(x, y, stone):
self.winner = self.current_player
return True
# Switch player
self.current_player = "white" if self.current_player == "black" else "black"
return True
def _check_win(self, x: int, y: int, stone: int) -> bool:
"""Check if the last move resulted in a win."""
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for dx, dy in directions:
count = 1
# Count in positive direction
nx, ny = x + dx, y + dy
while (
0 <= nx < self.board_size
and 0 <= ny < self.board_size
and self.board[nx][ny] == stone
):
count += 1
nx += dx
ny += dy
# Count in negative direction
nx, ny = x - dx, y - dy
while (
0 <= nx < self.board_size
and 0 <= ny < self.board_size
and self.board[nx][ny] == stone
):
count += 1
nx -= dx
ny -= dy
if count == 5:
return True
return False
def is_full(self) -> bool:
"""Check if the board is full."""
for row in self.board:
if 0 in row:
return False
return True
def get_board_copy(self) -> list:
"""Return a deep copy of the board."""
return [row[:] for row in self.board]
_module_counter = 0
def load_player_module(path: str) -> Callable:
"""Load a player module and return its get_move function."""
global _module_counter
_module_counter += 1
module_name = f"player_module_{_module_counter}"
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
if not hasattr(module, "get_move"):
raise ValueError(
f"Player module {path} must define a get_move(board, color) function"
)
return module.get_move
def run_game(player1_path: str, player2_path: str, board_size: int = 15) -> dict:
"""Run a single game between two players."""
# Load player modules
try:
player1_move = load_player_module(player1_path)
except Exception as e:
return {"winner": "player2", "error": f"Failed to load player1: {e}"}
try:
player2_move = load_player_module(player2_path)
except Exception as e:
return {"winner": "player1", "error": f"Failed to load player2: {e}"}
# Randomly assign colors
if random.random() < 0.5:
players = {
"black": (player1_move, "player1"),
"white": (player2_move, "player2"),
}
else:
players = {
"black": (player2_move, "player2"),
"white": (player1_move, "player1"),
}
game = GomokuGame(board_size=board_size)
max_moves = board_size * board_size
move_count = 0
while game.winner is None and move_count < max_moves:
move_fn, player_name = players[game.current_player]
try:
board_copy = game.get_board_copy()
move = move_fn(board_copy, game.current_player)
if not isinstance(move, (list, tuple)) or len(move) != 2:
# Invalid move format - other player wins
other_color = "white" if game.current_player == "black" else "black"
_, winner_name = players[other_color]
return {
"winner": winner_name,
"error": f"{player_name} returned invalid move format: {move}",
"players": players,
}
x, y = int(move[0]), int(move[1])
if not game.make_move(x, y):
# Invalid move - other player wins
other_color = "white" if game.current_player == "black" else "black"
_, winner_name = players[other_color]
return {
"winner": winner_name,
"error": f"{player_name} made invalid move: ({x}, {y})",
"players": players,
}
move_count += 1
except Exception as e:
# Error in player code - other player wins
other_color = "white" if game.current_player == "black" else "black"
_, winner_name = players[other_color]
return {
"winner": winner_name,
"error": f"{player_name} raised exception: {e}",
"players": players,
}
if game.winner:
_, winner_name = players[game.winner]
return {"winner": winner_name, "history": game.history, "players": players}
else:
return {"winner": "draw", "history": game.history, "players": players}
def write_game_log(
game_num: int,
result: dict,
players: dict,
player1_path: str,
player2_path: str,
output_dir: str = ".",
):
"""Write a detailed log file for a single game in JSON format."""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:-3]
json_filename = os.path.join(output_dir, f"log-{timestamp}.json")
# Write JSON log for visualizer
json_data = {
"game_number": game_num,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"board_size": 15,
"players": {
"black": {
"name": players["black"][1],
"path": player1_path
if players["black"][1] == "player1"
else player2_path,
},
"white": {
"name": players["white"][1],
"path": player1_path
if players["white"][1] == "player1"
else player2_path,
},
},
"winner": result["winner"],
"error": result.get("error"),
"moves": [
{"move_number": i + 1, "player": color, "x": x, "y": y}
for i, (color, x, y) in enumerate(result.get("history", []))
],
}
with open(json_filename, "w") as f:
json.dump(json_data, f, indent=2)
def main():
parser = argparse.ArgumentParser(description="Gomoku Game Engine")
parser.add_argument("player1", help="Path to player 1 code (main.py)")
parser.add_argument("player2", help="Path to player 2 code (main.py)")
parser.add_argument(
"-r", "--rounds", type=int, default=10, help="Number of rounds to play"
)
parser.add_argument("-b", "--board-size", type=int, default=15, help="Board size")
parser.add_argument(
"-o",
"--output-dir",
type=str,
default=None,
help="Output directory for log files",
)
args = parser.parse_args()
scores = {"player1": 0, "player2": 0, "draw": 0}
print(f"Running {args.rounds} games between:")
print(f" Player 1: {args.player1}")
print(f" Player 2: {args.player2}")
print()
for i in range(args.rounds):
result = run_game(args.player1, args.player2, args.board_size)
winner = result["winner"]
scores[winner] = scores.get(winner, 0) + 1
# Write detailed log for this game
if args.output_dir and "players" in result:
write_game_log(
i + 1,
result,
result["players"],
args.player1,
args.player2,
args.output_dir,
)
if "error" in result:
print(f"Game {i + 1}: {winner} wins (error: {result['error']})")
else:
print(f"Game {i + 1}: {winner} wins")
print()
print("FINAL_RESULTS")
p1_name = os.path.basename(os.path.dirname(args.player1))
p2_name = os.path.basename(os.path.dirname(args.player2))
print(f"Bot_1_main: {scores['player1']} rounds won ({p1_name})")
print(f"Bot_2_main: {scores['player2']} rounds won ({p2_name})")
print(f"Draws: {scores['draw']}")
if __name__ == "__main__":
main()