Puzzle · Logic

Tower of Hanoi

Move the entire stack of disks to another peg — the classic recursive puzzle.

Overview

Never place a larger disk on a smaller one. That single rule is the whole of Tower of Hanoi, and from it unfolds one of the cleanest recursive puzzles in the canon. Three pegs stand in a row; one holds a stack of disks sorted largest-at-bottom, and your job is to move the entire stack to another peg using the smallest number of moves. The classic mathematical result is that the optimal count for n disks is 2 to the nth power minus 1 — three disks take seven moves, eight disks take two hundred and fifty-five. This build lets you choose anywhere from three to eight disks, so you can step from the gentle three-disk tutorial up to a stack that punishes a single misordered move. The optimal-move counter sits on screen so you always know the target, a timer tracks how long the run is taking, and rule validation refuses any illegal placement outright. An auto-solve mode demonstrates the recursive solution step by step, which is the cleanest way to see why the algorithm works.

How to Play

Click a disk to lift it (the top disk of whichever peg you click), then click the destination peg to place it; on touch, tap the disk and then tap the target peg. Rule validation blocks any move that would set a larger disk onto a smaller one, so an illegal drop simply does not land. Choose between three and eight disks before you start. The optimal-move counter shows the 2-to-the-n-minus-1 target for your chosen disk count, and the timer runs until the stack sits complete on a different peg than it started. If you want to watch the recursive solution play out, the auto-solve button steps through the optimal sequence one move at a time.

Tips & Strategy

Think recursively. To move n disks from peg A to peg C, you first move the top n−1 disks to peg B, move the largest disk to peg C, then move the n−1 disks from B onto C. The smallest disk therefore moves on every other turn — every odd-numbered move in the optimal sequence — which gives you a rhythm to follow: small, other, small, other, small. For three disks that pattern is short enough to memorize; for eight it is two hundred and fifty-five moves and any error resets the count. If you lose your place, undo mentally to the last time the largest disk moved, because the largest disk moves exactly once in an optimal solution. Watch the optimal counter as you go — if your move count drifts above the target, you have already lost the minimum and should restart rather than patch. Start with three disks to internalize the pattern, then add one disk at a time rather than jumping straight to eight.

Controls

Keyboard
Click disk to lift, click peg to place
Mouse
Click disk then destination peg
Touch
Tap disk then tap destination peg

Features

  • 3–8 disks selectable
  • CSS animated disk moves
  • Optimal move counter (2^n−1)
  • Timer
  • Auto-solve step-by-step
  • Rule validation