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

Show on the map →

Called by game_loop

Calls _get_ai_move, get_human_move, print_board

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
lines 237–237board = chess.Board()lines 238–238print("PyChess — You are White, AI is Black.")lines 239–239print("Enter moves in UCI notation (e.g. e2e4). Type 'quit' to exit.")lines 240–240print_board(board)lines 242–242◇ while not board.is_game_over()lines 243–243◇ if board.turn == chess.WHITElines 244–244move = get_human_move(board)lines 245–245◇ if move is Nonelines 246–246print("Goodbye!")lines 247–247breaklines 248–248board.push(move)lines 249–249print_board(board)lines 250–250elselines 251–251print("AI is thinking...")lines 252–252ai_move = _get_ai_move(engine, board)lines 253–253◇ if ai_movelines 254–254print(f"\nAI plays: {ai_move}")lines 255–255print_board(board)lines 256–256elselines 257–257print("\nAI could not find a valid move.")lines 258–258breaklines 260–260◇ if board.is_game_over()lines 261–261result = board.result()lines 262–262print(f"Game over: {result}")

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
lines 274–274project_dir = str(EXAMPLE_ROOT / ".pychess")lines 275–275engine = EntropicEngine()lines 276–276engine.configure_dir(project_dir)lines 277–277server_json = json.dumps({"command": "python3", "args": ["chess_server.py"]})lines 278–278engine.register_mcp_server("chess", server_json)lines 279–279_play_game(engine)lines 280–280engine.destroy()

Flows

main

None main_wrapper.main()

Run game loop, handle keyboard interrupt.

examples/pychess/main_wrapper.py:286

Show on the map →

Called by none

Calls game_loop

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)

Activity · 5 steps
lines 292–292trylines 293–293game_loop()lines 294–294except KeyboardInterruptlines 295–295print("\nInterrupted.")lines 296–296sys.exit(0)

Flows