The course
Game AI: minimax and beyond is an expert Coding course in seven lessons, all in Python on a Raspberry Pi 4. Boards are drawn as text on the Serial Monitor and moves are typed in, and every step is checked against what the Pi prints or which lamp it lights.
- Game trees: tic-tac-toe as a list of 9 squares, a winner check from a table of lines, and recursion that counts every game from a position (3,468 from two moves in).
- Minimax: score a position assuming both players are perfect, pick the best move, prefer fast wins, and solve the whole game with a memo that holds all 5,478 positions. Then play it, and draw at best.
- Alpha-beta: count the work, then prune it. From two moves in, 7,332 positions become 871, then 768 with center-first ordering, and the whole game solves in 7,865 with no memo.
- Evaluation: Connect Four, a four-in-a-row check from the last move, and a score built from the 69 windows of four. A one-move player trusts it and misses a block, which is the case for searching deeper.
- Depth vs time: alpha-beta as negamax with a depth limit, a table of nodes and milliseconds per depth, iterative deepening against a budget, and a 3-move search that finds a forced win.
- Monte Carlo search: random playouts, flat Monte Carlo move choice, noise against sample size, and UCB1, the rule inside Monte Carlo tree search.
- Boss: an unbeatable Connect Four.
The finale
The boss lesson is a Connect Four game against the Pi on the Serial Monitor. You are X and type a column; the Pi answers with an alpha-beta search, first at a fixed depth of 4 and then with iterative deepening on a time budget, printing how deep it looked. The gallery's traffic light module shows the game: green for your move, amber while the Pi thinks, red when it wins.
Connect Four is solved: the first player wins with perfect play. The course says so, and explains what a truly perfect engine would add (a transposition table, better ordering, a sharper evaluation and an opening book). For nearly everyone who sits down to play, the Pi is unbeatable.
On the Pi
The catalog line for this course names an ESP32 with a touch screen. It runs on the Pi instead, so the engines are plain Python you can read and change, with the board as text.
The Pi runs MicroPython 1.29, not CPython, and that shows here:
time.monotonic()follows the simulated clock, which charges every turn of a Python loop the same small cost. Search times and the depth the clock allows are therefore the same on every run and every computer.randomis MicroPython's own generator. Withrandom.seed(2026)the playouts repeat exactly, but the numbers differ from CPython's.- A dict does not keep its keys in order, so results are printed one by one instead of printing the dict.
- Search is slower than on a desktop, so the lessons keep each search to a few thousand positions. Raise the depth or budget yourself to see it grow.