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 & exception handling
  • 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" 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. Bilbo is slain if his hit points reaches zero or less.  The game ends and the player has lost. If however Bilbo returns to the entrance/exit alive 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 to the player.
 
  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.
 
  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 starting positions as the code in the A5 directory - which if properly cited can be used in your submission).
 
  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 (any negative number) at the main menu then the cheat menu will be displayed. Cheat options: (d)ebug mode toggle (i)nvulnerability mode toggle. This feature requires that the program displays the cheat menu and gets the user's selection. Entering any value at the cheat menu except the two valid options just returns the game back to the main menu (time advances to the next turn).
 
  1. Player can quit the game by entering '0' at the main menu.
 
  1. Getting input for the main menu: as well as getting valid input the program can handle an invalid input type (i.e., it won't 'crash' if the player enters a non-numeric value) via an exception handling mechanism.
 
  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 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 (his health, wealth and likely his current location). It might be easier to pass this information in a list rather than track 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 using the named constant: print(bilboInfo[BILBO_HIT_POINTS]) is clearer than an unnamed constant: 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 (debugging messages will be displayed during the 'on' state). In order to get credit for features listed below and 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 (no damage from dragon fire) implemented. To get credit for this feature the player can toggle invulnerability on/off at the cheat menu. 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 invulnerability 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: from when Bilbo enters the main hall until he leaves it, Smaug will breath fire into that location. 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. 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 appear empty (treasure and walls appear to get 'wiped out' by this partially completed version).
 
 
  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 (13,E) then the fires in the main hall will die out (the squares will be cleared) and a message will indicate 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 (13,E). 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 game condition (Bilbo's hit points reaches zero or less). At this point the world should be displayed one last time and the game should end with an appropriate status message. 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 (0,E) - re-enter means that the square must first be exited). 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'). Bilbo can exit Erebor without moving past (1,E), no treasure has been collected so it's a little pointless but still possible.
 
  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 a text-based game, 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) only the output need be graphical. Your graphics don't have to be fancy (e.g., you can use different shapes, color and/or labels to distinguish the objects) but the graphical version must be understandable (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. (Or perhaps because you were bored because you found the existing assignment far too easy).
 

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. Source code Python program/'dot-py' file(s) must be electronically submitted according to [submission submission requirements].
    2. Copying/viewing other student's work 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 [misconduct link].
    3. Before you submit your assignment here is a checklist of items to be used in marking: [checklist link]

    External material used:

    External libraries that can be used (unless listed don't assume you can use an external library)