haive.games.nim.agent¶

Agent for playing Nim.

This module defines the Nim agent, which uses language models to generate moves and analyze positions in the game.

Classes¶

NimAgent

Agent for playing Nim.

Functions¶

ensure_game_state(state_input)

Ensure input is converted to NimState.

Module Contents¶

class haive.games.nim.agent.NimAgent(config=NimConfig())[source]¶

Bases: haive.games.framework.base.agent.GameAgent[haive.games.nim.config.NimConfig]

Agent for playing Nim.

Initialize the Nim agent.

Parameters:

config (NimConfig) – The configuration for the game.

analyze_player1(state)[source]¶

Analyze position for player1.

Parameters:

state (NimState) – The current game state.

Returns:

The command to analyze the position.

Return type:

Command

analyze_player2(state)[source]¶

Analyze position for player2.

Parameters:

state (NimState) – The current game state.

Returns:

The command to analyze the position.

Return type:

Command

analyze_position(state, player)[source]¶

Analyze the current position for the specified player.

Parameters:
  • state (haive.games.nim.state.NimState | dict[str, Any] | langgraph.types.Command) – The current game state.

  • player (str) – The player to analyze the position for.

Returns:

The command to analyze the position.

Return type:

Command

extract_move(response)[source]¶

Extract move from engine response.

Parameters:

response (Any) – The response from the engine.

Returns:

The move from the engine.

Return type:

Any

initialize_game(state)[source]¶

Initialize a new Nim game with configured pile sizes.

Parameters:

state (dict[str, Any] | haive.games.nim.state.NimState | langgraph.types.Command) – The initial state of the game.

Returns:

The command to initialize the game.

Return type:

Command

make_move(state, player)[source]¶

Make a move for the specified player.

Parameters:
  • state (haive.games.nim.state.NimState | dict[str, Any] | langgraph.types.Command) – The current game state.

  • player (str) – The player to make the move for.

Returns:

The command to make the move.

Return type:

Command

make_player1_move(state)[source]¶

Make a move for player1.

Parameters:

state (NimState) – The current game state.

Returns:

The command to make the move.

Return type:

Command

make_player2_move(state)[source]¶

Make a move for player2.

Parameters:

state (NimState) – The current game state.

Returns:

The command to make the move.

Return type:

Command

prepare_analysis_context(state, player)[source]¶

Prepare context for position analysis.

Parameters:
  • state (NimState) – The current game state.

  • player (str) – The player to prepare the context for.

Returns:

The context for the position analysis.

Return type:

Dict[str, Any]

prepare_move_context(state, player)[source]¶

Prepare context for move generation.

Parameters:
  • state (NimState) – The current game state.

  • player (str) – The player to prepare the context for.

Returns:

The context for the move generation.

Return type:

Dict[str, Any]

run_game(visualize=True)[source]¶

Run a complete Nim game with optional visualization.

Parameters:

visualize (bool) – Whether to visualize each game state.

Returns:

The final game state.

Return type:

Dict[str, Any]

run_game_with_ui(show_analysis=True)[source]¶

Run a complete Nim game with Rich UI.

This method runs a Nim game with Rich UI visualization, showing the game state after each move. It optionally includes analysis.

Parameters:

show_analysis (bool) – Whether to include analysis in the game.

Returns:

The final game state.

Return type:

Dict[str, Any]

setup_workflow()[source]¶

Set up the game workflow.

Returns:

None

Return type:

None

visualize_state(state)[source]¶

Visualize the current game state.

Parameters:

state (Dict[str, Any]) – The current game state.

Return type:

None

haive.games.nim.agent.ensure_game_state(state_input)[source]¶

Ensure input is converted to NimState.

This helper function ensures that the input state is properly converted to a NimState object, handling various input types (dict, NimState, Command).

Parameters:

state_input (dict[str, Any] | haive.games.nim.state.NimState | langgraph.types.Command) – The state to convert, which can be a dictionary, NimState, or Command.

Returns:

The converted state.

Return type:

NimState