Tower of Hanoi — free online puzzle game
2-10 min
Tower of Hanoi

Tower of Hanoi

Puzzle

Move a tower of discs from peg A to peg C, one disc at a time, never a bigger disc onto a smaller one — with a live count of how far you are from the shortest possible solution.

Score 0 Best 0

Home / Puzzle / Tower of Hanoi

Tower of Hanoi is the classic three-peg disc puzzle, free to play online with no download and no sign-up. Lift the top disc of a peg and set it on another, as long as it is not landing on a disc smaller than itself, until the whole tower stands on peg C. It sounds like nothing and it is famously hard to do well: the shortest possible solution doubles with every disc you add, so three discs take 7 moves, five take 31, eight take 255, and the legendary temple tower of 64 discs would take 18,446,744,073,709,551,615. Play any size from 3 discs to 8, drag a disc across or tap peg-to-peg, undo as far back as you like, and watch the counter tell you the moment you step off the shortest path. A built-in solver will play the fewest-moves line from wherever you have got to, at three speeds or one move at a time, so you can see the recursion rather than read about it.

How to play Tower of Hanoi

  1. Three pegs stand on the bench, labelled A, B and C. The discs start stacked on peg A, biggest at the bottom, and your job is to rebuild that same tower on peg C.
  2. Tap a peg to lift the disc sitting on top of it, then tap another peg to set it down. You can also drag a disc straight across. Tap the lifted peg again to put the disc back.
  3. Only one disc moves at a time, and only the top disc of a peg can be moved. Any peg may be used as a staging post — the middle one usually is.
  4. The single rule: a disc may never land on a disc smaller than itself. A disc may always go onto an empty peg or onto any larger disc.
  5. The header shows your move count beside the fewest moves the position can possibly be done in. It also shows how far off that shortest path you have strayed — a wrong move adds two, because you have to make it and then undo the damage.
  6. The fewest moves is always 2 to the power of the number of discs, minus one: 7 moves for 3 discs, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8. Finish in exactly that many for three stars; finish within half as many again for two.
  7. Undo takes back as many moves as you like, Hint shows the next move on a shortest line, and Show solution plays that whole line for you — Slow, Normal or Fast, or one move at a time with Step. Scramble mode starts the discs spread across all three pegs instead, with its own fewest-moves target.

Tips

  • Think about the biggest disc first. It only ever needs to move once, straight to peg C, and it can only do that when every smaller disc is parked together on the spare peg.
  • That turns the whole puzzle into a smaller copy of itself: to move n discs to C, move n-1 of them to B, move the big one to C, then move those n-1 across onto it. Say that out loud while you play and the moves stop feeling arbitrary.
  • There is a rhythm you can follow without thinking: the smallest disc moves on every other move, always in the same direction round the three pegs, and on the moves in between there is only ever one legal move that is not the smallest disc going straight back.
  • Which direction the smallest disc should travel depends on whether the disc count is odd or even. With an odd number of discs it cycles A to C to B; with an even number, A to B to C.
  • Never move the same disc twice in a row. Doing so always wastes the first of the two moves, and it is the single most common way a run slips off the shortest path.
  • Build up from a size you can do perfectly. Three and four discs take minutes to master; once 7 moves and 15 moves are automatic, 31 and 63 are the same pattern twice more.
  • Stuck halfway? Press Hint rather than restarting. The solver works out the shortest line from the exact position in front of you, not from the opening, so a messy board is still worth finishing.

FAQ

Is Tower of Hanoi free to play?

Yes. Every disc count from 3 to 8 is free to play in your browser, with no download, no account and nothing to buy. Your best score for each disc count is kept in your own browser.

Does Tower of Hanoi work on a phone?

Yes. The pegs respond to touch exactly as they do to a mouse: tap a peg to lift its top disc and tap another to drop it, or drag the disc across with one finger. The bench is drawn to fit an upright phone, so there is no need to turn it sideways.

What is the minimum number of moves in Tower of Hanoi?

For n discs it is 2 to the power of n, minus one. That gives 7 moves for 3 discs, 15 for 4, 31 for 5, 63 for 6, 127 for 7 and 255 for 8 — the target doubles and adds one every time you add a disc. The 64-disc tower of the legend would need 18,446,744,073,709,551,615 moves, which at one move a second is about 585 billion years.

How do you solve the Tower of Hanoi?

Recursively. To move n discs onto the goal peg, first move the top n-1 discs onto the spare peg, then move the biggest disc onto the goal, then move those n-1 discs from the spare peg onto it. Each of those two sub-jobs is the same puzzle one disc smaller, which is why the move count satisfies T(n) = 2 x T(n-1) + 1 and comes out at 2 to the power of n, minus one.

Is there a Tower of Hanoi solver on this page?

Yes, and it works from any position, not just the opening. Show solution plays a shortest line move by move at Slow, Normal or Fast, and Step plays exactly one move so you can follow the pattern. Hint shows only the next move. Using the solver finishes the puzzle but does not set a personal best, so your records stay yours.

How do you win, and how are the stars worked out?

You win when every disc is stacked on peg C in size order. Three stars are for finishing in the fewest moves possible from the position you started on; two stars for finishing within half as many again — 11 moves at 3 discs, 47 at 5, 383 at 8; one star for finishing at all. The cut-offs are worked out from the puzzle rather than picked by hand.

What is Scramble mode?

Instead of a neat tower on peg A, the discs start spread across all three pegs in a random but legal position, and the target becomes the fewest moves from there. Every legal arrangement of the discs can be finished — there is no dead end in this puzzle, only a long way round — so a scramble is always solvable, and the hint knows the shortest route out of it.

How is this different from ball sort or water sort puzzles?

Those are colour-matching games: you pour like onto like across a dozen tubes and a tube is finished when it holds one colour. Tower of Hanoi has three pegs, every disc is a different size, colour means nothing, and the whole point is the move count — the puzzle has one shortest solution length and this page measures you against it move by move.

Who invented the Tower of Hanoi?

The French mathematician Edouard Lucas, who put it on sale in 1883 under the pen name "N. Claus de Siam". The story of a temple where monks move 64 golden discs and the world ends when they finish came with the puzzle as part of its own marketing — the puzzle is real, the temple is not.