-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgame.py
More file actions
479 lines (397 loc) · 16.3 KB
/
game.py
File metadata and controls
479 lines (397 loc) · 16.3 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import numpy as np
import logging
from tqdm import tqdm
import pygame
import sys
log = logging.getLogger(__name__)
class Board:
"""
Gomoku board class
Board data:
1=white, -1=black, 0=empty
"""
def __init__(self, n=15):
self.n = n
# Create an empty board
self.pieces = [[0] * n for _ in range(n)]
def __getitem__(self, index):
return self.pieces[index]
def get_legal_moves(self, color):
"""Return all legal move positions"""
moves = []
for y in range(self.n):
for x in range(self.n):
if self[x][y] == 0:
moves.append((x, y))
return moves
def has_legal_moves(self):
"""Check if there are any legal moves"""
for y in range(self.n):
for x in range(self.n):
if self[x][y] == 0:
return True
return False
def execute_move(self, move, color):
"""Place a piece on the specified position"""
x, y = move
self[x][y] = color
def is_win(self, color):
"""Check if there is a win"""
# Check all directions
directions = [(1, 0), (0, 1), (1, 1), (1, -1)]
for y in range(self.n):
for x in range(self.n):
if self[x][y] == color:
# Check each direction
for dx, dy in directions:
count = 1
# Check forward
tx, ty = x + dx, y + dy
while (
0 <= tx < self.n
and 0 <= ty < self.n
and self[tx][ty] == color
):
count += 1
tx += dx
ty += dy
# Check backward
tx, ty = x - dx, y - dy
while (
0 <= tx < self.n
and 0 <= ty < self.n
and self[tx][ty] == color
):
count += 1
tx -= dx
ty -= dy
if count >= 5:
return True
return False
class GomokuGame:
square_content = {-1: "X", +0: ".", +1: "O"}
def __init__(self, n=15):
self.n = n
def getInitBoard(self):
b = Board(self.n)
return np.array(b.pieces)
def getBoardSize(self):
return (self.n, self.n)
def getActionSize(self):
return self.n * self.n
def getNextState(self, board, player, action):
b = Board(self.n)
b.pieces = np.copy(board)
# Fix: Ensure consistent coordinate transformation
move = (action // self.n, action % self.n) # This gives (x, y)
b.execute_move(move, player)
return (b.pieces, -player)
def getValidMoves(self, board, player):
b = Board(self.n)
b.pieces = np.copy(board)
valids = [0] * self.getActionSize()
legalMoves = b.get_legal_moves(player)
for x, y in legalMoves:
# Fix: Ensure consistent coordinate transformation
valids[self.n * x + y] = 1
return np.array(valids)
def getGameEnded(self, board, player):
b = Board(self.n)
b.pieces = np.copy(board)
if b.is_win(player):
return 1
if b.is_win(-player):
return -1
if not b.has_legal_moves():
return 0
return None
def getCanonicalForm(self, board, player):
return player * board
def getSymmetries(self, board, pi):
# Gomoku's symmetries include rotation and reflection
assert len(pi) == self.n**2
pi_board = np.reshape(pi, (self.n, self.n))
symmetries = []
for i in range(1, 5):
for j in [True, False]:
newB = np.rot90(board, i)
newPi = np.rot90(pi_board, i)
if j:
newB = np.fliplr(newB)
newPi = np.fliplr(newPi)
symmetries += [(newB, newPi.ravel())]
return symmetries
def stringRepresentation(self, board):
return board.tostring()
@staticmethod
def display(board, player1_first=True):
"""Update the GUI display"""
if not hasattr(GomokuGame, 'gui'):
GomokuGame.gui = GomokuGUI(len(board), player1_first)
GomokuGame.gui.draw_board(board, player1_first)
pygame.display.flip()
class GomokuGUI:
def __init__(self, board_size, player1_first=True):
pygame.init()
self.board_size = board_size
self.cell_size = 40
self.margin = 40
# Add space at the bottom for buttons and result text
self.bottom_margin = 80 # Space for buttons at bottom
# Calculate window size with bottom margin
self.window_size = 2 * self.margin + self.cell_size * (self.board_size - 1)
self.screen = pygame.display.set_mode((self.window_size, self.window_size + self.bottom_margin))
pygame.display.set_caption("AlphaZero Gomoku")
# Colors
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
self.BROWN = (205, 170, 125)
self.RED = (255, 0, 0)
# Font
self.font = pygame.font.Font(None, 24)
# Add button properties
self.button_height = 30
self.button_width = 100
self.button_margin = 10
# Position buttons at the bottom
self.next_button = pygame.Rect(
self.window_size - self.button_width - self.button_margin,
self.window_size + (self.bottom_margin - self.button_height) // 2,
self.button_width,
self.button_height
)
self.quit_button = pygame.Rect(
self.window_size - 2 * self.button_width - 2 * self.button_margin,
self.window_size + (self.bottom_margin - self.button_height) // 2,
self.button_width,
self.button_height
)
self.player1_first = player1_first
def get_mouse_position(self):
"""Convert mouse position to board coordinates"""
x, y = pygame.mouse.get_pos()
board_x = int((x - self.margin + self.cell_size/2) / self.cell_size)
board_y = int((y - self.margin + self.cell_size/2) / self.cell_size)
if 0 <= board_x < self.board_size and 0 <= board_y < self.board_size:
return board_x, board_y
return None
def draw_board(self, board, player1_first=True):
# Fill background
self.screen.fill(self.BROWN)
# Draw grid lines
for i in range(self.board_size):
# Vertical lines
start_pos = (self.margin + i * self.cell_size, self.margin)
end_pos = (self.margin + i * self.cell_size, self.margin + (self.board_size-1) * self.cell_size)
pygame.draw.line(self.screen, self.BLACK, start_pos, end_pos)
# Horizontal lines
start_pos = (self.margin, self.margin + i * self.cell_size)
end_pos = (self.margin + (self.board_size-1) * self.cell_size, self.margin + i * self.cell_size)
pygame.draw.line(self.screen, self.BLACK, start_pos, end_pos)
# Draw stones
for y in range(self.board_size):
for x in range(self.board_size):
if board[x][y] != 0:
center = (
self.margin + y * self.cell_size,
self.margin + x * self.cell_size
)
# Adjust color based on player order
if player1_first:
color = self.WHITE if board[x][y] == 1 else self.BLACK
else:
color = self.BLACK if board[x][y] == 1 else self.WHITE
pygame.draw.circle(self.screen, color, center, self.cell_size // 2 - 2)
def draw_game_over(self, result, is_final_round=False):
"""Draw game over message and control buttons"""
# Clear the bottom area first
pygame.draw.rect(self.screen, self.BROWN,
(0, self.window_size, self.window_size, self.bottom_margin))
# Draw result message - moved up by adjusting the vertical position
if result == 1:
msg = "You Win!"
elif result == -1:
msg = "AI Wins!"
else:
msg = "Draw!"
text = self.font.render(msg, True, self.RED)
text_rect = text.get_rect(
center=(self.window_size // 2,
self.window_size + self.bottom_margin // 10) # button and text position fixed...
)
self.screen.blit(text, text_rect)
# Draw buttons (position unchanged)
if not is_final_round:
pygame.draw.rect(self.screen, self.WHITE, self.next_button)
next_text = self.font.render("Next Game", True, self.BLACK)
next_text_rect = next_text.get_rect(center=self.next_button.center)
self.screen.blit(next_text, next_text_rect)
pygame.draw.rect(self.screen, self.WHITE, self.quit_button)
quit_text = self.font.render("Quit", True, self.BLACK)
quit_text_rect = quit_text.get_rect(center=self.quit_button.center)
self.screen.blit(quit_text, quit_text_rect)
def handle_game_over_input(self):
"""Handle button clicks after game over"""
mouse_pos = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0]: # Left click
if self.next_button.collidepoint(mouse_pos):
return "next"
elif self.quit_button.collidepoint(mouse_pos):
return "quit"
return None
class RandomGomokuPlayer:
def __init__(self, game):
self.game = game
def play(self, board):
a = np.random.randint(self.game.getActionSize())
valids = self.game.getValidMoves(board, 1)
while valids[a] != 1:
a = np.random.randint(self.game.getActionSize())
return a
class GreedyGomokuPlayer:
def __init__(self, game):
self.game = game
def play(self, board):
valids = self.game.getValidMoves(board, 1)
candidates = []
for a in range(self.game.getActionSize()):
if valids[a] == 0:
continue
nextBoard, _ = self.game.getNextState(board, 1, a)
score = self.game.getScore(nextBoard, 1)
candidates += [(-score, a)]
candidates.sort()
return candidates[0][1]
class HumanGomokuPlayer:
def __init__(self, game):
self.game = game
self.gui = GomokuGUI(game.n)
def play(self, board):
valid = self.game.getValidMoves(board, 1)
self.gui.draw_board(board)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = self.gui.get_mouse_position()
if pos:
x, y = pos
# Fix: Swap x and y to match the game's internal representation
a = self.game.n * y + x # Changed from n * x + y
if valid[a]:
pygame.display.flip()
return a
pygame.display.flip()
class Arena:
"""
An Arena class where any 2 agents can be pit against each other.
"""
def __init__(self, player1, player2, game, display=None):
"""
Input:
player 1,2: two functions that takes board as input, return action
game: Game object
display: a function that takes board as input and prints it. Is necessary for verbose
mode.
"""
self.player1 = player1
self.player2 = player2
self.game = game
self.display = display
self.player1_first = True # Track original player order
self.current_round = 0
self.total_rounds = 0 # Will be set in playGames
def playGame(self, verbose=False):
"""
Executes one episode of a game.
Returns:
either
winner: player who won the game (1 if player1, -1 if player2, 0 if draw)
"""
players = [self.player2, None, self.player1]
curPlayer = 1
board = self.game.getInitBoard()
# Reset GUI for new game if it exists
if hasattr(self.game, 'gui'):
# Pass player order information to GUI
self.game.gui = GomokuGUI(len(board), self.player1_first)
it = 0
while self.game.getGameEnded(board, curPlayer) is None:
it += 1
if verbose:
assert self.display
print("Turn ", str(it), "Player ", str(curPlayer))
self.display(board, self.player1_first) # Pass player order information
action = players[curPlayer + 1](
self.game.getCanonicalForm(board, curPlayer)
)
valids = self.game.getValidMoves(
self.game.getCanonicalForm(board, curPlayer), 1
)
if valids[action] == 0:
log.error(f"Action {action} is not valid!")
log.debug(f"valids = {valids}")
assert valids[action] > 0
board, curPlayer = self.game.getNextState(board, curPlayer, action)
if verbose:
assert self.display
print("Game over: Turn ", str(it), "Result ", str(curPlayer))
self.display(board, self.player1_first) # Pass player order information
if hasattr(self.game, 'gui'):
result = curPlayer * self.game.getGameEnded(board, curPlayer)
if not self.player1_first:
result = -result
# Check if this is the final round
self.current_round += 1
is_final_round = (self.current_round >= self.total_rounds)
self.game.gui.draw_game_over(result, is_final_round)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
action = self.game.gui.handle_game_over_input()
if action == "next" and not is_final_round:
break
elif action == "quit":
pygame.quit()
sys.exit()
pygame.display.flip()
return curPlayer * self.game.getGameEnded(board, curPlayer)
def playGames(self, num, verbose=False):
"""
Plays num games in which player1 starts num/2 games and player2 starts num/2 games.
Returns:
oneWon: games won by player1
twoWon: games won by player2
draws: games won by nobody
"""
self.total_rounds = num
self.current_round = 0
num = int(num / 2)
oneWon = 0
twoWon = 0
draws = 0
self.player1_first = True # First half: player1 goes first
for _ in tqdm(range(num), desc="Arena.playGames (player1 go first)"):
gameResult = self.playGame(verbose=verbose)
if gameResult == 1:
oneWon += 1
elif gameResult == -1:
twoWon += 1
else:
draws += 1
self.player1, self.player2 = self.player2, self.player1
self.player1_first = False # Second half: original player2 goes first
for _ in tqdm(range(num), desc="Arena.playGames (player2 go first)"):
gameResult = self.playGame(verbose=verbose)
if gameResult == -1:
oneWon += 1
elif gameResult == 1:
twoWon += 1
else:
draws += 1
return oneWon, twoWon, draws