The Introduction to Computer Science I for non-majors by James Tam Return to the course web page

CPSC 217: Assignment 5

New Concepts to be applied for the assignment

  • Lists
  • File input and output
  • Common debugging mechanisms

Other technical concepts (such as functions) must be employed. Also style requirements (such as good naming conventions and avoiding the user of global variables) must also be followed.

 

 
Figure 1: The reluctant participant in the quest, Bilbo Baggins.

Introduction

Prior to writing his famous trilogy: The Lord of the Rings, Professor John Robert Reuel Tolkien had already published "The Hobbit"1 a tale of the adventure of Bilbo Baggins and the Company of thirteen dwarves. (JT's comment: This is an exert from the second edition of the book that could describe the plot far better than I ever could):

"The Hobbit is a tale of high adventure, undertaken by a company of dwarves, in search of dragon-guarded gold. A reluctant partner in this perilous quest is Bilbo Baggins, a comfort-loving, unambitious hobbit, who surprises even himself by his resourcefulness and his skill as a burglar.

Encounters with trolls, goblins, dwarves, elves and giant spiders, conversations with the dragon, Smaug the Magnificent, and a rather unwilling presence at the Battle of the Five Armies are some of the adventures that befall Bilbo. But there are lighter moments as well: good fellowship, welcome meals and song." - J.R.R. Tolken "The Hobbit"

For this assignment you are to write a text based game with an overhead view of the dwarven kingdom: Erebor (see Figure 2). This game re-creates the portion of Bilbo's quest just after the secret entrance tunnel to Erebor has been discovered and a 'volunteer' is needed to go in. The full version of the game will allow Bilbo to sneak into the Erebor in order to retrieve as much of the treasure as possible. The treasure is guarded by the dragon "Smaug the Golden". Although Smaug himself doesn't directly appear in the game the effects of his dragon fire do. At the start of the game Bilbo starts off at the entrance to the dwarven hall: Coordinates (0,E). Smaug will be asleep so Bilbo can freely move about (the game is in "mode 1"). Once Bilbo is in the main hall Smaug will awaken and begin bombarding the hall with fireballs (the game now enters "mode 2"). Bilbo, like all Hobbits, is a stout fellow but even he cannot withstand dragon fire for long so it will be a tradeoff between collecting as much treasure as possible and getting out of Erebor alive. Once Bilbo has reached the entrance tunnel, Smaug knows that the would be thief has almost escaped so he will stop breathing fire into the main hall but will instead focus on the tunnel. By the time that Bilbo starts moving back up the tunnel's exit, Smaug will have positioned himself at the entrance to the tunnel and start breathing fire into it (the game now enters "mode 3"). At this point it's a race against time for Bilbo (actually a race against the flames) as he tries to reach the entrance before he gets caught by the fire. If Bilbo's hit points reaches zero then he has been slain, the player has lost and the game ends. If however Bilbo returns to the entrance/exit then the game ends and the player's success is determined by how much treasure has been gathered.

The world will consist of a 2D list with each element a string of length one (a character). Different characters will be used to represent different types of Middle Earth objects: <space> = empty locations, '*' = dragon fire, 't' = treasure, '#' = wall, 'H' = the hobbit Bilbo

 
Figure 2: Erebor, the game world

Features to implement

  1. Displays an introduction at the beginning of the game just after it starts. The introduction should explain the rules.
 
  1. Displays a conclusion when the game ends. In the full version of the game it should display the game state: lost (Bilbo slain), exited (Bilbo reaches exit) and how much treasure (if any) was collected by the player but the is provided (in the A5 directory0.
 
  1. The game world 'Erebor' consists of a 2D list of single characters. The list is initialized with the start positions of all the entities in Erebor. (Your program must use the same starting positions as mine).
 
  1. The program should display the game world using a numbered grid so that columns and rows can be clearly identified.
 
  1. As Erebor is displayed, information about Bilbo's current hit point level and gold collected is also shown (see top Figure 2).
 
  1. Displays the main (move) menu to the player. The 4 cardinal directions (N, S, E, W) and inter-cardinal directions (NW, NE, SW, SE) are mapped to positions on the computer keypad (1 = SW, 2 = S, 3 = SE, 4 = W, 5 = No move, 6 = E, 7 = NW, 8 = N, 9 = NE).
 
  1. If the player selects a hidden option '0' at the main menu then the cheat menu will be displayed. Cheat options: (d)ebug mode toggle (i)nvulnerability mode Program displays cheat menu and get user selection. Entering any other value just returns back to the main menu (time advances to the next turn).
 
  1. Player can quit the game by entering a negative value at the main menu.
 
  1. Getting input for the main menu: the program can handle invalid input type (i.e., it won't 'crash' if the player enters a non-numeric value).
 
  1. Game tracks the following attributes for Bilbo: Hit points (starts at 20), gold (starts at 0). Bilbo's hit points will go down as he is comes into contact with dragon fire and his gold score increases as he picks up treasure. (JT's suggested approach: there will likely be several pieces of information you want to track about Bilbo (these two and likely his current location). It might be easier to pass this information in a list rather than individual variables. If you do this then make sure you define some named constants to make it clear what information is being accessed e.g., BILBO_HIT_POINTS = 0, bibloInfo = [20,0,0,0] # HP=20, gold =0, row = 0, column = 0, if accessing hit points then use the named constant e.g., print(bilboInfo[BILBO_HIT_POINTS]) is clearer than print(bilboInfo[0])
 
  1. Debug mode implemented: To get credit for this feature the player can toggle debugging messages on/off at the cheat menu and debugging messages will be displayed during the 'on' state. In order to get credit for features labeled "Debug must show feature working" debug mode must show enough information so that the marker can quickly determine if the feature has been implemented correctly. You can check with your marker for the specifics on this but generally you are should show the value of different variables to demonstrate that the feature works e.g., when Bilbo is invulnerable show him getting hit with dragon fire but a message is displayed indicating that he won't take damage, when invulnerability is turned off show him taking damage (hit points before, damage inflicted, hit points afterwards).
 
  1. Invulnerability mode implemented. It's toggled on/off at the cheat menu. When turned on, Bilbo will not take damage. The mode (on/off) must be displayed as Bilbo's stats are displayed. Debug must show feature working (different from just showing status, it must somehow show that Bilbo would actually incur some damage during a turn but doesn't because invulvnerability is enabled.
 
  1. Bilbo can move onto any location (except for walls). You will progressively receive more credit as you implement more of the sub-features listed below (a) - (c). Debug must show feature working.
 
 
  1. Bilbo can move from one square to another (except into walls).
 
 
  1. Bilbo will take 4 points of damage if he's moved onto a square that contains dragon fire '*'
 
 
  1. Bilbo will pick up 3 pieces of gold from squares that contain treasure 't'. The treasure is gone after Bilbo has picked it up and won't appear after he leaves the square.
 
  1. Mode 2 implemented: as long as Bilbo is in the main hall Smaug will breath fire into the hall. Each turn some randomly determined locations will contain dragon fire. Locations that previously contained fire can either contain fire again or the fire may die out if Smaug doesn't breath into the same location. Potentially any empty square (or the one containing Bilbo) in the main hall can be hit with the fire. You will progressively receive more credit as you implement more of the sub-features listed below (a) - (d). Debug must show feature working.
 
 
  1. Each square has a 10% chance of getting blasted with fire. Squares that were hit previously (but not hit again this turn) will revert back to their old appearance (empty or containing treasure).
 
 
  1. Dragon fire only appears in empty squares (or the one containing Bilbo).
 
 
  1. If the fire appears in the same square as Bilbo then he sustains 4 points of damage.
 
  1. Mode 3 implemented: When the player re-enters the tunnel (from the main hall: location row = 14, column = 15) then the dragon fires in the main hall will die out (the squares will be cleared) and a message will appear indicating that Smaug is now moving towards the tunnel. When the player moves off this square (up or down - although the latter wouldn't be a good idea for the player because Bilbo is now trapped by the flame...in this latter case the program stays in mode 3 and doesn't revert back to mode 2) fire will appear at row 13, column 14 (entrance to the main hall. 50% of the time Smaug's fire will travel zero squares, 25% of the time it will move up one square while the remainder of the time it will move up two squares. The fire is progressive, once it appears in the tunnel it won't die out and will travel 0 - 2 squares each turn. Debug must show feature working.
 
  1. Program can detect the lose gain condition (Bilbo's hit points reaches zero or less and he is slain). At this point the world should be displayed one last time and the game should end with an appropriate status message displayed. The player is not eligible for a high score (feature #18).Debug must show feature working.
 
  1. Program can determine when the exit has been reached by Bilbo (he re-enters row 1 - he has to actually leave the square before it can be re-entered). At this point the game should end and a status message should appear that indicates that Bilbo has escaped as well as the amount of treasure that he managed to get (this is the player's 'score').
 
  1. The program tracks high scores in a file called "scores.txt". When Bilbo exits Erebor the program will read the high scores from the file. The file has two lines: an integer to signify the largest amount of gold stolen and another to indicate the hit points of Bilbo matched to the game in which the amount of gold was stolen (not the maximum hit points ever retained when the game ended). For example if Bilbo's current  gold is 9 and hit points are 20 but the file's current score is 12 gold and 10 hit points, because the file has a higher gold value the current value of 20 hit points are not recorded into the file.  If the file doesn't exist then the program should create a new file and then record Bilbo's current two stats.  If the file does exist then it will only update the two lines if the amount of gold in the current game exceeds the gold score recorded in the file. High scores are recorded only if Bilbo is still alive: if the file exists when he dies then it remains unchanged, if it didn't exist and Bilbo dies then a new one won't be created.
 
  1. Extra bonus feature: instead of displaying the simulation in text a graphical library is used (e.g., "tkinter"). The interface itself can still be console based (i.e., select a menu option via keyboard input) just the output can be graphical to get credit. Your graphics don't have to be fancy (e.g., you can use different primitives, color and/or text to distinguish the objects) but the graphical version must clearly represent the same information as the text-based equivalent (e.g., don't use rectangles of the same size and appearance for Bilbo, the treasure, the walls and the dragon fire). You won't get a lot of extra marks for implementing this feature (because graphics isn't a key feature of this program). But I did include this feature in case you wanted to spice up your program and/or you wanted to increase the probability that your assignment received a higher grade.
 

Components of a turn
  • Display game world
  • Player moves
  • Check for exit condition
  • (If applicable): Dragon fire
  • Check for lose game condition
     
  • Example output files:

    They can be found in the course directory under: /home/courses/217/assignments/assignment5

    Submitting your work:

    1. Assignments (source code/'dot-py' file) must be electronically submitted according to [the assignment submission requirements].
    2. As a reminder, you are not allowed to work in groups for this class. Copying the work of another student will be regarded as academic misconduct (cheating). For additional details about what is and is not okay for this class please refer to the following [link].
    3. Before you submit your assignment to help make sure that you haven't missed anything here is a [checklist] of items to be used in marking.