main_wrapper.py¶
examples/pychess/main_wrapper.py
PyChess — play chess against a local LLM via the entropic Python wrapper.
Owners: @tvanfossen
_play_game
None main_wrapper._play_game(EntropicEngine engine)
Alternate human/AI turns until game over or quit.
examples/pychess/main_wrapper.py:231
Source · 34 lines
def _play_game(engine: EntropicEngine) -> None:
"""Run the main game loop.
@brief Alternate human/AI turns until game over or quit.
@version 1
"""
board = chess.Board()
print("PyChess — You are White, AI is Black.")
print("Enter moves in UCI notation (e.g. e2e4). Type 'quit' to exit.")
print_board(board)
while not board.is_game_over():
if board.turn == chess.WHITE:
move = get_human_move(board)
if move is None:
print("Goodbye!")
break
board.push(move)
print_board(board)
else:
print("AI is thinking...")
ai_move = _get_ai_move(engine, board)
if ai_move:
print(f"\nAI plays: {ai_move}")
print_board(board)
else:
print("\nAI could not find a valid move.")
break
if board.is_game_over():
result = board.result()
print(f"Game over: {result}")
Activity · 24 steps
Flows
game_loop
None main_wrapper.game_loop()
Initialize engine with MCP server, run game, clean up.
examples/pychess/main_wrapper.py:268
Source · 15 lines
def game_loop() -> None:
"""Create engine and run the chess game.
@brief Initialize engine with MCP server, run game, clean up.
@version 1
"""
project_dir = str(EXAMPLE_ROOT / ".pychess")
engine = EntropicEngine()
engine.configure_dir(project_dir)
server_json = json.dumps({"command": "python3", "args": ["chess_server.py"]})
engine.register_mcp_server("chess", server_json)
_play_game(engine)
engine.destroy()
Activity · 7 steps
Flows
main
None main_wrapper.main()
Run game loop, handle keyboard interrupt.
examples/pychess/main_wrapper.py:286
Source · 13 lines
def main() -> None:
"""Entry point.
@brief Run game loop, handle keyboard interrupt.
@version 1
"""
try:
game_loop()
except KeyboardInterrupt:
print("\nInterrupted.")
sys.exit(0)