Tower of Hanoi

Hanoi requires a browser with JavaScript support (or NewtonScript support, i.e., Newt's Cape on 2.x Newton with i:General:NewtonScript:Compile preference); besides Newt's Cape, it has been tested with

If Images are not loaded automatically, you'll have to Load/Show Images manually after (re)loading page. Let me know about your experience with other browsers.

What is Tower of Hanoi? | Using Hanoi | Example | Versions | Distribution


Moves: Move: Speed:
Disks: Method:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Describe Tower of Hanoi

Hanoi icon

In the classic "Tower of Hanoi" game/puzzle, a stack of disks, sorted by size, appears (on the left). You must move these to another stack (on the right) one at a time.

However, you cannot place a larger disk on top of a smaller one, so you must move disks temporarily to another stack.

Use Hanoi

Moves

based on n Disks, total expected moves until solution: 2n-1

Moves

displays the current number of moves

Speed

Slow
0.5 seconds (500ms) between each jump or move step
Medium
0.1 seconds (100ms)
Fast
0.01 seconds (10ms)

Disks

choose a number (2-14) based upon selected Speed and your patience. It updates "Moves". Note: if you have image loading disabled, you will have to do an explicit Load/Show Images

Start/Stop

Use this to reset stack to left. For jump and move methods, this starts solving; for MSIE, button changes to Stop.

Method

user
(default): move a disk between stacks 0-2 until you solve (or get tired); to move a disk in newer browsers, click on source stack (button at bottom may highlight), then click on destination stack; or use use buttons at bottom. Remember, you cannot place a larger disk on top of a smaller one (unless you "cheat")
jump
program moves disks quickly* between stacks (see "Speed")
move
program moves disks between stacks in more steps
cheat
like user, but you can place any disk on top of another one

Example

Hanoi is a good example of "recursion" (even though the actual underlying algorithm is implemented iteratively for better process control).

With the situation of 3 disks starting on left stack: If you knew how to move the top 2 disks somewhere else, e.g., to the middle stack, [steps 1-3 below], all you would have to do then is move the bottom disk to the right [step 4]; and since you already know how to move 2 disks, just move those from middle to right [steps 5-7].

Here are step-by-step directions for 3 disks (#1 refers to smallest disk) and 3 stacks (left,middle,right):

  1. move disk(#1) from left to right
  2. move disk(#2) from left to middle
  3. move disk(#1) from right to middle
  4. move disk(#3) from left to right
  5. move disk(#1) from middle to left
  6. move disk(#2) from middle to right
  7. move disk(#1) from left to right

You can watch the program do this by setting method to Move and Speed to Slow.

Hanoi Versions

I saw an InterLisp-D version of Tower of Hanoi in late 70s at Xerox PARC. The "classical" version uses a recursive algorithm; the iterative version (which more easily supports Stop) is based on some Scheme code posted by Stancu Radu.

Last updated: 21 Jun 2005: updated syntax via JSLint and tested with newer browsers

Distribution

This version is free, and can be downloaded locally and mirrored in its entirety. If you copy/modify/improve the code, I would appreciate an acknowledgment of this version and link to this page.

© 1999-2007, S. Weyer. All Rights Reserved Worldwide.

Valid HTML 4.01 Transitional