#!/usr/bin/env python3"""Simple runner for Fox and Geese game."""importloggingimporttracebackimportuuidfromrich.consoleimportConsolefromrich.panelimportPanelfromhaive.games.fox_and_geese.agentimportFoxAndGeeseAgentfromhaive.games.fox_and_geese.configimportFoxAndGeeseConfig# Set up logging - uncomment to see detailed debug logslogging.basicConfig(level=logging.CRITICAL)
[docs]defrun_fox_and_geese_game(agent:FoxAndGeeseAgent,thread_id:str|None=None):"""Run a Fox and Geese game using agent.run()."""console=Console()# Generate or use thread_id for persistencethread_id=thread_idorf"fox_geese_thread_{uuid.uuid4().hex[:8]}"console.print(f"๐งต Using thread_id: [cyan]{thread_id}[/cyan]")# Initialize the game stateinitial_state=agent.state_manager.initialize()console.print(Panel(f"๐ฆ Fox at position: {initial_state.fox_position}\n"f"๐ชฟ {initial_state.num_geese} geese on the board",title="๐ฎ Starting Fox and Geese game",border_style="green",))try:# Run the full agent workflow until ENDfinal_state=agent.run(initial_state,thread_id=thread_id)# Display final game outcomeconsole.print("\n")console.print(Panel(f"๐ฏ Winner: [bold]{final_state.winneror'None'}[/bold]\n"f"๐ Game Status: [bold]{final_state.game_status}[/bold]\n"f"๐ข Total moves: [bold]{len(final_state.move_history)}[/bold]\n"f"๐ชฟ Geese remaining: [bold]{final_state.num_geese}[/bold]",title="๐ Game Over!",border_style="yellow",))# Display last few movesiffinal_state.move_history:moves_text="\n".join([f" - {move}"formoveinfinal_state.move_history[-5:]])console.print(Panel(moves_text,title="๐ Recent moves",border_style="blue"))# Display analyses if availableifhasattr(final_state,"fox_analysis")andfinal_state.fox_analysis:console.print(Panel(final_state.fox_analysis[-1],title="๐ฆ Fox Analysis",border_style="red",))ifhasattr(final_state,"geese_analysis")andfinal_state.geese_analysis:console.print(Panel(final_state.geese_analysis[-1],title="๐ชฟ Geese Analysis",border_style="cyan",))returnfinal_stateexceptExceptionase:console.print(f"[bold red]Error running game: {e}[/bold red]")returnNone
[docs]defrun_fox_and_geese_with_ui(agent:FoxAndGeeseAgent,delay:float=2.0):"""Run a Fox and Geese game with UI visualization."""console=Console()console.print(Panel("Starting Fox and Geese game with Rich UI",title="๐ฎ Fox and Geese",border_style="cyan",))try:# Run the game with UIfinal_state=agent.run_game_with_ui(delay=delay)console.print("\n")console.print(Panel("Game has completed!",title="๐ Game Complete",border_style="green"))returnfinal_stateexceptExceptionase:console.print(f"[bold red]Error running game with UI: {e}[/bold red]")returnNone
# Entry pointif__name__=="__main__":console=Console()try:# Create agent configconfig=FoxAndGeeseConfig(name="fox_and_geese_game",enable_analysis=True,visualize=True,runnable_config={"configurable":{"thread_id":uuid.uuid4().hex[:8],"recursion_limit":400,}},)# Create agentagent=FoxAndGeeseAgent(config)# Choose run modeconsole.print(Panel("1. Run with Rich UI (recommended)\n2. Run in console mode",title="๐ฎ Fox and Geese Game Options",border_style="cyan",))choice=input("Enter choice (1 or 2): ").strip()ifchoice=="1":run_fox_and_geese_with_ui(agent,delay=1.5)else:run_fox_and_geese_game(agent)exceptExceptionase:console.print(f"[bold red]Critical error: {e}[/bold red]")console.print(Panel(traceback.format_exc(),title="Error Details",border_style="red"))