gekro
GitHub LinkedIn

Tower of Hanoi

Play Tower of Hanoi with 3 to 8 discs, see the minimum move count, and watch the shortest solution play out step by step from wherever you are.

Moves 0

Minimum 15

Time 0:00

Best -

Pick up the top disc of a peg, then choose where to put it.

Last move No moves yet.

Click or tap a peg to pick up its top disc, then click the peg to drop it on. Click the same peg again to put it back. With the keyboard: left and right arrows move between pegs, Enter or Space picks up and drops, 1 to 3 choose a peg directly, Escape puts a disc back, U undoes. Move every disc to another peg, one at a time, never a larger disc onto a smaller one.

How the solution works

To move a tower of n discs, move the top n - 1 discs to the spare peg, move the largest disc, then move the n - 1 discs back on top of it. That makes the minimum for n discs 2 x (the minimum for n - 1) + 1, which is 2^n - 1: 7 moves for 3 discs, 255 for 8. Auto-solve uses the same idea from wherever you are: it works from the largest disc down, skips any disc that is already where it needs to be, and plays the shorter of the plans for the two other pegs. Auto-solved games are not counted in your best results. Undo takes a disc back, but the move still counts in your total.

As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.

© 2026 Rohit Burani · MIT · Built at gekro.com · View source ↗

Guide

How to play

Three pegs, and a stack of discs on the first one with the largest at the bottom. Move the whole stack to another peg, one disc at a time, and never put a larger disc on a smaller one.

  • Mouse or touch: click a peg to pick up its top disc (it lifts), then click the peg to drop it on. Click the same peg again to put the disc back. Illegal moves show a short message instead of doing anything.
  • Keyboard: left and right arrows move between pegs, Enter or Space picks up and drops, 1, 2 and 3 choose a peg directly, Escape puts a disc back, U undoes.
  • Screen readers: each peg is a button that names its contents from bottom to top, for example “Peg 2: discs 3 and 1, top is 1”, and a status line says which disc moved and between which pegs.

Every disc has its own colour as well as its number, from the smallest to the largest: rose, orange, amber, lime, green, teal, sky and violet. The colour is only decoration, because the number printed on each disc says which one it is. The three pegs have a soft tint of their own (sky, violet and amber) so they are easy to tell apart. A peg you can legally drop onto gets a dashed, glowing edge, which is a shape cue as well as a colour. When you finish, the discs hop one after another and the result card turns green. With reduced motion switched on in your system settings the hop is skipped.

Beside your move count the page shows the minimum possible for that many discs. When you finish it says Perfect if you matched it, or how many moves over the minimum you were. Undo takes a disc back, but the move still counts in your total, so the number stays honest. The timer starts at your first move and pauses when the tab is hidden.

The minimum is 2^n - 1

To move a tower of n discs you have to move the n - 1 discs above the largest one out of the way, move the largest, then move those n - 1 discs back on top. So the minimum for n discs is twice the minimum for n - 1, plus one. That gives 2^n - 1: 7 moves for 3 discs, 15 for 4, 31 for 5, and 255 for 8. Each extra disc doubles the work.

The tests check this instead of assuming it:

  • The recursive solution has exactly 2^n - 1 moves and every move is legal, for every disc count from 1 to 12.
  • A breadth-first search over every position finds no shorter path for 1 to 8 discs. There are exactly 3^n reachable positions, because each disc can sit on any of the three pegs and the order within a peg is then forced. For 8 discs that is 6,561 positions, and the farthest any of them is from the start is also 255 moves.

Patterns in the solution

Two regularities fall out of the shortest solution, and they are tested as described below:

  • Which disc moves at step k: write k in binary and count the zeros at the end. The disc labelled d moves at step k exactly when k has d - 1 trailing zeros. So the smallest disc moves on every odd step. A disc labelled d moves 2^(n - d) times in total, which means the smallest does half the work and the largest moves once.
  • The smallest disc always travels the same way round. From peg 1 to peg 3, an odd number of discs sends it 1, 3, 2, 1, and an even number sends it 1, 2, 3, 1. The same disc never moves twice in a row (tested at 8 discs).

Auto-solve

Auto-solve plays the shortest solution one move at a time with a short delay (a minimal one if your device asks for reduced motion). Stop hands control back to you at any point. It works from where you are, not only from the start: it takes the largest disc, and if it is already in place it ignores it, otherwise it clears everything smaller onto the spare peg, moves it, and rebuilds. It plays the shorter of the plans for the two other pegs. For 3 to 6 discs the tests compare this against exhaustive search from every one of the possible positions and the lengths match.

Games that used Auto-solve are marked as such and do not count toward your best results.

Privacy and storage

Nothing is uploaded and there is no account. Your best move count and best time for each disc count are saved in your browser’s local storage as small whole numbers inside a small JSON object, under one key, and only for games you solved yourself. Clearing site data removes them. The page address holds the number of discs, so you can link to a particular size.

Limits

  • The game offers 3 to 8 discs. The engine is tested up to 12, but 12 discs is 4,095 moves and a poor fit for a phone screen.
  • Best moves can only ever match the minimum, so the useful record is the best time.
  • The timer counts whole seconds and does not run during or after Auto-solve.
  • It plays the standard three-peg puzzle only. Variants with four pegs or restricted moves are not included.

Everything runs locally in your browser.

All Fun tools →

For informational purposes only. Not financial, medical, or legal advice. You are solely responsible for how you use these tools.