haive.games.chess.utils¶
Chess game utility functions.
- This module provides helper functions for the chess game, including:
Game status determination
Board visualization
Move validation
These utilities support the core functionality of the chess module by providing common operations used across different components.
Functions¶
|
Determine the current game status based on the board position. |
|
Generate an ASCII representation of the chess board. |
|
Validate a chess move and return the resulting position. |
Module Contents¶
- haive.games.chess.utils.determine_game_status(board)¶
Determine the current game status based on the board position.
Analyzes a chess board to determine its current status (checkmate, stalemate, check, etc.) based on the rules of chess.
- Parameters:
board (chess.Board) – Chess board to analyze
- Returns:
- Game status as one of: “checkmate”, “stalemate”, “draw”,
”check”, or “ongoing”
- Return type:
Examples
>>> board = chess.Board() >>> determine_game_status(board) 'ongoing'
>>> # Fool's mate position >>> board = chess.Board("rnbqkbnr/pppp1ppp/8/4p3/6P1/5P2/PPPPP2P/RNBQKBNR b KQkq - 0 2") >>> board.push_san("Qh4#") >>> determine_game_status(board) 'checkmate'
- haive.games.chess.utils.generate_ascii_board(fen, last_move=None)¶
Generate an ASCII representation of the chess board.
Creates a text-based visualization of a chess board from its FEN representation, optionally highlighting the last move made.
- Parameters:
- Returns:
- ASCII representation of the board with coordinates and
optional move highlighting
- Return type:
Examples
>>> # Starting position >>> board_ascii = generate_ascii_board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") >>> print(board_ascii.split("\\n")[0]) '8 r n b q k b n r'
>>> # With last move highlighted >>> board_ascii = generate_ascii_board( ... "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", ... "e2e4" ... )
- haive.games.chess.utils.validate_move(fen, move_uci)¶
Validate a chess move and return the resulting position.
Checks if a move is valid in the given position and returns validation results including error messages and the resulting position if valid.
- Parameters:
- Returns:
- A tuple containing:
is_valid (bool): Whether the move is legal
error_message (str | None): Error message if move is invalid, None otherwise
resulting_fen (str | None): FEN of the position after the move if valid, None otherwise
- Return type:
Examples
>>> # Valid move >>> is_valid, error, new_fen = validate_move( ... "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", ... "e2e4" ... ) >>> is_valid True >>> error is None True >>> new_fen.startswith("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR") True
>>> # Invalid move >>> is_valid, error, new_fen = validate_move( ... "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", ... "e2e5" ... ) >>> is_valid False >>> "not legal" in error True >>> new_fen is None True