Experiment 01 · algorithms
Game Trainer
Tic-Tac-Toe, Connect Four and Gomoku against a computer that shows its work. Most game AIs just answer with a move. This one colours every square by how good it is, ranks the moves, draws its search tree and explains why, with two very different algorithms to compare.
Play it
The real Game Trainer, right here
This is the app itself, not a recording. You play X, the computer plays O, and the trainer on the right explains every position. Switch game at the top, and switch engine under Setup.
Two ways to think
Same board, two very different engines
Think through every reply
Try every move, then every answer to it, and so on. Assume both sides play their best, and pass the results back up. Exact, but the tree grows very fast.
Proof
Tic-Tac-Toe is solved outright: 71,742 positions from the empty board, and every first move is a draw with perfect play.
Play lots of quick games
Finish the game at random, thousands of times, and count what wins. Spend more of the effort on moves that look promising, but keep checking the others.
Big boards
It needs no hand-written idea of a good position, only the rules. The default for Connect Four is 20,000 simulations per move.
Too many moves on 15×15
Gomoku has 225 squares. Looking at all of them, several moves deep, is out of reach for either engine.
Only look near the stones
Candidates are empty squares near the action, ranked by a quick attack and defence score: the best 20 at the top of the tree and 8 deeper down.
How minimax works
Step through a search
X to move, three empty squares. One move wins, one draws and one loses, but only if you look ahead. Step through the search and watch the scores travel up the tree: +1 means X wins, 0 a draw, −1 O wins. X picks the highest score, O the lowest.
How Monte Carlo Tree Search works
Four steps, thousands of times
Each simulation walks down the tree by the best UCB score, adds one new move, plays the rest of the game at random, and sends the result back up. UCB is the win rate so far plus a bonus for moves that have been tried less. Run a few and watch the effort shift.
- 1 · Selection–
- 2 · Expansion–
- 3 · Simulation–
- 4 · Backpropagation–
By the numbers
One engine, three games
Experiment 02 · simulation
Infinite Monkeys
Given enough monkeys, typewriters and time, will one of them type Shakespeare? Most simulators wait for one famous line and never see it. Ours checks every keystroke against the complete works, so you watch real passages appear, see which play they come from, and compare the luck with the maths.
The real app
The Infinite Monkey Simulator
Screenshots from the running app with 1,000 monkeys on the classic 27-key typewriter. They follow the page's light or dark setting, like the app itself.
Try it
A room full of monkeys, in this page
The same idea, running in your browser. Each monkey presses one of 27 keys at random, once a second of monkey time. After every key we look up the longest tail of its typing in Shakespeare's 154 sonnets. The real app does this against all 5.0 million characters of the complete works.
Monkeys
8.1 billion is one for every person on Earth.
Time acceleration
One real second is one minute of monkey time.
The sonnets load when you press Start.
Longest passage typed so far
How long would it take?
The gap
Three things monkey demos usually get wrong
Waits for one line
It picks a famous phrase and waits for it. Even "to be or not to be" takes about 5.8 × 1025 keystrokes, so nothing ever happens.
Searches all of Shakespeare
After every key, the longest tail of the monkey's typing is looked up in all 5.0 million characters. With 1,000 monkeys a nine-letter passage is expected within about an hour of monkey time, with its play and line.
Time is whatever the loop does
"Ten million tries" says nothing about how long monkeys would actually need.
One key per second per monkey
K keystrokes by M monkeys take K / M seconds. Time acceleration sets how fast the monkey clock runs against yours, from real time to as fast as the CPU goes.
A number, no proof
You get a count, but not whether it is what probability predicts.
Luck checked against theory
With D(L) distinct passages of length L, each key has a D(L) / 27L chance of finishing one. A chart puts that next to what the monkeys really typed.
How it works
What happens on every keystroke
Press a random key
A fast SplitMix64 generator per monkey picks one of the keys. The text is folded to the same keys: lower case, straight quotes, single spaces.
Try to grow the match
The whole text sits in a suffix array, so a match is a range of sorted positions. The new key narrows that range with a binary search: ha → ham.
Or drop from the front
If nothing continues, the run is over. Characters come off the front until the tail occurs again, so hamq falls back to q.
Keep the record
A new longest run is stored with the monkey, the keystroke, the monkey time, and the play and line where Shakespeare wrote it.
By the numbers
Big numbers, honestly counted
Experiment 03 · web app
Piano Timing Coach
Piano learning apps usually need a MIDI keyboard, or only check that you pressed the right key. This one listens to a real piano through the microphone and grades every note on timing, to the millisecond, in an ordinary browser tab.
The real app
Practising Twinkle Twinkle
Screenshots of the running app, played from the computer keyboard so we could time a few mistakes on purpose.
The gap
Why a microphone is the hard way
Hear one note at a time
A piano keeps ringing. When you play the next key, the old notes are still there, and a tuner-style detector often reports the wrong one.
Listen for what changed
Right after the hammer strikes, the sound is compared with the sound just before. The harmonics that rose belong to the new key.
The metronome counts as a note
The click leaks from the speakers into the microphone, and knocks on the piano sound like playing.
Clicks are thrown out
A note only counts if its own harmonics gained energy and it is clearly tonal. A click raises everything evenly, so it is rejected.
Latency makes you late
Speakers, microphone and browser all add delay, so playing exactly on the beat gets graded late.
One clock, calibrated
Notes, metronome and screen share the time the sound actually reaches you. A test through the speakers, or tapping along on headphones, measures the delay.
How it works
Before and after the hammer
A simplified spectrum of what the microphone hears. The pale bars are the moment before an onset, the solid bars just after it. Pick what happens next.
By the numbers