CSCI 110, Spring 2011
Home | | Course Schedule | | Assignments | | Lecture NotesDue Tuesday, March 29, at the beginning of class.
Program Description
The program will prompt the user for the number of rings to use for the puzzle.
It should then output the sequence of moves that a person should make to solve
the puzzle for that many rings. For example, one move would be written as:
The result of running the program should be a list of instructions that can be followed to solve the puzzle.
Project Specifications
Place all your code in a file named <username>_puzzle.py,
where <username> refers to your own username. For example, Professor Royden's file
would be named croyden_puzzle.py.
Your program should consist of a very short main program, that prompts the user for the number of rings to be used in the puzzle. It will then call a function, named Hanoi( ) which will solve the puzzle for moving from peg "A" to peg "B" using peg "C" as a spare.
The Hanoi() function should take 4 parameters: The number of rings to move and three characters. The first character indicates the label of the source peg (from which you are moving the stack of rings). The second character indicates the destination peg (the peg to which you are moving the stack of rings). The third character indicates the spare peg. Thus, you could move a stack of 3 rings from peg "A" to peg "B" using peg "C" as a spare with the following call to Hanoi():
Hanoi(3, "A", "B", "C");
You do not need any other parameters or local variables for this function. The function should use the recursive strategy described above to solve the Hanoi( ) puzzle for the given number of rings. This function should be fairly short (not more than about 6 or 7 lines of code altogether).
You should make use of the following function to move a ring from one peg to another:
def moveRing(ring, fromPeg, toPeg) print "Move ring", ring, "from peg", fromPeg, "to peg", toPeg + "."
Note: you should put the moveRing() function definition in your program before the definition for the Hanoi( ) function. Otherwise, the Hanoi() function will not recognize it.
Sample Output
Your output should be as follows:
Sample output 1:
How many rings are in the tower? 2 Move ring 1 from peg A to peg C. Move ring 2 from peg A to peg B. Move ring 1 from peg C to peg B.
Sample output 2:
How many rings are in the tower? 4 Move ring 1 from peg A to peg C. Move ring 2 from peg A to peg B. Move ring 1 from peg C to peg B. Move ring 3 from peg A to peg C. Move ring 1 from peg B to peg A. Move ring 2 from peg B to peg C. Move ring 1 from peg A to peg C. Move ring 4 from peg A to peg B. Move ring 1 from peg C to peg B. Move ring 2 from peg C to peg A. Move ring 1 from peg B to peg A. Move ring 3 from peg C to peg B. Move ring 1 from peg A to peg C. Move ring 2 from peg A to peg B. Move ring 1 from peg C to peg B.
To start this problem, create a program called "hero.py" and place it in a file called <username>_hero.py. Copy and paste your code from lab 6, problem 2 into this file. This should have a definition of a Hero class, with the following properties and methods:
Properties:
Methods:
Step 1: Add a property called inventory, that is a list of items carried by the hero. Assign an empty list to this property as its initial value.
Step 2: Add the following methods to the class:
Step 3: Modify the main program to do the following:
Your output should be as follows (user input is underlined):
Please enter the hero's name: Hercules I am the hero, Hercules The following items are in the inventory: sword potion armor Hercules has attacked a troll. Hercules now has 8 health points. The healing potion heals Hercules. Hercules now has 10 health points.
To Submit Your Finished Project:
1. Hand in a hard copy of the files <username>_puzzle.py and
<username>_hero.py. Hand these to your instructor
in class on the project's due date.
3. Hand in a printout of 2 sample runs of the Hanoi program and 1 run of the hero program like those shown above. Hand this to your instructor in class on the project's due date.
4. Print your name at the top of the cover page and staple it to the top of your hard copy and sample runs.
5. In addition to the hard copy mentioned above, email your <username>_puzzle.py and <username>_hero.py files to me at croyden@mathcs.holycross.edu
Get started early and have fun!