main.py

examples/pychess/main.py

PyChess — play chess against a local LLM via entropic.

Owners: @tvanfossen

_play_ai_turn

bool main._play_ai_turn(object chess_engine, chess.Board board)

Run AI generation, fall back to manual input on failure.

examples/pychess/main.py:115

Show on the map →

Called by game_loop

Calls get_ai_move, get_human_move, print_board

Source · 24 lines
def _play_ai_turn(chess_engine: object, board: chess.Board) -> bool:
    """Handle AI's turn. Returns False if AI failed and player quit.

    @brief Run AI generation, fall back to manual input on failure.
    @version 2
    """
    import engine as eng

    print("AI is thinking...")
    ai_move = eng.get_ai_move(chess_engine)
    if ai_move is not None:
        print(f"AI plays: {ai_move}")
        print_board(board)
        return True

    print("\nAI could not find a move. Play for Black or type 'quit'.")
    move = get_human_move(board, prompt="Move for Black (UCI): ")
    if move is None:
        return False
    board.push(move)
    print_board(board)
    return True

Activity · 14 steps
lines 121–121import engine as englines 123–123print("AI is thinking...")lines 124–124ai_move = eng.get_ai_move(chess_engine)lines 125–125◇ if ai_move is not Nonelines 126–126print(f"AI plays: {ai_move}")lines 127–127print_board(board)lines 128–128return Truelines 130–130print("\nAI could not find a move. Play for Black or type 'quit'.")lines 131–131move = get_human_move(board, prompt="Move for Black (UCI): ")lines 132–132◇ if move is Nonelines 133–133return Falselines 134–134board.push(move)lines 135–135print_board(board)lines 136–136return True

Flows

_play_human_turn

bool main._play_human_turn(chess.Board board)

Prompt for move, push to board, display.

examples/pychess/main.py:96

Show on the map →

Called by game_loop

Calls get_human_move, print_board

Source · 15 lines
def _play_human_turn(board: chess.Board) -> bool:
    """Handle human's turn. Returns False if the player quit.

    @brief Prompt for move, push to board, display.
    @version 2
    """
    move = get_human_move(board)
    if move is None:
        print("Goodbye!")
        return False
    board.push(move)
    print_board(board)
    return True

Activity · 7 steps
lines 102–102move = get_human_move(board)lines 103–103◇ if move is Nonelines 104–104print("Goodbye!")lines 105–105return Falselines 106–106board.push(move)lines 107–107print_board(board)lines 108–108return True

Flows

_print_game_result

None main._print_game_result(chess.Board board)

Display winner or draw status.

examples/pychess/main.py:76

Show on the map →

Called by game_loop

Calls none

Source · 16 lines
def _print_game_result(board: chess.Board) -> None:
    """Print the game outcome.

    @brief Display winner or draw status.
    @version 1
    """
    result = board.result()
    print(f"Game over: {result}")
    outcome = board.outcome()
    if outcome and outcome.winner is not None:
        winner = "White" if outcome.winner == chess.WHITE else "Black"
        print(f"Winner: {winner}")
    else:
        print("Draw!")

Activity · 8 steps
lines 82–82result = board.result()lines 83–83print(f"Game over: {result}")lines 84–84outcome = board.outcome()lines 85–85◇ if outcome and outcome.winner is not Nonelines 86–86winner = "White" if outcome.winner == chess.WHITE else "Black"lines 87–87print(f"Winner: {winner}")lines 88–88elselines 89–89print("Draw!")

Flows

game_loop

None main.game_loop()

Initialize engine, alternate human/AI turns until game over.

examples/pychess/main.py:142

Source · 29 lines
def game_loop() -> None:
    """Run the main game loop.

    @brief Initialize engine, alternate human/AI turns until game over.
    @version 3
    """
    import engine as eng

    print("Loading model...")
    chess_engine = eng.create_engine()
    board = chess_engine.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:
            if not _play_human_turn(board):
                break
        elif not _play_ai_turn(chess_engine, board):
            break

    if board.is_game_over():
        _print_game_result(board)

    eng.shutdown(chess_engine)

Activity · 16 steps
lines 148–148import engine as englines 150–150print("Loading model...")lines 151–151chess_engine = eng.create_engine()lines 152–152board = chess_engine.boardlines 154–154print("PyChess — You are White, AI is Black.")lines 155–155print("Enter moves in UCI notation (e.g. e2e4). Type 'quit' to exit.")lines 156–156print_board(board)lines 158–158◇ while not board.is_game_over()lines 159–159◇ if board.turn == chess.WHITElines 160–160◇ if not _play_human_turn(board)lines 161–161breaklines 162–162◇ elif not _play_ai_turn(chess_engine, board)lines 163–163breaklines 165–165◇ if board.is_game_over()lines 166–166_print_game_result(board)lines 168–168eng.shutdown(chess_engine)

Flows

main

None main.main()

Run game loop, handle keyboard interrupt.

examples/pychess/main.py:174

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 180–180trylines 181–181game_loop()lines 182–182except KeyboardInterruptlines 183–183print("\nInterrupted.")lines 184–184sys.exit(0)

Flows

print_board

None main.print_board(chess.Board board)

Display board state and move list to stdout.

examples/pychess/main.py:25

Source · 20 lines
def print_board(board: chess.Board) -> None:
    """Print the board with coordinates and move history.

    @brief Display board state and move list to stdout.
    @version 1
    """
    print()
    print(format_board_text(board))
    if board.move_stack:
        moves = []
        for i, move in enumerate(board.move_stack):
            ply = i + 1
            if ply % 2 == 1:
                moves.append(f"{(ply + 1) // 2}. {move.uci()}")
            else:
                moves[-1] += f" {move.uci()}"
        print(f"\nMoves: {' '.join(moves)}")
    print()

Activity · 12 steps
lines 31–31print()lines 32–32print(format_board_text(board))lines 33–33◇ if board.move_stacklines 34–34moves = []lines 35–35◇ for i, move in enumerate(board.move_stack)lines 36–36ply = i + 1lines 37–37◇ if ply % 2 == 1lines 38–38moves.append(f"{(ply + 1) // 2}. {move.uci()}")lines 39–39elselines 40–40moves[-1] += f" {move.uci()}"lines 41–41print(f"\nMoves: {' '.join(moves)}")lines 42–42print()

Flows