Principles of AI · Unit 1: Inside the Machine ·  Activity 1.1.3

Matchbox Nim — A Machine That Learns by Playing

A learner made of cups and beads, a game with a hidden pattern, and a chatbot you have to test  ·  Approx. 2 class days

StarringPlay · Find the PatternLearn · Matchbox CupsBuild · Test the Bot

Quick reference for Nim, the pattern behind it, the matchbox learner, and testing chatbot-written code.

Matchbox Nim infographic: the rules of Nim, losing piles 1, 3, 7 and 15, a cup and bead learner, reward and penalty, the learner depends on its opponent, and testing chatbot-built code

🧭 Plot Summary

Nim looks like a simple take-away game: two players, a pile of pieces, and whoever takes the last piece loses. But there is a hidden pattern, and a player who knows it never loses from the right starting spot. In this activity you first play until you find it. Then you build a learner out of cups and beads that knows nothing about the pattern, and train it the only way it can be trained: by playing games, rewarding the moves that led to wins, and penalizing the moves that led to losses. That is reinforcement learning.

On Day 2 you ask a chatbot to build a playable Nim page. It will probably look right. Your job is to find out whether it is right, by writing test cases and running them. The chatbot's friendly comments and a few good-looking games don't count as evidence.

What you'll do in this activity

  • Play Nim, a take-away game, until you can spot the pattern that decides who wins.
  • Build a learner out of cups and colored beads that knows nothing about the pattern, then train it by playing games against it.
  • Watch the beads shift toward moves that led to wins, and see what happens when the learner never wins.
  • Write a prompt that gets a chatbot to build a playable Nim page, then improve it over several versions.
  • Write test cases to find out whether the page does what you asked, instead of trusting that it "looks right."
  • Ask how the chatbot could write a game solver without ever playing a game.

Why it matters

Some AI learns from a pile of labeled examples, like your flower classifier in 1.1.2. Other AI learns by acting and getting feedback, the way game-playing systems do. Either way, what comes out of the machine still has to be checked. Writing and running test cases is the habit you'll use in the Classifier & Model Card project (1.1.4) and every time a chatbot hands you code.

✅ Self-Check Before You Play On

Check off each item as you get there. These aren't grades. They're your own signal.