CSCI 110, Spring 2011

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

    CSCI 110 Homework 3

    Troll Battle!

    Due Tuesday, February 22, at the beginning of class.

    Background: Text based games.
    Before computers were fast enough to show seamless animated graphics, several text-based games were developed. The most famous of these were "Adventure" (the first one) and "Zork". You can download a version of Zork to try out at info.com or play it directly at http://thcnet.net/zork. In these games, the scenes are described in prose, rather than pictures and images, and the player must tell the computer what to do, e.g. "go west", "light torch", "hit dragon", etc. The game then describes what happens. Over the course of the game, according to its own description, "the intrepid explorer delves into the forgotten secrets of a lost labyrinth deep in the bowels of the earth, searching for vast treasures long hidden from prying eyes, treasures guarded by fearsome monsters and diabolical traps!"

    Program description:
    In this assignment you will write a very small piece of a text based game. In it, the player finds him- or herself facing a large troll. The player can choose to attack the troll or run away. If the player attacks, the attack may damage the troll by varying amounts (chosen at random by the computer), or it may miss, leaving the troll free to attack the player and end the game.

    Part a. (15 points)
    There are a few tricky pieces to this program so you will write it in stages. For this stage, you will write the bulk of the program, including a simplified version of the while loop.

    In this program, which you should name <username>_battle1.py, the loop will only depend on whether the user answers yes or no to the question of whether they want to attack. Only the troll will lose health points. The user will be able to continue attacking the troll as long as he or she wants, even when the troll's health points go below zero.

    The following are sample outputs for this program. User input is underlined.

    Sample output 1:

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss, allowing the troll to attack you.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 5
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 0
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is -1
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is -4
    Do you want to attack? no
    You have defeated the troll!
    You may continue your quest.
    

    Sample output 2:

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss, allowing the troll to attack you.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 9
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 7
    Do you want to attack? no
    You ran away.  You live to fight another day!
    

    Writing the program
    To write the program, you will need to write the following pieces, in order.

    • Write your program prologue with your name, etc.
    • Import the random library, as discussed in class (import random)
    • Use print statements to write the six descriptive lines at the beginning.
    • Set up two variables to keep track of the user's health (you will need this in parts b and c) and the troll's health. Set both these variables to a value of 10.
    • Ask the user if they want to attack, and record the response in a variable
    • Write a while loop that will repeat as long as the user's response is "yes"
    • In the body of the while loop, your program should do the following:
      • Choose a random number between 1 and 5, inclusive, and store it in a variable. (Use random.randrange(low, high) as discussed in class)
      • print a message saying that the attack wounded the troll
      • subtract the random number you just chose from the troll's health.
      • Report the user's health and the troll's health (use the two variables you set up).
      • Ask again if the user wants to attack, and store the response in the appropriate variable.
    • After the while loop exits, if the troll's health is less than or equal to zero, the program should print a message that the troll has been defeated.
    • If the troll's health is greater than zero, the program should print the message about running away.
    The output of your program should match the sample outputs above as closely as possible, with the exception of the random numbers, which will differ for each run of the program.

    Save this program as <username>_battle1.py.

    Part b. (2 points)
    It is rather unsportsmanlike to allow the user to keep attacking the troll even after he is dead (health points are zero or less). In this part of the assignment you will modify the program from part a that will exit the loop once the troll's health points reach zero or less.

    To modify your program, do the following:

    • Copy the code from your program in part a into a file named <username>_battle2.py
    • Modify the condition in your while loop so that it will continue as long as the user's response is "yes" and the troll's health points are greater than zero. (You will need to use the logical operator, and, to combine the two conditions.) In other words, the loop will end when the user says "no" or when the troll's health points are zero or less.
    • Modify the body of the loop so that it only asks if the user wants to attack again if the troll's health is greater than zero. (Hint: use a conditional statement within the loop)

    A sample output is given below.

    Sample output 3 (Compare this to Sample output 1 above).

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss, allowing the troll to attack you.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 8
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 3
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is -2
    You have defeated the troll!
    You may continue your quest.
    

    Save your code for this part in a file named <username>_battle2.py

    Part c. (3 points)
    This game is really no fun if there is no risk to the player when he or she attacks the troll. In this part of the program, you will modify the program from part b so that the user may miss when he or she attacks, which will lead to a devastating attack by the troll, and an end to the game.

    To modify the program, do the following:

    • Copy the code from your program in part b into a file named <username>_battle3.py
    • Modify the condition in your while loop so that it will continue as long as the user's response is "yes" and the troll's health points are greater than zero and the user's health points are greater than zero.
    • Modify the random number generator so that it chooses a random number between 0 and 5, inclusive (instead of 1 and 5 as in parts a and b).
    • If the random number chosen is zero, set the user's health points to zero and report that the attack missed and the troll attacks. Otherwise, subtract the random number from the troll's health points as before.
    • In the body of the while loop, the program should only ask if the user wants to attack if the troll's health points are greater than zero and the user's health points are greater than zero. (Again, you will need to use the logical operator, and.)
    • After exiting the loop, the program should test to see if the user's health points are zero, and if so, write the appropriate "game over" message. Otherwise, if the troll's health points are zero, it should write that the troll was defeated. Otherwise, it should write that the user ran away.

    Sample outputs of the final version of the program are given below.

    Sample output 4:

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss and lose health.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 6
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 2
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is -3
    You have defeated the troll!
    You may continue your quest.
    

    Sample output 5:

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss and lose health.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 7
    Do you want to attack? yes
    Your attack missed.  The troll attacks you.
    Your health level is 0
    The troll's health level is 7
    The troll delivered a devastating attack with his battle-axe.
    Game over!
    

    Sample output 6:

    You find yourself in a dark cave, facing a large troll.
    Your health level is 10.
    The troll's health level is 10.
    You may attack the troll or run away.
    If you attack, you might miss and lose health.
    If you run away, you stay healthy, but you will not complete the mission.
    Do you want to attack? yes
    Your attack succeeded in wounding the troll.
    Your health level is 10
    The troll's health level is 6
    Do you want to attack? no
    You ran away.  You live to fight another day!
    

    What to turn in:

    1. A copy of the homework 3 cover sheet stapled to the front of the following items:
    2. A listing of each of your 3 programs. Be sure to properly indent and comment them.
    3. A printout of three runs of your program from part c showing each of the possible outcomes (user loses, troll loses, or user runs away). You may have to play several times to get the condition where the user loses. If you copy and paste the runs you want to print into a Word document, you will avoid printing excess runs of the program.
    4. In addition, please email a copy of your program files, named <username>_battle1.py, <username>_battle2.py and <username>_battle3.py as attachments to me at croyden.mathcs.holycross.edu.