Computer Science I for majors by James Tam

Return to the course web page

CPSC 231: Assignment 4

New concepts to be applied for the assignment

  1. Complex program design, problem solving a non-trivial problem1
  2. 2D lists
  3. File input

(Although functions are not a new concept to be applied for this assignment your program must be decomposed into functions using "good style" Slide No. 80 - 82 ('decomposition' notes) and documented as specified on Slide #76. Otherwise you will be penalized heavily for functionality and documentation.

Writing a simple adventure game:

For this assignment you are to write a text-based adventure game based on 'The Simpsons Movie' © Fox. The game world will be modeled as a 20x30 two-dimensional list. Sample program output is shown below:

 
 

Character

Value represented

 

H

Homer Simpson (controlled by the player)

 

#

A building (cannot be entered)

 

E

Agents of the EPA (controlled by the computer)

 

~

Water (Homer can enter, EPA agents cannot)

 

S

The town of Springfield (Homer can enter, EPA agents cannot)

  The space represent empty parts of the game world.

In the assignment4 directory there is a [partial source code] program that provides the starting positions for all the characters in the list. Although it is not mandatory that you use this start code, to get full credit your program must be able read in, at a minimum, all the starting positions in all the [data files] provided. (Other data files may be used by the marker with different positions but they will all consist of 20x30 game world with the above set of characters).

The goal of the game is to bring Homer from Alaska (top portion of the grid) back to the town of Springfield (near the bottom right). However if Homer takes too long to travel out of Alaska he will freeze and the player loses the game. Also if Homer moves into the water he drowns and the game is lost. Finally the tireless agents of the EPA will try to capture Homer and if that occurs the game is lost. The game will be run on a turn-by-turn basis. A turn elapses when: Homer moves or the player tries to move him (Homer ignores the command or the player tries to move Homer onto a building), Homer stays on the same square (direction 5), or the player toggles the debugging mode on or off. A turn won't pass if the player enters an erroneous value for the movement direction (10 or larger). After Homer moves the EPA agents will move in the fashion described in Point #15 below. The game continues until: 1) The player has lost the game (Homer drowns, freezes or is captured...Doh!)  2) The player has won the game...Woohoo! 3) The player has quit the game. *Shrugs*

Features to be implemented (you will also be graded on other criteria such as coding style and providing program documentation as listed in the marking key):

 
  1. Display an introduction each time that the game is run which provides instructions for playing the game.
 
  1. Display a conclusion each time that the game ends. The conclusion must show the player's game status (won, quit, or if lost how the player lost the game: captured, frozen, drowned) to get credit for those features.
 
  1. Displays a menu that shows the allowable directions that Homer can move which are mapped to the keypad on the keyboard. The menu should show the 4 cardinal compass points (N = 8, W = 4, E = 6, S = 2). The menu should also indicate that selecting 0 will allow the player to quit the game and selecting 5 is to 'pass' on the move. (The option for entering the debugging mode (any negative value) should not be shown in the movement menu because it's supposed to be a hidden 'cheat' function).
 
  1. The starting positions for the objects in the game are loaded from a file. (If you use the default initialization method that's provided in the starting 'simpsons.py' file in the assignment directory you will only be awarded a partial grade at most for features labeled "File input required"). That's because the marker needs to be able to quickly determine if a feature is working).
 
  1. Homer can be moved in the four cardinal compass directions (to get credit for Feature#3 your program merely has to display the menu whereas to get credit for this point your program has implemented the code that actually makes Homer move). Entering #5 will result in the player 'passing' on Homer's move for this turn (time passes i.e., a count is made to determine if Homer freezes and the EPA agents can move).
 
  1. If the player tries to move Homer in one of the inter-cardinal compass directions (NW, NE, SW, SE) an appropriate error message will be displayed e.g., "Homer doesn't know how to move that way" and Homer will not move.
 
  1. If the player tries to enter a value larger than 9 for the movement direction another error message will be provided. (Because it's a user error rather than an intentional skipping of a turn (Feature #5) time in the game does not elapse in this case i.e., the EPA agents don't move). The player will be prompted again for his/her selection.
 
  1. Entering zero for the direction of movement will allow the player to quit the game.
 
  1. Entering a negative number for the direction of movement will toggle debugging messages on and off by using the global 'debug' flag. Note that this is the one exception on prohibition on the use of global variables. To get credit for this feature you have to use the debugging flag and an output message at least once in your program. The exact content of these debugging messages (e.g., showing function calls, showing the contents of variables at different points in your program) are left to your discretion but as the name implies they should be used to help you debug your program. (JT's hint: you should implement this feature early on so you can actually use it to help you debug your program because this is a complex assignment it will actually help you in the development process. You shouldn't implement it at the end merely to fulfill the assignment requirements when it's too late to help you fix the errors in your game because that defeats the whole purpose of writing a debugging tool). Also note the features that involve random events (No. 12, 15) not only require that you implement debugging messages to get credit for that feature but also that the debugging messages display the required information (so your marker can quickly determine if the feature is correctly implemented or not). All debugging messages must be preceded by 3 angled brackets and followed by three angled brackets. e.g. print("<<< Calling function displayWorld() >>> ") to allow quick recognition of when they're employed.
 
  1. (File input required) The game prevents Homer from entering an occupied square (contains a building or an EPA agent). A suitable error message should be displayed and time will pass as normal (EPA agents move, Homer's time in the arctic counts another turn).
 
  1. If Homer enters the water, he drowns and the game ends (with an appropriate consolation message for the player).
 
  1. The game adds to an unfocused, easily distracted side to Homer's personality (which is pretty much an actual personality trait of the character Homer Simpson). Each turn that the player tries to move Homer (direction 2, 4, 6, 8) there is a 25% chance that Homer finds something more interesting at his current location. To get credit for this feature your program should indicate to the player that Homer has become distracted this turn and refuses to move (e.g., "Homer becomes distracted and refuses to move") but it doesn't have to be as elaborate as the ones in the sample output files. Time will pass even if Homer is distracted and 'wastes time'.

    Required output of the debugging messages:

    Format:
      <<< Randomly generated result >>>
      <<< A list that shows for each possible randomly generated number (or range of numbers) how the program will react.
      <<< The way that the program actually reacted for the random number generated >>>

    Example program output:

      <<< Number randomly generated = 24 >>>
      <<< Number = 1 - 25, Homer is distracted >>>
      <<< Number = 26 to 100, Homer follows the player's movement instructions >>>
      <<< Homer becomes distracted and refuses to move >>>
 
  1. The game tracks the amount of time that Homer spends in Alaska (top six rows of the grid - start counting at the row that consists entirely of water). Homer can only remain in this area for 10 turns before he freezes.  To get credit for this feature, each time that the game shows the grid it also displays how long Homer has been in Alaska and how much time he has left. There is a simple and an enhanced version of this feature (you can only get credit for one version). With either version when his time runs out Homer freezes and the game ends (with a suitable consolation message being displayed).
   
  1. Simple version: If Homer spends a total of 10 turns in Alaska he freezes. Those turns don't have to be successive ones. If Homer leaves and re-enters Alaska the count down for his 'time remaining before he freezes' will continue where it left off.
   
  1. Enhanced version: Homer has to spend a total of 10 successive turns in Alaska before he freezes. If he leaves and re-enters Alaska then the count down will start again at the beginning (he has 10 turns before he freezes).
 
  1. (File input required) When Homer makes it back to Springfield (moves on top of the square marked 'S') the game is won and a suitable congratulatory message should be displayed. If Homer reaches Springfield just as an EPA agent catches him it's deemed that he has reached a safe haven in time and the player wins the game and suitable congratulatory message is displayed (image below).
   
     
 
  1. The agents of the EPA can move (in a purely random fashion, equal probability for each outcome). Agents will only move to an empty (contains a space) and adjacent square with a one in nine probability of a particular square being picked (there's eight agent squares plus there's a chance that the agent won't move). That mean's that agents don't 'drown' or try move onto squares that contain buildings.  (JT's hint: to implement this feature properly you need some way to track the (row/column coordinates of each agent in a fashion somewhat similar to how you tracked Homer's location- except that you now must track 6 pairs of row/column coordinates instead of just one pair. The sample starting code uses a 2D list to track this information. This feature is more challenging than the others so you should leave it to near the end).

    Required output of the debugging messages: similar to Feature #12 in order to get credit for this feature your program must display specific output messages that clearly indicate to the marker that your program is working.

    General format:
    <<< <The randomly generated movement direction>, <The current (row/column) before the move>, <The destination (row/column) after the move> >>>

    Example program output:
    <<< Agent direction: 2, Agent source (r/c): (8/9), Agent destination (r/c): (9/9) >>>

 
  1. (File input required) The game can detect when an EPA agent is in a square that is adjacent to Homer (up to 8 surrounding squares if Homer isn't near the edges) and will immediately end the game (with a suitable consolation message being displayed).
   
    Prior to player's move: Homer and agent are in close proximity
     
   
    Player tries to move Homer but the command is ignored. The agent moves adjacent to Homer and captures him: the game immediately ends and a suitable consolation message is displayed.
     

Resources available

In the course directory under the path: /home/231/assignments/assignment4 you will find a number of resources:

Submitting your work:

  1. Assignments (the source code/'dot-py' file) must be electronically submitted according to the assignment submission requirements using D2L.
  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 notes on misconduct for this course.
  3. Before you submit your assignment here is a [checklist] of items to be used in marking.

1 This from the view of a student who is new to programming the design and problem solving issues faced in this assignment are non-trivial. It's worth noting that compared to an actual commercial system that this software is still quite small.