Solving the Tower of Hanoi Problem in JS.

Solving the Tower of Hanoi Problem in JS.

The Tower of Hanoi is a puzzle game invented in the 18th century. The objective of the game is to move all the disks from the left-most rod to the right-most rod, following a set of rules.

The rules are as follows:

  • There are three rods, labelled 1, 2, and 3. There are a number of disks of different sizes which can slide onto any rod.

  • A larger disk may not be placed on top of a smaller disk.

  • Only one disk may be moved at a time.

The puzzle starts with the disks in a neat stack in ascending order of size on rod 1, the smallest at the top with subsequent disks stacked on top of each other in order of size.

To solve this we first need to think about how we would go about solving this puzzle by hand. To do this let us consider the following scenarios.

  • We only have one disk

  • We have more than one disk

We have only one disk

If we had only one disk in this puzzle, then all we would have to do is move it from the start rod, 1 to the end rod 3.

We have more than one disk.

This is where things start getting more interesting. So assuming we have 3 disks and we want to move them from the start rod to the end rod.

  • First we recursively move the small disks to the middle rod 2.

  • Second we would move the biggest disk from the start rod, 1, to the end rod 3.

  • Recursively move the other disks from the spare rod to the last rod.

So let us write some code to do this.

First we need to create a helper function that shows us the movement of the disks from one rod to the other.

This would simply look like;

let print_move = (start, end) => {
   console.log(start, '-->', end)
}

As you can see we are just printing out the movement from the start rod to the end rod.

Now let us move to writing the function that solves this challenge.

let hanoi = (number_of_disks, start, end) => {
    if (number_of_disks === 1) {
        print_move(start, end)
    } else {
        let other = 6 - (start + end)
        hanoi(number_of_disks - 1, start, other)
        print_move(start, end)
        hanoi(number_of_disks - 1, other, end)
    }    
}

As you can see we handle our base case by just printing out the way the disk would move from the first disk to the last one.

In the next block of code we see a new variable called other. This variable is used to track the position of the second rod. We do this by

6 - (start + end)

The reasoning here is we have 3 rods, rod1, rod 2 and rod 3. To get the middle rod we first get the total which is

3 + 2 + 1 = 6

Then we subtract the sum of the start and end, i.e

3 + 1

This therefore gives us our middle rod as being

6 - 4 = 2

As defined in our algorithm, we first move all the disks except the last one to the middle rod, print the move and then move to moving the disks in the middle rod to the last one.

Conclusion

Congratulations on making it this far into the article and learning how to solve the Tower of Hanoi problem.