The Matchbox Brain
Each box is a pile size. Beads inside vote for the next move.
It starts out totally clueless. Every game it plays makes it a little smarter. Watch real machine learning happen, live, right in front of you.
There's a pile of beads โ you choose how many to start with: 12, 21, or 42. You and the AI take turns removing 1, 2, or 3 beads. Careful โ whoever is forced to take the very last bead LOSES! This game is based on the classic game of Nim โ there's a winning strategy that works every time, with a great explanation in this physical version. Our AI, though, starts out with no idea what that winning strategy is!
The AI doesn't "think" like a normal computer. It has one matchbox for every possible pile size โ a 12-bead game has 11 boxes, a 42-bead game has 41! More beads means more boxes means more to learn, so bigger games take a lot longer to train. Each box is filled with coloured beads:
Red = take 1 Blue = take 2 Green = take 3
On the AI's turn, it opens the box with the same number as how many beads are left in the pile โ so if 7 beads remain, it opens Box 7 โ and pulls out one random bead without looking. Whatever colour it grabs is the move it makes!
If the AI wins, for every box it opened during that round, it adds an extra bead of the colour it drew โ a reward, so it's more likely to make those exact moves again. If the AI loses, it removes the bead it drew from every box it opened during that round โ taking away all the beads used in that losing round, so those moves become a little less likely next time.
Play enough games, and the boxes slowly fill up with the beads that actually win. That's it โ that's the whole "AI"! No code changes, just beads moving in and out of boxes.
See the Generation counter at the top of the page? At Generation 0, the brain starts completely random โ every box has the same beads in it, so it's really just guessing. Every full game adds +1 Generation, and the brain updates a little each time.
Keep playing, and the beads that win pile up while the beads that lose disappear โ no person is reprogramming it, it's teaching itself, one game at a time. Training runs until Generation 250 โ for the smaller games that's usually enough to get really good.
Whether it's enough for the biggest game is an open question, though! Try training all three sizes and check the AI's Track Record chart near the bottom of the page โ this is a real experiment, and the bigger games might not have finished learning by the time training stops.
A matchbox-brained AI learns Misรจre Nim, one game at a time.
Every time a full game finishes, that's one Generation! Win or lose, the AI's matchbox brain learns something from it โ so Generation is really just a counter for how many games have helped train the AI so far.
Training stops at Generation 250 โ by then a smaller brain has usually settled into playing its best move almost every time. After that, you can still play against it for fun โ those games just don't count toward the total.
But bigger games take a lot longer to learn โ a 12-bead brain only has 11 boxes to master, while a 42-bead brain has 41! Whether 250 generations is actually enough for the biggest brain is a genuinely open question โ this is an experiment, so compare all three on the AI's Track Record chart and see for yourself!
Curious what the brain looked like earlier? Drag the Time-Travel slider back to any past Generation and watch the Matchbox Brain change!
This one's for the curious! It's not just that a 42-bead game has more boxes than a 12-bead one โ it's that training time gets disproportionately worse as the game gets longer. Here's the actual mechanism.
A matchbox only changes when the whole game is won or lost โ not when one particular move turns out to be good or bad. A 12-bead game is over in 3-4 moves per player, but a 42-bead game can run 11-14 moves deep. That means an early move's reward or punishment is decided by a long chain of later moves it has no control over. If an early box makes a great choice but a much later box throws the game, the early box still gets penalised โ even though it did nothing wrong.
Because of that noise, the boxes closest to the end of the game settle into good habits first โ there's barely any chain between their move and the result. Once those are reliable, the box before them can finally start learning cleanly too, since the downstream outcome isn't random anymore. Good play effectively propagates backwards through the game, one pile size at a time.
In this exact game (take 1-3, last bead loses), pile sizes 4 beads apart are automatic losses for whoever has to move from them, no matter what they do โ 1, 5, 9, 13, and so on. Those are the checkpoints that have to lock in, roughly in order from the end of the game backwards.
There's no single fixed answer, because every run is random โ how many losses it takes to prune away a bad bead, and how quickly each downstream box settles down, both vary by chance each time you play. But the scaling is something we can reason about: training time should grow closer to the square of the checkpoint count, not the checkpoint count itself, since each checkpoint has to wait for every checkpoint after it to settle down first. That's a real, checkable prediction โ not a certainty, since it's a rough scaling argument rather than an exact derivation โ so compare it against the AI's Track Record chart and see how close it actually gets.
Here's the full mechanism behind those "win โ add a bead" and "lose โ remove a bead" messages โ the two actions, and why repeating them thousands of times actually produces good play.
Every matchbox holds coloured beads, one colour per legal move. On the AI's turn, it grabs one bead at random from the box matching how many beads are left โ whatever colour it grabs is the move it makes.
Win โ add a bead. If the AI wins, every box it opened that round gets an extra bead of the exact colour it drew there. That move becomes a little more likely to be drawn from that box next time.
Lose โ remove a bead. If the AI loses, every box it opened that round loses one bead of the colour it drew (down to a minimum of 0). That move becomes a little less likely next time.
This looks almost too simple โ there's no lookahead, no strategy, no "thinking" at all, just beads being added and removed. But because a move's odds of being drawn are set by what fraction of the beads in its box are its colour, and winning moves get reinforced more often than losing ones (since they tend to lead to more wins), that fraction slowly shifts in favour of whatever colour actually wins.
It even snowballs a little: a colour that wins gets an extra bead, which makes it slightly more likely to be drawn again, which means it gets picked (and can win) even more often, which adds still more beads. Over enough games, the boxes end up dominated by whichever colour genuinely correlates with winning โ not because the AI understood anything, but because that colour simply out-reproduced the others.
No single game proves anything on its own โ a bad move can still win by luck, and a good move can still lose. That's exactly why it takes many games: individual results are noisy, but averaged over dozens or hundreds of them, the moves that truly win more often reliably end up with more beads. That's the same basic idea behind a lot of real machine learning โ trial, error, and letting the noise average out over enough attempts.
Take the last bead and you lose!
This grid shows everything the Matchbox Brain has learned so far โ every box, all in one place. Let's break down what it all means!
Every possible pile size gets its own box. There's no Box 1 though โ if only 1 bead is left, there's no real choice to make, you just have to take it! That's why a 12-bead game needs a box for every size from 2 up to 12, and a 42-bead game needs one for every size from 2 up to 42.
Each box is filled with colourful beads โ red for take 1, blue for take 2, green for take 3. To choose a move, the AI reaches in without looking and grabs one random bead. Whatever colour it grabs, that's the move! A box with lots of red beads is much more likely to grab red โ there's no brain doing maths here, just counting.
Every time the AI loses, it takes away the beads it used โ so a colour can sometimes disappear from a box completely! If every colour in a box ran out at once, it'd have nothing left to grab โ back in 1961, the very first version of this game called that the "resignation" problem. The moment it happens here, the box is instantly given 1 bead of each colour back, so it can always keep playing.
Look at the real Matchbox Brain below โ each box gets a soft colour tint showing which move it likes best right now. The stronger the colour, the more sure that box is!
Train a brain all the way up and see if you can spot something cool: the colours lock into a repeating pattern every 4 boxes. The grey boxes are the tricky "checkpoint" piles (like 5, 9, 13...) where no move is really safe โ read Why Do Bigger Games Take So Much Longer? to find out why they show up every 4 boxes too.
Scientists have special words for all of this โ if you ever hear grown-ups talk about "reinforcement learning", this bead game is exactly what they mean! It isn't just a toy idea, either: a real scientist named Donald Michie built the very first version out of actual matchboxes and beads back in 1961. It's simple, but it's the same basic idea behind some of today's most powerful AI.
Matchbox Educable Noughts and Crosses Engine
MENACE was built in 1961 by AI researcher Donald Michie โ 304 real matchboxes, each holding coloured beads, trained by hand to play Noughts and Crosses (tic-tac-toe) through nothing but trial and error. No computer involved. It's one of the earliest working machine learning systems ever built, decades before this kind of trial-and-error learning had a name.
This app borrows the exact same trick โ matchboxes, coloured beads, win/lose adjusting beads โ and points it at a different game: Misรจre Nim instead of Noughts and Crosses. Same engine, new game. That's the "Nim" in "MENACE Nim".
It'll go straight back to Generation 0 โ every game you've trained it with will be gone, and it'll play completely randomly again until you retrain it.
This can't be undone.
Press "New Game" to begin!
Each box is a pile size. Beads inside vote for the next move.
All three brains at once, so you can see the bigger ones take longer to learn. Each line tracks that brain's win rate over its last 20 games (a rolling average, so it responds to recent play rather than flattening out over time). Each stacked row below is one block per game it's played โ the AI won, the AI lost.