#!/usr/bin/env python3"""Example of playing a Mancala game.This script demonstrates how to initialize and run a Mancala gamewith the Haive framework. It provides a rich terminal UI and handlesvarious configuration options.Usage: python example.py [--stones=4] [--no-analysis] [--no-visual] [--debug]Options: --stones=N Set the number of stones per pit (default: 4) --no-analysis Disable position analysis --no-visual Disable visualization --debug Enable debug output"""importargparseimportloggingimporttracebackimportuuidfromhaive.core.config.runnableimportRunnableConfigManagerfromrich.consoleimportConsolefromrich.panelimportPanelfromhaive.games.mancala.agentimportMancalaAgentfromhaive.games.mancala.configimportMancalaConfig
[docs]defmain():"""Run the Mancala game demo."""# Set up argument parsingparser=argparse.ArgumentParser(description="Run a Mancala game demo")parser.add_argument("--stones",type=int,default=4,help="Number of stones per pit")parser.add_argument("--no-analysis",action="store_true",help="Disable position analysis")parser.add_argument("--no-visual",action="store_true",help="Disable visualization")parser.add_argument("--debug",action="store_true",help="Enable debug output")args=parser.parse_args()# Set up logginglog_level=logging.DEBUGifargs.debugelselogging.INFOlogging.basicConfig(level=log_level)logger=logging.getLogger("mancala_demo")# Create a console for rich outputconsole=Console()# Display headerconsole.print(Panel.fit("[bold cyan]Mancala Game Demo[/bold cyan]\n""An ancient board game of strategy and counting",border_style="cyan",))try:# Import the MancalaAgent# Display configurationconsole.print("[bold]Game Configuration:[/bold]")console.print(f"• Stones per pit: {args.stones}")console.print(f"• Position analysis: {'disabled'ifargs.no_analysiselse'enabled'}")console.print(f"• Visualization: {'disabled'ifargs.no_visualelse'enabled'}")console.print(f"• Debug mode: {'enabled'ifargs.debugelse'disabled'}")console.print()# Initialize the agent with configuration and increased recursion limit# Create a runnable config with higher recursion limitrunnable_config=RunnableConfigManager.create(thread_id=str(uuid.uuid4()),recursion_limit=200,# Increase from default 25 to avoid recursion limit errors)config=MancalaConfig(stones_per_pit=args.stones,enable_analysis=notargs.no_analysis,visualize=notargs.no_visual,runnable_config=runnable_config,)agent=MancalaAgent(config)console.print("[bold yellow]Starting game...[/bold yellow]")console.print("(This may take a moment to initialize the LLM engines)")console.print()# Run the gamefinal_state=agent.run({"initialize":{"stones_per_pit":args.stones}})# Display final resultiffinal_state:console.print(Panel.fit(f"[bold green]Game Complete![/bold green]\n"f"Final Score: Player 1: {final_state.player1_score}, Player 2: {final_state.player2_score}\n"f"Winner: {final_state.winneror'Draw'}",border_style="green",))else:console.print(Panel.fit("[bold yellow]Game ended without returning a final state[/bold yellow]\n""This might happen if an error occurred during gameplay.",border_style="yellow",))exceptModuleNotFoundErrorase:console.print(Panel.fit("[bold red]Error: Required module not found[/bold red]\n"f"{e!s}\n\n""This example requires the Haive framework and its dependencies.\n""If you want to test the core Mancala logic without these dependencies,\n""try running 'minimal_test.py' instead.",border_style="red",))exceptExceptionase:console.print(Panel.fit(f"[bold red]Error running game:[/bold red]\n{e!s}",border_style="red",))ifargs.debug:traceback.print_exc()else:console.print("Run with --debug flag for detailed error information.")