Date assigned: 2/11/03
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.
Only one disk can be held in your hand at a time. The other disks must be on posts.
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.
No disk can ever be put on top of a smaller disk.
Write a program that prints instructions for solving the Towers of Hanoi puzzle. It should read four things from the user: (1) a number n of disks, (2) a source post character (3) a destination post character, and (4) an intermediate post character. Your program should print instructions for moving n disks from the source post to the destination post, using the intermediate post as a place to put disks during the move.
Number the moves, starting at move 1. For example, suppose the number of disks n is 3, the source post is called L, the destination post is called R and the intermediate post is called R. Then the following would be printed.
1. Move a disk from post L to post R. 2. Move a disk from post L to post M. 3. Move a disk from post R to post M. 4. Move a disk from post L to post R. 5. Move a disk from post M to post L. 6. Move a disk from post M to post R. 7. Move a disk from post L to post R.The disk to be moved is always the top one on the post. The instructions do not need to say which disk that will be.
Use recursion. We wrote a function similar to what you need in class, but yours will need to do a little more. Write a function that takes five parameters:
12. Move a disk from post M to post R. 13. Move a disk from post M to post L. 14. Move a disk from post R to post L. 15. Move a disk from post M to post R. 16. Move a disk from post L to post M. 17. Move a disk from post L to post R. 18. Move a disk from post M to post R.where the numbering has started from 12. It would return 7, since a total of 7 moves were made.
It is easy to move no disks. To move n disks from post A to post B, using post C as the other post, first ask a friend to move n-1 disks from post A to post C. Then move the largest disk from post A to post B. Finally, ask another friend to move n-1 disks from post C to post B.
Test your solution, and check whether the instructions that it prints work. When it is working, print it and turn it in. Be sure that your program is well written and well commented. Write the contract for your function into your file before you write the function body.
Do not use any global variables in your program. Rely on passing parameters.