haive.games.checkers.state_manager¶
Checkers game state management module.
This module provides comprehensive state management functionality for the classic Checkers game, including board management, move validation, jump detection, and king promotion handling.
Checkers is a classic strategy game played on an 8×8 board with 64 squares, using only the dark squares. Each player starts with 12 pieces on their side of the board. Regular pieces move diagonally forward, but kings can move diagonally in any direction. Players capture opponent pieces by jumping over them, and multiple jumps are possible.
- Classes:
CheckersStateManager: Main state management class for checkers operations.
Example
Basic checkers game setup and play:
>>> from haive.games.checkers.state_manager import CheckersStateManager
>>> from haive.games.checkers.models import CheckersMove
>>>
>>> # Initialize standard checkers game
>>> state = CheckersStateManager.initialize()
>>> print(f"Current player: {state.current_player}") # "red"
>>> print(f"Board size: 8x8 with {len(state.pieces)} total pieces")
>>>
>>> # Get legal moves (including mandatory jumps)
>>> legal_moves = CheckersStateManager.get_legal_moves(state)
>>> print(f"Available moves: {len(legal_moves)}")
>>>
>>> # Make a move
>>> if legal_moves:
... move = legal_moves[0]
... new_state = CheckersStateManager.apply_move(state, move)
... print(f"Move applied: {move.from_square} to {move.to_square}")
Note
Uses standard 8×8 checkers board with 64 squares (only dark squares used)
Players are “red” and “black” with red moving first
Mandatory jump rule: if a jump is available, it must be taken
Kings are promoted when pieces reach the opposite end of the board
Multiple jumps in sequence are supported when available
Classes¶
Manager for checkers game state. |
Module Contents¶
- class haive.games.checkers.state_manager.CheckersStateManager[source]¶
Manager for checkers game state.
- This class provides static methods for managing checkers game states:
Game initialization with default settings
Legal move generation (including mandatory jumps)
Move application with validation
Analysis updates
Game status checks
King promotion handling
The manager implements a functional approach where methods take the current state and return a new state, rather than modifying the state in place.
- classmethod apply_move(state, move)[source]¶
Apply a move to the current game state.
Takes a move and applies it to the current state, returning a new state. Handles piece movement, captures, king promotion, and game status updates.
- Parameters:
state (CheckersState) – Current game state
move (CheckersMove) – Move to apply
- Returns:
New game state after the move
- Return type:
CheckersState
Examples
>>> state = CheckersStateManager.initialize() >>> moves = CheckersStateManager.get_legal_moves(state) >>> new_state = CheckersStateManager.apply_move(state, moves[0]) >>> new_state.turn 'black' >>> len(new_state.move_history) 1
- classmethod check_game_status(state)[source]¶
Check and update the game status.
Evaluates the current game state to determine if the game is over and who the winner is, if any.
Game-ending conditions include: - A player has no pieces left - A player has no legal moves
- Parameters:
state (CheckersState) – Current game state
- Returns:
Updated game state with correct status
- Return type:
CheckersState
- classmethod get_legal_moves(state)[source]¶
Get all legal moves for the current player.
Checks for all legal moves in the current position, following checkers rules: - If jump moves are available, only jump moves are returned (mandatory jumps) - Otherwise, regular moves are returned - Kings can move in all diagonal directions - Regular pieces can only move forward (down for black, up for red)
- Parameters:
state (CheckersState) – Current game state
- Returns:
List of legal moves for the current player
- Return type:
list[CheckersMove]
Examples
>>> state = CheckersStateManager.initialize() >>> moves = CheckersStateManager.get_legal_moves(state) >>> len(moves) > 0 True >>> all(move.player == "red" for move in moves) True
- classmethod initialize()[source]¶
Initialize a new checkers game.
Creates a fresh checkers game state with the standard starting board configuration, red to move first, and initial values for all tracking fields.
- Returns:
A new game state with the initial board setup.
- Return type:
CheckersState
Examples
>>> state = CheckersStateManager.initialize() >>> state.turn 'red' >>> state.game_status 'ongoing' >>> len(state.move_history) 0