CodeRigor

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.

Free to useLessons & exercises7 authored lessons3 units
Start with the first lesson.

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

Python
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.

UNIT 013 lessons Board and moves
  1. 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
  2. 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
  3. 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
UNIT 023 lessons Search
  1. 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
  2. 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
  3. 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
UNIT 031 lesson The engine
  1. LESSON 03.01

    Capstone: a chess engine that finds mates

    Combine evaluation, move ordering, quiescence and iterative deepening with mate scores into a small engine, and test it against brute-force mate searches.

    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

  1. 01Read and write a chess position

    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.

  2. 02Minimax and alpha-beta

  3. 03Bitboards

    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.