The Towers of Hanoi puzzle has three posts and some number n of disks. Each disk has a hole in the middle, so that it can be put on a post, and the disks are of different sizes.

Initially, the disks are all stacked on the leftmost post, with larger disks closer to the bottom and smaller disks closer to the top. For example, if there are three disks, the puzzle starts out looking like this.

The object is move all of the disks from the leftmost post to the rightmost post. So when the puzzle is solved, it looks like this.

But there are some rules that must be followed.

  1. Only one disk can be held in your hand at a time. The other disks must be on posts.

  2. A disk can only be put down by placing it on one of the posts. For example, you can't put a disk on the table beside the puzzle.

  3. No disk can ever be put on top of a smaller disk. You can only put a disk on top of one that is larger than it.

It is convenient to refer to the posts by names, such as L, M and R, for left, middle and right. Instructions to move three disks from L to R are as follows.

  Move a disk from L to R.
  Move a disk from L to M.
  Move a disk from R to M.
  Move a disk from L to R.
  Move a disk from M to L.
  Move a disk from M to R.
  Move a disk from L to R.

 

Write a procedure Hanoi(n, a, b, c) that takes a nonnegative integer n and three characters a, b and c, which are names of posts. (The names can be any characters that you choose.) It should write instructions to move n disks from post a to post b, where the third post is called c. For example, statement

  Hanoi(1, 'P', 'Q', 'E').
should write instructions to move one disk from post 'P' to post 'Q', where the third post is called 'E'. It writes
  Move a disk from P to Q.
since that is all that is required to move one disk. Statement
  Hanoi(2, 'A', 'B', 'C').
should write instructions to move 2 disks from post 'A' to post 'B' with the third post called 'C'. It writes
  Move a disk from A to C.
  Move a disk from A to B.
  Move a disk from C to B.
 
Statement
  Hanoi(3, 'L', 'R', 'M').
should write instructions to move 3 disks from post 'L' to post 'R', with a third post called 'M'. It writes
  Move a disk from L to R.
  Move a disk from L to M.
  Move a disk from R to M.
  Move a disk from L to R.
  Move a disk from M to L.
  Move a disk from M to R.
  Move a disk from L to R.

 

Write Define ... %Define around the definition of Hanoi. The overall form will be

  Define
    Hanoi(n: Integer, a: Char, b: Char, c: Char). =
      ...
  %Define
where you fill in the body (...). See the hints below.

 

Follow the procedure definition by an Execute part that defines n = 4 and writes instructions to move n disks from post 'L' to post 'R' with the third post called 'M'. Write it so that, if you change the definition of n to say that n = 5, then the program will write instructions to move 5 disks, without any other changes being necessary.

Test your program. Does it work? Try it with smaller values of n, and check that the instructions are correct. (The correct instructions are more obvious for smaller values of n.) When you are confident that it works, submit it.

 

Hints for writing the definition of procedure Hanoi.

  1. If n = 0 then you are asked to write instructions to move no disks. Just do not write anything at all.

  2. If n > 0, then break the problem down into the following steps.

    1. Ask somebody else to move n-1 disks from a to c, with the third post called b. That moves the top n-1 disks out of the way, on the extra post c, exposing the n-th disk on the start post. Just use your Hanoi procedure to do this. Notice that the instructions written by somebody else will follow the rules of the puzzle, so you don't need to worry about that. But what those instructions will accomplish, in the end, if moving the top n-1 disks out of the way just as if they had been picked up as a group and moved.

    2. Write one line saying to move a disk (the n-th one) from a to b. Now the bottom disk is where it is supposed to be.

    3. Finally, ask somebody else to move n-1 disks from c to b, with the third post called a. That moves the top n-1 disks on top of the one on post c.

Note that a, b and c refer to the post names given to Hanoi. They are not literally 'a', 'b' and 'c'.

 

  [Language: Cinnameg  Kind: program]