CSCI 110, Spring 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    CSCI 110 Homework 6

    Due Tuesday, March 29, at the beginning of class.

    Problem 1: Tower of Hanoi

    Introduction
    In this problem you will write a program to solve the Tower of Hanoi puzzle for any given number of rings. In this puzzle, you are presented with a stack of rings on a peg, arranged from largest to smallest, with the largest ring on the bottom, and the smallest ring on the top. There are two other empty pegs available. The goal of the puzzle is to move all the rings from the first peg (peg "A") to the second peg (peg "B") making use of the spare peg (peg "C"). You may only move one ring at a time, and you cannot put a ring on top of a ring that is smaller than it is. We will accomplish this with a recursive program. We can describe the solution in English as follows: If the tower is only one disk high, move it from peg "A" to peg "B". Otherwise, first move all the disks except the bottom one from peg "A" to peg "C" (this is just a smaller version of the original problem). Then move the bottom ring from peg "A" to peg "B". Finally, move the smaller tower of disks from peg "C" to peg "B". The goal of this problem is to gain some practice in writing a recursive function.

    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:

      Move ring 4 from peg A to peg C.

    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.
      


    Problem 2: The Hero Class

    In this problem, you will add some more methods to the Hero class that you wrote in lab 6. You will write a short program to create a hero object that uses the methods to manipulate the hero. You will add to the class so that the hero carries an inventory of items that can be listed or added to. The class will also include a method that restores the hero's health if the inventory includes a potion.

    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:

    • health
    • name

    Methods:

    • __init__(self, name)
    • __str__(self)
    • attack_troll(self)

    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:

    • def add_inventory(self, item)
      This should append item to the hero's inventory.
    • def print_inventory(self)
      This should print "The following items are in the inventory."
      It should then print each item (hint: use a for loop)
    • def heal(self):
      This should search the inventory for a "potion".
      If a potion is found, it should print "The healing potion has healed" the hero, and it should set the hero's health to 10.
      It should then print the hero's health, e.g. "Lancelot now has 10 health points."

    Step 3: Modify the main program to do the following:

    • Before attacking the troll, add a "sword", a "potion" and "armor" to the hero's inventory (using add_inventory( ))
    • Immediately after adding the three items, before attacking the troll, print the hero's inventory (using print_inventory( ))
    • After attacking the troll, heal the hero with the heal( ) method.

    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!