MONT 105S, Spring 2009

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

MONT 105S Homework 6

Troll Battle!

Due Wednesday, February 11, 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 you can play online. 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.

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:

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:

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 6 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.