A tour of building a chess program from scratch. Its first chapter, The Board and Pieces, covers how the board and pieces are represented, which is the representation this lesson makes you build and check.
COURSE / REAL PRACTICE
Build a chess engine
Build a chess engine in plain Python: read positions, generate every legal move and match known move counts exactly, then search with alpha-beta, bitboards and a transposition table. Lessons pair with public videos by Sebastian Lague. Unofficial practice material, not affiliated with the video’s author.
Read the explanation and diagrams, then work through the practice. Your browser profile supplies your saved progress.
What you’ll work toward
Generate legal chess moves that match published perft counts, search them with pruning that never changes the answer, and finish with an engine that solves short mate and tactics positions.
Read an explanation, inspect the diagrams and work through the available checks, debugging exercises and builds. Feedback and prerequisite links guide the next step.
Before you start
This course builds on these skills:
Individual lessons can add prerequisites. Your lesson progress shows your actual readiness.
A look inside the first lesson
Read and write a chess position
names = [f + r for r in "12345678" for f in "abcdefgh"]
print(names[0], names[7], names[8], names[63])
print(names.index("e4"), 3 * 8 + 4)Your lesson sequence
Follow the same units and lessons in either view. Prerequisite links and your practice progress stay with each lesson.
Board and moves
- LESSON 01.01
Read and write a chess position
Number the 64 squares, parse a FEN string into an immutable position with strict validation, write it back byte for byte, and draw the board.
Open lesson - LESSON 01.02
Moves for every piece
Generate the pseudo-legal moves of knights, kings, sliders and pawns on a 64-square board, with no wrap-around at the edges, and test whether a square is attacked.
Open lesson - LESSON 01.03
Legal moves and perft
Play moves on a copy, reject any that leave your king attacked, handle castling and en passant exactly, and prove it against published perft counts.
Open lesson
Search
- LESSON 02.01
Minimax and alpha-beta
Search a game tree with negamax, prune it with alpha-beta without changing the answer, order moves to prune more, and check it against exhaustive minimax.
Open lesson - LESSON 02.02
Bitboards
Store a set of squares in one integer and build edge-safe shifts, knight, king and pawn attacks, blocker-aware sliding attacks and passed pawns, graded against a slow oracle.
Open lesson - LESSON 02.03
Zobrist keys and a transposition table
Hash a position with XORed random keys, update the hash move by move, and let alpha-beta reuse results through a fixed-size table with exact, lower and upper bounds.
Open lesson
WATCH ALONG
Videos for these lessons
One hand-picked talk, lecture or tutorial per lesson, in course order. They play here when you press play; nothing loads from the video provider before that.
3 videos· 1 h 41 min
01Read and write a chess position
-
An eleven-minute walk through game-tree search, evaluation, minimax and alpha-beta pruning, ending with the pruning code and an example. Watch it before building, then compare its pruning with the node counts here.
-
The video has chapters on bitboards and on a lookup-table method for sliding pieces, among search topics. Watch the bitboard part first, then build the shifts, masks and ray cutting yourself and compare against a slow oracle.