games.tic_tac_toe.state_manager¶
Tic Tac Toe game state management module.
This module provides comprehensive state management functionality for the classic Tic Tac Toe game, including board initialization, move validation, win detection, and game status tracking.
Tic Tac Toe is a classic strategy game played on a 3×3 grid where players take turns placing their marks (X or O) in empty squares. The first player to get three marks in a row (horizontally, vertically, or diagonally) wins.
- Classes:
TicTacToeStateManager: Main state management class for Tic Tac Toe operations.
Example
Basic Tic Tac Toe game setup and play:
>>> from haive.games.tic_tac_toe.state_manager import TicTacToeStateManager
>>> from haive.games.tic_tac_toe.models import TicTacToeMove
>>>
>>> # Initialize game
>>> state = TicTacToeStateManager.initialize()
>>> print(f"Current player: {state.turn}")
>>>
>>> # Make a move in the center
>>> move = TicTacToeMove(row=1, col=1, player="X")
>>> new_state = TicTacToeStateManager.apply_move(state, move)
>>>
>>> # Check for winners
>>> winner = TicTacToeStateManager.get_winner(new_state)
>>> print(f"Winner: {winner}")
Note
The board uses 0-based indexing where (0,0) is top-left and (2,2) is bottom-right. Players are represented as “X” and “O” strings in the game state.
Classes¶
Manager for Tic Tac Toe game state. |
Module Contents¶
- class games.tic_tac_toe.state_manager.TicTacToeStateManager¶
Bases:
haive.games.framework.base.state_manager.GameStateManager
[haive.games.tic_tac_toe.state.TicTacToeState
]Manager for Tic Tac Toe game state.
- classmethod add_analysis(state, player, analysis)¶
Add an analysis to the state.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
player (str) – The player who performed the analysis.
analysis (haive.games.tic_tac_toe.models.TicTacToeAnalysis) – The analysis to add.
- Returns:
Updated state with the analysis added.
- Return type:
- classmethod apply_move(state, move)¶
Apply a move to the current state and return the new state.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
move (haive.games.tic_tac_toe.models.TicTacToeMove) – The move to apply.
- Returns:
A new game state after applying the move.
- Return type:
- Raises:
ValueError – If the move is invalid.
- classmethod check_game_status(state)¶
Check and update the game status.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
- Returns:
The game state with updated status.
- Return type:
- classmethod find_winning_move(state, player)¶
Find a winning move for the specified player, if any.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
player (str) – The player to find a winning move for (‘X’ or ‘O’).
- Returns:
List of winning move coordinates (row, col), or empty list if none.
- Return type:
- classmethod get_legal_moves(state)¶
Get all legal moves for the current state.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
- Returns:
A list of all legal moves.
- Return type:
List[TicTacToeMove]
- classmethod get_winner(state)¶
Get the winner of the game, if any.
- Parameters:
state (haive.games.tic_tac_toe.state.TicTacToeState) – The current game state.
- Returns:
The winner (‘X’ or ‘O’), or None if the game is ongoing or a draw.
- Return type:
Optional[str]
- classmethod initialize(**kwargs)¶
Initialize a new Tic Tac Toe game.
- Parameters:
**kwargs – Keyword arguments for game initialization. first_player: Which player goes first (‘X’ or ‘O’). Default is ‘X’. player_X: Which player is X (‘player1’ or ‘player2’). Default is ‘player1’. player_O: Which player is O (‘player1’ or ‘player2’). Default is ‘player2’.
- Returns:
A new Tic Tac Toe game state.
- Return type: