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
91218421416247133ROOT128NEW0one selection · one expansion · one random rollout · one update
selection expansion simulation backprop

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

01

A simulator

Rules that answer two questions: which moves are legal here, and is the game over?

02

A result signal

One number at the end — 1 for a win, 0 for a loss, 0.5 for a draw.

03

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.

255,168
possible tic-tac-toe games
small enough to solve
~10120
chess game-tree complexity
Shannon's estimate
~10170
legal Go positions
more than atoms in the observable universe

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.

Watch the tree grow
One click runs a full iteration. Follow the amber path down, the green node appearing, the random rollout, and the numbers flowing back up.
0iteration
1nodes
0root visits
1Selection
2Expansion
3Simulation
4Backpropagation
root—
0visits
selection path new node backprop path3 most-visited children shown per node

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.

Speed1×
Best move so far: —nodes 1 · iterations 0
the whole algorithm~15 lines
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 root

What 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.

The UCB1 dial
Three levers, three hidden win rates. Every pull, MCTS picks the arm with the highest upper confidence bound. Drag C and watch the personality of the search change.
UCB1 = Qi/Ni + C · √( ln Np / Ni )
exploitation
average reward so far — trust what works
exploration
grows with ln N, shrinks with n — forgive the untried
1.40

Balanced. Enough optimism to keep testing, enough greed to converge.

running average reward (last 10 pulls)0 pulls · —

Levers

A
0 pulls · —
unvisited + ∞UCB ∞
B
0 pulls · —
unvisited + ∞UCB ∞
C
0 pulls · —
unvisited + ∞UCB ∞

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.

Play against the tree
Every AI move runs a fresh MCTS search in your browser. Nothing is precomputed — the only thing it knows is the rulebook.

Your move — you are X.

you 0 · draw 0 · mcts 0

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 MCTSAlphaGo-style
Evaluation functionHand-written, requiredNoneLearned value network
Tree growthUniform width, fixed depthAsymmetric, sample-drivenAsymmetric + policy prior
Can stop earlyNo — must reach the horizonYes — anytimeYes — anytime
ParallelismHard (shared bounds)Easy (leaf or root)Easy
Famous forChess, checkersPuzzles, planning, gamesGo, 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.