← All posts

Goofy Chess: A Board, Some Images, and a Library Doing All the Work

2023Python · Game

Chess rules are a thousand edge cases hiding inside a simple premise. Castling requires tracking king and rook movement. En passant exists for exactly one half move after a double pawn push. Pawn promotion changes the piece mid-game. Stalemate, checkmate, insufficient material, threefold repetition. Implementing all of that from scratch is a project in itself. I wanted to build a chess game, not a chess engine.

python-chess handled every rule. My job was the interface.

The Library Did the Work

The board state lives in a chess.Board object. It tracks piece positions, move history, castling rights, en passant targets, and whose turn it is. When a player clicks a square, the code converts the pixel coordinate to a chess square using a 7-row subtraction to account for tkinter inverted y-axis. The first click selects a piece. The second click creates a Move and checks board.legal_moves. If the move passes validation, board.push executes it and updates every internal state automatically.

The rendering is a read-only snapshot of that state. The draw function clears the canvas, draws an 8x8 grid with alternating colors, and places piece images by calling board.piece_at on every square. It knows nothing about rules. It just reflects whatever the board object currently holds.

What Is Missing

The game does not detect checkmate. Players have to notice the game ended and close the window themselves. There is no undo, no move history, no restart button, and no indication of whose turn it is. If the images folder goes missing, the program crashes without a helpful error.

It is 92 lines of glue code connecting a GUI library to a chess library. The interesting part of the project was not writing the code. It was watching a 20-line function call handle castling, en passant, and promotion correctly on the first try because the library had already been debugged by someone else.

That is the entire point of using a library built by someone else.

View all projects →View on GitHub ↗