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
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
Flows
_play_human_turn
bool main._play_human_turn(chess.Board board)
Prompt for move, push to board, display.
examples/pychess/main.py:96
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
Flows
_print_game_result
None main._print_game_result(chess.Board board)
Display winner or draw status.
examples/pychess/main.py:76
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
Flows
game_loop
None main.game_loop()
Initialize engine, alternate human/AI turns until game over.
examples/pychess/main.py:142
Called by main
Calls _play_ai_turn, _play_human_turn, _print_game_result, create_engine, print_board, shutdown
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
Flows
main
None main.main()
Run game loop, handle keyboard interrupt.
examples/pychess/main.py:174
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
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()