Introduction to Computer Science II by James Tam Return to the course web page

CPSC 233: Assignment 5 (Worth 8%)

Due Friday November 26th.

New concepts to be applied in this assignment

  1. Inheritance
  2. Software reuse

 

Introduction

Professor John Robert Reuel Tolkien was a faculty member at the prestigious Oxford university.  He begin work on "The Lord of the Rings" trilogy shortly before "The Hobbit" was published in 1937.  Perhaps because of the rich use of metaphors in his work, many continue to draw parallels between his books and actual events in history: the Wain riders are said to be patterned after the Mongol invaders of Europe, the "War of the ring" was supposedly influenced  by Second World War - all of which was vehemently denied by Tolkien.  Perhaps it is a testimate to his literary genius that people today not only continue to enjoy his work but continue to see deeper meanings.  

 

Assignment description

For this assignment you are to implement a two dimensional text-based  Lord of the Rings game.  To win the player, who controls the hobbit Frodo, needs to capture Gollum and travel to Mount Doom.  If Gollum or Frodo dies then the player loses the game.  Other inhabitants of this world include the evil orcs and the dreaded Nazgul (the Ring Wraiths).

 

Grades for working assignments:

D+ grade: The default values for Middle Earth have been properly initialized: the mountains are instances of class MEObject that are in the correct starting positions.  The program can display the current state of the world onscreen.  The student has implemented a CommandProcessor class that will display a menu of "Game options" for each turn.   The menu will only have limited functionality for this grade level: 1) To quit the game 2) To not quit the game and continue onto the next turn (i.e., display Middle Earth again) 3) To invoke the cheat menu (which will be a hidden menu option "X").  For this grade level your cheat menu doesn't have to do anything so it's sufficient that the 'X' option just displays an empty menu.

Game options:
(Q)uit game

Cheat menu:
(Q)uit cheat menu

For C- and higher grade levels, you will implement a turn based game and each turn will be divided into two parts: 1) The sub-turn for the computer-controlled creatures (Gollum may move or escape, if he is in custody, and the Nazgul may move or attack) 2) The human player's turn (when the above game option menu will be displayed).

Implementation requirements for the higher grade levels: You need to implement six classes: Hobbit, Gollum, Nazgul, MiddleEarth, CommandProcessor and World.

Each of the first three classes (Hobbit, Gollum and Nazgul) must extend class MECreature (see the directory /home/233/assignments/assignment5 for the source code but below is a brief description of the high lights):

Class MECreature extends class MEObject so that your classes will indirectly inherit from the latter class.  Again you can view and copy the source code for this class from the appropriate assignment directory but a high light of the key points include:

1) The Hobbit class:

  An instance of class Hobbit, called "Frodo", will be controlled by the player of the game.  The hobbit will represented in the game with the 'H' character.  Frodo will always start the game at position (1,1), "The Shire", and can move up to one square each turn.  The player wins the game when Gollum has been captured (see the description for the Gollum class) and they travel to Mount Doom, position (12,17) marked with a "D" on the map.   The player loses the game if Frodo's hit points ever reaches zero or if he attacks and kills Gollum. 
   
 
   
  Although they may not look soft and weak , hobbits are surprisingly sturdy creatures that can absorb a fair amount of damage.  Consequently they each have twenty hit points.  However, hobbits have a poor attack capability, "I'm a gourmet not a fighter!" 
   
 
   
 

The amount of damage that they inflict will be an integer value from 1 - 6 (inclusive).  This randomly generated value will be calculated when you implement the "attack" method which instances of class Hobbit must implement. Because of their lack of combat skill, each hobbit will have another attribute which reflects the percentage chance that it can escape combat.  When Frodo is in a square that is adjacent to the Nazgul, he has a 30% chance of remaining undetected and avoiding a fight.  (Meaning that 30% of the time Frodo won't remain hidden from the Nazgul but 70% of the time Frodo will still be found and attacked by the Nazgul).  The hobbit's ability the avoid combat is not automatic and must be consciously invoked.  This reflects the fact that the hobbit is not naturally invisible but must try to actively hide from it's enemies in order to go unnoticed.  In terms of the game, when you implement the hobbit class you will need to add three menu options: 1) The option for Frodo to move 2) The option for Frodo to attack 3) The option for Frodo to hide.  The latter two options will only appear when Frodo is adjacent to another MECreature.

  Game options:
(A)ttack
(H)ide
(M)ove

(Q)uit game
   
  Marks for implementing the Hobbit class (max of two grade steps):
 
  • +1 letter step for defining the attributes of a hobbit and overriding the attack method.
 
  • +1 letter step if your game can detect that when Frodo dies, the lose game condition has occurred.  In order to get credit for this feature you need to be able to quickly demonstrate that your program is working to your marker.  Consequently you must implement an option in the cheat menu:
    Cheat menu:
(D)eath of Frodo, game lost
(Q)uit cheat menu

Your program will then immediately indicate to the player has gotten Frodo killed and that game cannot be won without him.   Hint: The easiest way to do this is set Frodo's hit points to zero and the game status flag to indicate that the game has been lost (you will also need to get your program to detect this condition).

 

Note: because most of the features of the other two classes only become evident when instances of them are interacting with Frodo, typically you must first implement the code for the Hobbit class before you can get any credit for the other two classes.

 

2) The Gollum class:

  There will only be one instance of class Gollum in the game.  He starts out at position (6,8), in his lair in the Misty Mountains, and each turn he will randomly move one square each turn.  Unless there is an obstacle in a particular direction there is an equal chance that direction of travel will include of the eight compass points.  In the game Gollum will appear as the "G" character.
   
  Sméagol (AndySerkis)
   
  He was once the Hobbit Sméagol, but long term possession of the powerful "One Ring" has irrevocably warped him forever into the evil yet pitiable creature known as Gollum.  Like a regular Hobbit, Gollum starts out with 20 hit points.  If Frodo should move to a square that is square that is adjacent to Gollum, the player will be given the addition menu option "C" which allows Frodo to automatically capture him.   When Gollum has been captured by Frodo, Gollum will automatically move to follow Frodo i.e., whenever Frodo moves, Gollum will always end up in an adjacent square.   In this version of the game, Gollum cannot attack so the attack method for class Gollum will return only zero.  However Gollum still retains the Hobbit's skill at evasion and hiding.  For each turn that Gollum is in the custody of Frodo there will be a 30% chance that he can escape. If Frodo moves to a square that is adjacent to Mount Doom and he still has Gollum in custody then the player will have won the game.  At this point Gollum betrays Frodo and finally gets possession of the Ring of Power but in the excitement he accidentally falls into the volcano thus destroying both himself and the ring resulting a spectacular eruption.
   
 
   
  Because getting Gollum to Mount Doom is necessary for winning the game, if the player attacks and kills Gollum, the game will immediately indicate that the game has been lost and the game will end.
   
  Marks for class Gollum (max of five grade steps):
 
  • +1 step for properly defining the attributes of class Gollum and overriding the attack method.
 
  • +1 step for implementing the move functionality for Gollum.  Each turn he will randomly move into one of the adjacent and unoccupied squares.  Since he lost the ring Gollum can't stand still so you cannot generate a new set of row and column coordinates that are identical to his current ones.   It is allowed to have the destination row coordinate equal the current row coordinate or the destination column coordinate equal the current column coordinate because in case Gollum will have moved to a new location.
 
  • +1 step for implementing the escape functionality for Gollum.  That is, after Frodo captures Gollum, each turn your program checks to see if Gollum has managed to escape.  Because part of Gollum's escape includes the ability to run away, you must have first implemented Gollum's random move functionality in order to get credit for this feature.
 
  • +1 step if your program detects that the won game condition has occurred.  Again in order to credit for this feature you must be able to quickly demonstrate to your TA that your program works so you must further augment the cheat menu: 
    Cheat menu:
(D)eath of Frodo, game lost
(W)in game

When the "Win game" cheat option has been selected, your program will immediately reset and display the map so that win game condition has occurred: Gollum is in the custody of Frodo and Frodo is in a square that is adjacent to Mount Doom.  Hint: The easiest way to do this is simply re-initialize the contents of the array so that Frodo and Gollum have reached Mount Doom and to set the win game flag to true.  Then your program merely needs to detect that the win game condition has occurred.

 
  • +1 step if you program detects that the lose game condition has occurred.  Again in order to get credit for this feature you need to be able to quickly demonstrate to your TA that your program is working.  To do this you need to add yet another option to the cheat menu.
    Cheat menu:
(D)eath of Frodo, game lost
(L)ose game, Gollum dies
(W)in game

Your program will then immediately indicate to the player that he or she should not have killed Gollum and that game cannot be won without him.   Hint: The easiest way to do this is set Gollum's hit points to zero and to set the game status flag to indicate that the game has been lost.

 

3) The Nazgul class:

  Except for the Dark Lord Sauron, the Nazgul are the deadliest opponents that Frodo will encounter in Middle Earth. Each Nazgul, was once a human that was given one of the lesser rings of power by Sauron and was eventually corrupted by the power of the rings.  For this version of the game there will be only one Nazgul (fortunately for Frodo!)   Each Nazgul has the ability to both mete out a devastating amount of damage as well as stamina to absorb many attacks.  They start out with 113 hit points and each attack inflicts 1 - 10 points of damage (inclusive).  The Nazgul will start out at position (11,16) and then randomly "teleport" into the one of the grey squares marked on the map.  The Nazgul is represented with the "N" character in this game.
   
 
   
  Marks for class Nazgul (max of two grade steps):
 
  • +1 step for properly defining the Nazgul class, setting the hit points and appearance as well as the correct implementation of the attack method.  The Nazgul will automatically attack Frodo whenever the two are in adjacent squares unless Frodo manages a successful evasion attempt.  Gollum will be attacked by the Nazgul.
 
  • +1 step for implementing the code to allow the Nazgul to move.  As stated earlier the Nazgul will randomly move into one of the squares from (11,16) to (14,19) except for the square that contains Mount Doom (12,17) or when a square is occupied.  Unlike the case with Gollum, the randomly generated set of destination coordinates can be identical to the current ones.  However, the Nazgul will not move if it is already in combat with Frodo.  It will continue attacking him until either Frodo moves to an adjacent square or if Frodo makes a successful hide attempt.

Classes that must be implemented in all the working versions of the game

  1. Class MiddleEarth: The starting execution point in the program (contains the main method).

  2. Class CommandProcessor: The purpose of this class to provide an interface to the user: it shows the available menu options as well as prompting the user for his or her selection and acting upon his or her choice.

  3. Class World: This class is analogous to the Biosphere class from the previous assignment and it tracks information that relates to the Middle Earth world as a whole.  Unlike the previous assignment you will only need to implement a single array.  The map is implemented as an array of references to MEObjects.  Each array element can either be null or contain the address of an object.  In the assignment directory I have a version of the class that initializes the world with the proper starting positions of the mountains in middle earth.  The mountains act as movement barriers so none of the MECreatures should be able to move onto a square occupied by a mountain.  As you implement the code for the other creature classes you can add their starting positions to the map.  This class will include information about the min and max ranges for the row and column coordinates (in the form of static, final attributes). These values are used in the appropriate mutator methods of class MEObject to check if new coordinates for an object will be valid of not.

Pre-created code that you have to use for all grade levels (which can be found in the assignment directory):

  1. Class GameStatus.  This class replaces the Debug class from the previous assignment.  Besides containing an attribute to indicate whether or debugging mode has been turned on, there will be two other attributes which indicate whether the "game lost" or "game won" conditions have been met.  You will have to be careful that the latter two attributes are not both set to true.  However, they can both be set to false (e.g., when the player starts the game, neither the win game or lose game conditions should have occurred yet).

  2. Class MEObject: This is the parent class for all the objects that exist in Middle Earth and includes the inanimate mountains as well as the sentient and inanimate life forms.   You need to include the code for this class with your assignment submission and you cannot modify the definition of this class.

  3. Class MECreature: The parent class for all the living creatures in Middle Earth.  Class Hobbit, Gollum and Nazgul must inherit from it and you cannot modify the source code for this class.

 

Grades for non-functional assignments

D level assignment

The student has invested considerable work in the assignment and the code compiles but it doesn't fulfill any of the above requirements.

D- level assignment

The student has invested considerable work in the assignment but it doesn't compile.

 

Other submission requirements

  1. Good coding style and documentation:  They will play a role in determining your final grade for this assignment.  Your grade can be reduced by a letter step or more for poor coding practices e.g., "A" to "A-" for using poor naming conventions for identifiers, insufficient documentation, the use of global variables (other than the GameStatus flags), improper application (or misapplication) of inheritance or by not assigning the proper operations and attributes to particular classes.

  2. Include a README file in your submission:  For this assignment your README file must indicate what functions you have completed as well as the grade level that you are aiming for.  This will allow your marker to quickly determine what he or she must look for and speed up the marking process.  

  3. Assignments (source code and the README file) must be electronically submitted via submit.  In addition a paper print out of the source code must be handed into the assignment drop box for the tutorial that you are registered in (located on the second floor of the Math Sciences building).  Electronically submitting the assignment allows your marker to run the code in order to quickly determine what features were implemented.  Providing a paper printout makes it easier for your marker to read and flip through your source code.

  4. A demonstration of your working program with your TA will be requiredThe demonstrations will take place in the tutorials but you must book a specific period of time with your TA.  If you do not demo your code to your TA, then the maximum grade that will receive is a "D".  Further details will be provided in the tutorials about the demos.

Suggested Approaches to this Assignment:

  1. All 5 points from the previous assignment also apply for this one.  However I cannot emphasize enough that you cannot start this assignment at the last minute, start as soon as you can or else you may not finish in time.

The Lord of the Rings trademark is owned by Tolkien Enterprises.  The license to the Lord of the Rings movie trilogy is owned by New Line Entertainment.   References to either the original trademark or the inclusion of images are for education fair use only and not meant as a copy write challenge.