games.reversi.state_manager¶
Reversi (Othello) game state management module.
This module provides comprehensive state management functionality for Reversi/Othello, including move validation, disc flipping mechanics, and game progression tracking.
Reversi (also known as Othello) is a strategy board game played on an 8×8 board with 64 discs that are black on one side and white on the other. Players take turns placing discs with their color facing up, attempting to trap opponent discs between their own to flip them. The game starts with four discs in the center in a cross pattern.
- Classes:
ReversiStateManager: Main state management class for Reversi/Othello operations.
Example
Basic Reversi game setup and play:
>>> from haive.games.reversi.state_manager import ReversiStateManager
>>> from haive.games.reversi.models import ReversiMove
>>>
>>> # Initialize standard Reversi game
>>> state = ReversiStateManager.initialize()
>>> print(f"Current player: {state.turn}") # "B" (Black)
>>> print(f"Board size: 8x8")
>>> print(f"Black discs: {state.black_count}, White discs: {state.white_count}")
>>>
>>> # Get legal moves (must flip at least one opponent disc)
>>> legal_moves = ReversiStateManager.get_legal_moves(state)
>>> print(f"Legal moves for Black: {len(legal_moves)}")
>>>
>>> # Make a move
>>> if legal_moves:
... move = legal_moves[0]
... new_state = ReversiStateManager.apply_move(state, move)
... print(f"Move at ({move.row}, {move.col}) flipped {move.flipped_count} discs")
Note
Standard 8×8 board with initial cross pattern in center
Players are “B” (Black) and “W” (White) with Black moving first
Legal moves must flip at least one opponent disc
Game ends when no legal moves exist for both players
Winner is determined by who has more discs when game ends
Pass moves are automatic when no legal moves exist
Classes¶
Manager for Reversi/Othello game state. |
Module Contents¶
- class games.reversi.state_manager.ReversiStateManager¶
Bases:
haive.games.framework.base.state_manager.GameStateManager
[haive.games.reversi.state.ReversiState
]Manager for Reversi/Othello game state.
- classmethod add_analysis(state, player, analysis)¶
Add an analysis to the state.
- Parameters:
state (haive.games.reversi.state.ReversiState) – The current game state.
player (str) – The player who performed the analysis.
analysis (haive.games.reversi.models.ReversiAnalysis) – 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.reversi.state.ReversiState) – The current game state.
move (haive.games.reversi.models.ReversiMove) – 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.reversi.state.ReversiState) – The current game state.
- Returns:
The game state with updated status.
- Return type:
- classmethod get_legal_moves(state)¶
Get all legal moves for the current state.
- Parameters:
state (haive.games.reversi.state.ReversiState) – The current game state.
- Returns:
A list of all legal moves.
- Return type:
List[ReversiMove]
- classmethod get_skip_move(state)¶
Apply a skip move when player has no legal moves.
- Parameters:
state (haive.games.reversi.state.ReversiState) – The current game state.
- Returns:
A new game state after skipping the turn.
- Return type:
- classmethod get_winner(state)¶
Get the winner of the game, if any.
- Parameters:
state (haive.games.reversi.state.ReversiState) – The current game state.
- Returns:
The winner (‘B’ or ‘W’), or None if the game is ongoing or a draw.
- Return type:
Optional[str]
- classmethod initialize(**kwargs)¶
Initialize a new Reversi/Othello game.
- Parameters:
**kwargs – Keyword arguments for game initialization. first_player: Which player goes first (‘B’ or ‘W’). Default is ‘B’. player_B: Which player is Black (‘player1’ or ‘player2’). Default is ‘player1’. player_W: Which player is White (‘player1’ or ‘player2’). Default is ‘player2’.
- Returns:
A new Reversi game state.
- Return type:
- classmethod is_legal_move(state, row, col, player)¶
Check if a move is legal.
- Parameters:
state (haive.games.reversi.state.ReversiState) – The current game state.
row (int) – Row index of the move.
col (int) – Column index of the move.
player (str) – Player making the move (‘B’ or ‘W’).
- Returns:
True if the move is legal, False otherwise.
- Return type: