Example lessons

Shell Scavenger Hunt

A terminal puzzle: follow clues through a folder tree with cd, ls and cat, and find the treasure hidden among decoys

1 class period22 files

Welcome to CS Room

First day on CS Room: use the terminal, run Python, and publish a live web page

1 class period5 files

Tic Tac Toe

Build an unbeatable Tic-Tac-Toe AI with minimax

4-5 class periods5 filesIncludes tests
Lesson plan
Tic Tac Toe

Tic-Tac-Toe AI

Students write an unbeatable Tic-Tac-Toe player using minimax. It's about 50 lines of code across five functions. The hard part is the idea: recursion, and searching a tree of future games. A factorial warm-up comes first, so students who have never written a recursive function get one working before minimax.

Time: 4 class periods of 45 minutes, plus an optional fifth. Minimax is where students stall, and most classes need two periods for it. Prerequisites: Python functions, loops, conditionals and lists. No recursion needed. Tests: 27 (5 for the warm-up, 22 for the game), set up for you when you import the lesson

Goals

By the end, students can:

  • write a recursive function with a base case and a recursive case
  • explain how minimax picks a move by scoring every possible rest of the game
  • trace minimax by hand on a small board
  • use a test suite to check their work one function at a time

Before class

  • Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson on the class's Assignments tab. It arrives as a draft with its tests already set up.
  • Publish it. Students get a Run Tests button, and their scores appear in the Gradebook.
  • The reference solution is the Reference solution link on the lesson's card on the Lessons page.

In class

Day 1: game trees and win detection

  1. Ask: can Tic-Tac-Toe be solved? What would "unbeatable" mean? (Perfect play from both sides always ends in a draw.) (5 min)

  2. Hand-trace minimax as a class on this board, with the AI (O) to move. Draw the tree on the whiteboard and score each finished game +10, −10 or 0. (15 min)

    X | O | X
    O | X | _
    _ | _ | O
    

    O has to block at the bottom-left square, and the game ends in a draw (score 0).

  3. Tour the files: README.md, warmup_factorial.py, main.py and the two test files. (5 min)

  4. Students write check_winner and is_board_full. They should reach 10 of 22 on test_game.py. (20 min)

Day 2: recursion

  1. Teach recursion with factorial on the whiteboard: the base case, the recursive case, and how factorial(3) calls down and returns back up. (10 min)
  2. Students write factorial and get 5 of 5 on test_warmup.py. Pair up anyone who can't: a student who can't write factorial won't get through minimax. (15 min)
  3. Bridge: factorial has one base case and one recursive call. Minimax has three base cases and one recursive call per empty square. (5 min)
  4. Write the three base cases of minimax together. (15 min)

Day 3: minimax

  1. Students write the recursive case of minimax. Walk the room; the common mistakes are in the table below. (35 min)
  2. Students who finish start get_best_move. (10 min)

Day 4: best move and play

  1. Students write get_best_move and reach 22 of 22. Anyone still on minimax keeps going. (20 min)
  2. Students play their AI with python main.py and try to beat it. (10 min)
  3. Discuss: why can't you beat it? Why wouldn't this work for chess? How is it different from how a chatbot picks its next word? (15 min)

Day 5 (optional): speed

  1. Students add a counter to minimax. With the AI moving first on an empty board it looks at about 550,000 positions. Chess has about 35 legal moves a turn, so looking just 8 moves ahead is about 2 trillion positions. (10 min)
  2. Teach alpha-beta pruning on a small tree: once the AI has found a move that draws (0), it can stop exploring any other move as soon as it sees a human reply there that wins (−10), because that move can only be worse. Students add it. The first move drops to about 31,000 positions, and the AI plays just as well. (25 min)
  3. Compare the counts as a class. (10 min)

Where students get stuck

SymptomCause
check_winner says " " wonThey didn't check that the line isn't empty: board[a] != EMPTY and board[a] == board[b] == board[c]
RecursionErrorA base case never fires, usually because check_winner or is_board_full is wrong
The AI plays badlyScore signs are flipped, or is_ai_turn isn't flipped on the recursive call
get_best_move fails its testsIt returns the score instead of the square

Check for understanding

  • The test score: 27 of 27.
  • Exit ticket: in one sentence, how does minimax choose a move?
  • Give students a board with three empty squares and have them trace minimax by hand.

Going further

The README lists extensions: counting positions, preferring faster wins, alpha-beta pruning, and minimax for Connect Four or Nim. Some of them change the scores and fail the tests, so the README tells students to work in a copy of main.py.

CSTA standards

Our own mapping. CSTA has not reviewed it.

2017 standards

  • 3B-AP-08Describe how artificial intelligence drives many software and physical systems.
  • 3B-AP-09Implement an artificial intelligence algorithm to play a game against a human opponent or solve a problem.
  • 3B-AP-10Use and adapt classic algorithms to solve computational problems.
  • 3B-AP-11Evaluate algorithms in terms of their efficiency, correctness, and clarity.
  • 3B-AP-13Illustrate the flow of execution of a recursive algorithm.
  • 3B-AP-14Construct solutions to problems using student-created components, such as procedures, modules and/or objects.
  • 3B-AP-21Develop and use a series of test cases to verify that a program performs according to its design specifications.

2026 standards

  • HS-ALG-PS-02Optimize the design of an algorithm using procedural abstraction and control structures.
  • HS-ALG-PS-03Evaluate algorithms for efficiency, correctness, and clarity, using metrics or test cases.
  • HS-ALG-PS-04Describe the differences between deterministic and probabilistic algorithms.
  • HS-ALG-ML-06Justify the selection of a type of AI algorithm to accomplish a task.
  • HS-PRO-PD-12Create a modular program that uses procedures, external libraries, or objects to improve reusability and readability.
  • HS-PRO-RD-17Analyze how a segment of code works, including the role of parameters, return values, and data structures.
  • HS-PRO-TR-19Evaluate a computing technology's alignment with design specifications and responsible design values, including its correctness, effectiveness, and user experience.
  • S2-GMD-DD-03Construct in-game artificial intelligence to control NPC behavior.