forked from bymayanksingh/connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_data.py
More file actions
119 lines (100 loc) · 3.93 KB
/
game_data.py
File metadata and controls
119 lines (100 loc) · 3.93 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
from typing import Tuple, Optional, Any
from game_board import GameBoard
from agent_factory import make_agent
class GameData:
"""
The game data class contains all of the data for the game.
"""
radius: int
height: int
width: int
sq_size: int
size: Tuple[int, int]
game_over: bool
turn: int
last_move_row: [int]
last_move_col: [int]
game_board: GameBoard
# Agent-related fields
game_mode: str # 'pvp', 'pva', 'ava'
agent1: Optional[Any]
agent2: Optional[Any]
# Board size and win condition
cols: int
rows: int
win_condition: int
def __init__(self):
self.game_over = False
self.turn = 0
self.last_move_row = []
self.last_move_col = []
# Default board size
self.cols = 7
self.rows = 6
self.win_condition = 4
self.game_board = GameBoard(rows=self.rows, cols=self.cols)
self.action = None
self.panel_size = 600
self.sq_size: int = 100
self.width: int = self.cols * self.sq_size + self.panel_size
self.height: int = (self.rows + 1) * self.sq_size
self.size: Tuple[int, int] = (self.width, self.height)
self.radius: int = int(self.sq_size / 2 - 5)
# Initialize agent-related fields
self.game_mode = 'pvp' # Default to player vs player
self.agent1 = None
self.agent2 = None
def set_board_size(self, cols: int, rows: int, win_condition: int) -> None:
"""
Set the game board size and win condition.
Args:
cols: Number of columns in the board
rows: Number of rows in the board
win_condition: Number of pieces in a row needed to win
"""
self.cols = cols
self.rows = rows
self.win_condition = win_condition
# Reinitialize the game board with new dimensions
self.game_board = GameBoard(rows=rows, cols=cols, win_condition=win_condition)
# Update display size based on new dimensions
self.width = cols * self.sq_size + self.panel_size
self.height = (rows + 1) * self.sq_size
self.size = (self.width, self.height)
def set_game_mode(self, mode: str) -> None:
"""
Set the game mode and initialize agents if needed.
Args:
mode: 'pvp' for player vs player, 'pva' for player vs agent,
'ava' for agent vs agent
"""
self.game_mode = mode
if mode in ['pva', 'ava']:
# Create a new agent - no pre-training needed since it uses online learning
if self.agent1 is None:
print("Initializing agent ...")
# Centralized configuration via agent_factory
self.agent1 = make_agent(dp_only=True, gamma=0.95, verbose=False)
else:
# Reset the agent for a new game but preserve its learned values
print("Resetting agent for new game...")
self.agent1.reset()
# Ensure the reset agent keeps the configuration
self.agent1 = make_agent(dp_only=True, gamma=0.95, verbose=False)
if mode == 'ava':
# If you want independent agents, create a second one here.
# For now we reuse the same instance.
self.agent2 = self.agent1
def get_state_for_agent(self) -> Any:
"""
Convert the current game state to a format suitable for the agent.
Returns:
Any: The game state in agent-readable format
"""
return {
'board': self.game_board.board,
'turn': self.turn,
'game_board': self.game_board, # Include the game board reference
'last_move': (self.last_move_row[-1] if self.last_move_row else None,
self.last_move_col[-1] if self.last_move_col else None)
}