Interactive explainer
Monte Carlo
Tree Search
How a machine decides what to do next when the game tree is far too big to search — by playing random games, keeping score, and letting the best ideas rise to the top.
- Phases
- 4
- Formula
- UCB1
- Oracle
- none
01 — The idea
Stop scoring positions. Start sampling them.
Classical search needs an oracle that can look at a board and say how good it is. Monte Carlo Tree Search doesn't have one — and doesn't need one.
Minimax with alpha-beta pruning walks the tree from the root down and scores every position it reaches with a hand-written evaluation function: a pawn is worth 1, a rook 5, control of the centre 0.3. That works for chess, where decades of human knowledge are baked into those numbers. It does not work for Go, where nobody has ever written a good evaluation function by hand.
MCTS replaces the evaluation with sampling. From a position, play a random game all the way to the end. The result — win, loss or draw — is one sample of that position's value. One sample is noise. A thousand samples are a decent estimate, and the tree remembers them so future samples concentrate where they matter.
The search is therefore a loop with no horizon and no oracle: the tree grows one node at a time, and every node carries the two numbers that drive everything — how many times it was visited and how well those visits went.
Everything it needs
A simulator
Rules that answer two questions: which moves are legal here, and is the game over?
A result signal
One number at the end — 1 for a win, 0 for a loss, 0.5 for a draw.
A budget
How many iterations you can afford. More is better; any amount is usable.
The flip
Don't evaluate positions. Simulate them.
The evaluation function is replaced by an average over random games. The fixed-depth search is replaced by a tree that remembers which lines have paid off. Nothing about the position is ever scored by hand.
02 — The loop
Four moves, repeated until the clock runs out
Every iteration is the same four steps. Selection walks down the tree following UCB1. Expansion attaches one new leaf. Simulation plays a random game to the end. Backpropagation writes the result into every node on the path.
Ready when you are.
The tree is a single root with nine unexpanded moves. Press Step to run the first iteration, or Auto to let it run.
function mcts(root, budget):
repeat budget times:
node ← root
# 1. selection
while node is fully expanded and not terminal:
node ← child with the highest UCB1
# 2. expansion
if node has untried moves:
node ← expand one untried move at random
# 3. simulation
reward ← random playout from node
# 4. backpropagation
while node exists:
node.visits += 1
node.wins += reward for the player
who just moved here
node ← node.parent
return the most-visited child of rootWhat to notice
- Visits concentrate on a handful of moves. The tree becomes lopsided within twenty iterations — that asymmetry is the whole point.
- A node's win rate is always stored for the player who just moved, so a parent can compare its children on equal footing.
- Press Run 10 a few times and watch the best move stabilise. More iterations, same loop, better answer.
Reading a node
X→5 is the move that led here. The big number is its visit count. The bar is its win rate for the player who made that move.
03 — The formula
Every node is a slot machine
A tree search is a chain of bandit problems: at each node, the agent must decide which child to try. UCB1 answers with studied optimism — assume the under-sampled child might be great, until the evidence says otherwise.
Balanced. Enough optimism to keep testing, enough greed to converge.
Levers
Unvisited arms get an infinite bonus — the first three pulls are free exploration.
04 — The proof
Now play against it
The same loop, pointed at you. Each of its moves is a fresh search from the current board — no opening book, no evaluation function, no tablebase.
Your move — you are X.
800 iterations per move · solid
What the search saw
Play a move and the search statistics will appear here.
Why it is strong here
Tic-tac-toe is small enough that 800 iterations cover the important lines. Against a random player, plain MCTS wins almost every game — the tree simply samples enough endings to notice which moves lead to trouble.
05 — The point
Why it works, and where it breaks
MCTS is not a better minimax. It is a different bargain: give up the perfect evaluation and get a search that fits any simulator, stops whenever you ask, and improves with every iteration.
| Minimax + α-β | Plain MCTS | AlphaGo-style | |
|---|---|---|---|
| Evaluation function | Hand-written, required | None | Learned value network |
| Tree growth | Uniform width, fixed depth | Asymmetric, sample-driven | Asymmetric + policy prior |
| Can stop early | No — must reach the horizon | Yes — anytime | Yes — anytime |
| Parallelism | Hard (shared bounds) | Easy (leaf or root) | Easy |
| Famous for | Chess, checkers | Puzzles, planning, games | Go, chess, shogi |
Asymmetric growth
UCB1 sends visits where the evidence is good. The tree deepens along promising lines and quietly abandons hopeless ones — without ever visiting them.
Anytime
There is no horizon to reach. Stop at any moment and the most-visited child is a usable move. Double the budget and the answer usually improves.
No oracle
The only knowledge required is the rulebook plus a terminal reward. A random playout is an unbiased, if noisy, estimate of value.
Sharp tactics
Random rollouts can walk straight past a forced mate, so a shallow tree can look calm right before disaster.
Budget
At 100 iterations the tree is a rough sketch. Quality scales with compute — there is no free lunch.
Go
Random play in Go is so weak that plain MCTS plateaued. AlphaGo kept the loop and added policy and value networks to guide it.