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

CPSC 219: Assignment 5

Due March 30 at 4 PM

New concepts to be applied for the assignment1

  1. Inheritance
  2. Casting within an inheritance hierarchy
  3. Method overriding & Polymorphism

[JT's hint: The final exam will include a question or questions relating to material in the 'hierarchy' section. The  question(s) be drawn from the three new concepts introduced in this assignment (and likely other topics such as implementing interfaces). Consequently I strongly recommend that if you don't have time to attempt all the major features of the assignment that you at least attempt features that require the application of all of the above topics.]

Introduction

For this assignment you will be creating a computer game that simulates a portion of Professor JRR Tolken's novel, "The Fellowship of the Ring" (see Figure 1).

Figure 1: Breaking of the Fellowship (Boromir defends the Hobbits)

The Gondorian hero Boromir 'B' must ensure that the hobbits 'h' escape from the horde of evil orcs: goblins 'g' and the elite Uruk-hai 'U'. The hobbits will automatically flee to the exit 'X' while Boromir is controlled by the player. The orcs will be controlled by the computer and use their scimitar swords to attack Boromir or the hobbits if they come within striking range. The elite Uruk-Hai carry bows and can hit targets from afar. If the players have bought enough time for the hobbits reach the exit (bottom right) then the game is won (even if Boromir is slain). If the hobbits are slain the game is lost. The hobbits may be physically weak but they can blend into terrain (i.e., "hide in shadows") so half of the attacks will miss. Time is broken down into discrete time units 'turns' and each turn is broken into steps or 'sub-turns'. In order to make your program easier to mark your program must react in a predictable fashion so the order of the sub-turns must be same as the ones specified in the "Sub-steps" section.

Required classes for this program

Pre-created classes that you must use (see the 'resources' section) Classes that you must implement
MEObject Lotr
Coordinate CommandProcessor
Humanoid MEWorld
Mode Hobbit
GoodGuy Human
  Goblin
  UrukHai

Pre-created classes

MEObject: represents all the entities in the game world.  Each instance of the class will have an appearance attribute. Also each MEObject will have a 'hitPoints' attribute that indicates the amount of damage that the entity can take before being slain (or destroyed for the non-living objects). This class will have a number of class constants: one for the appearance, one for the default hit points and one to determine the threshold of life (at or below this level the entity is longer living). Each child class should have its own class constant to indicate the default appearance of the instances of that class e.g., class UrukHai { public static final DEFAULT_APPEARANCE = 'U'; ... }

Coordinate: each location (array element) in the world can be described by a row/column pair. Although I could have required you to use a pair of integers to describe the location it would be easier for this assignment to use a single object. Some methods will generate and return a location (e.g., class Humanoid's move method) so it's easier to return the row and column value as a single coordinate rather than as two separate integer values. (Recall that methods only return at most a single value).

Humanoid: a child class of MEObject, it represents living and moving occupants of the world. Consequently instances of this class will have four attributes: the current row/column coordinate of the humanoid, the attack range, and a boolean flag that indicates whether the object has moved yet during the turn. Additionally Humanoids have methods that allow them to move (randomly generates a value that determines which of the adjacent squares that it will attempt to move to - the MEWorld class will determine whether that location is a valid: is it in bounds and is it empty).  Finally humanoids can attack one opponent each turn. The default damage is 2 points although the child classes will inflict differing amounts of damage by overriding the attack method.

Mode: a class used to track the game's state: (1) debug: will debugging messages appear (2) cheat: is cheat mode enabled (Boromir but not the hobbits) becomes invulnerable (3) status of the game: is it neutral or has the player won or lost. The game can only have one state so the attributes and methods are entirely static.

Classes that you must implement

Lotr: the starting execution point and should contain a reference to an instance of class CommandProcessor so that the game can be started:

public class Lotr {
     public static void main (String [] args) {
          CommandProcessor anInterface = new ComandProcessor ();
          anInterface.run();
     }
}

CommandProcessor: this class acts as the user's interface to the game (handles all input and output).

Minimum required attributes: a reference to an instance of class MEWorld (the game world), something to store the user's selection for the main and cheat menu.

Minimum required methods: will include everything associated with the user interface (e.g., displaying menus, getting user selections, determining what selection was made etc. It should also be responsible for starting and ending the game).

MEWorld: tracks all information about the game world, Middle Earth.

Minimum required attributes: a reference (called 'grid') to a 10x10 array of references to 'MEObject', several constants for the array (MIN = index of first element in a row/column, MAX = index of last element in a row/column, SIZE = size of the array).  Empty elements should be set to null. Occupied elements will be a reference to an MEObject object (or a child class).

Minimum required behaviors: display the game world (you will get a few extra marks for displaying a numbered grid around each array element, shouldn't be hard since were given this code previously), moving the entities in the game (JT's hint: each entity has attributes so when you move a life form around you should not just create a new life form at the destination because the new object's attributes starting attributes may not be the same as the current values of the attributes of the entity being moved)...in short the methods include anything that changes the data of the game world (the grid). Note: the constructor for this class will determine the starting positions for the objects in the game world. For this version of the game the positions will be fixed each time that the game is run.

Hobbit: one of the heroes in the game but is controlled by the computer.

Minimum required attributes: several constants will be included to model the hobbits behavior: HIDE_TERRAIN (the percentage chance that a particular attack on the hobbit will miss), MAX_HIT_POINTS = 10 (hobbits may not be strong but they are tougher than the average humanoid so they have a higher default value), EAST/SOUTH (indicates which direction that the hobbit will travel). At the start of the game the hobbit will continue moving east until either an obstacle is encountered, it's been slain or it has reached the eastern most column (in the latter case the hobbit will then move south).

Minimum required behaviors: include some overridden methods. There should be a new attack method to reflect the range of damage that a hobbit can inflict (1 - 6 points). Also this class should have a new method of deducting damage that determines whether or not the hobbits take damage from a particular attack (JT: this is actually a helpful hint for implementing the 'hide in shadow's capability).

Human: the hero in the game that is controlled by the player.

Minimum required attributes: a constant MAX_HIT_POINTS = 20 (humans are physically larger and stronger than many of the other inhabitants of Middle Earth so this value is set higher).

Minimum required behaviors: an overridden attack method to reflect the range of damage that a human can inflict (4 - 13) points.

Goblin: the weakest of the orc-kind. Humanoids that are villains that are controlled by the computer.

Minimum required behaviors: an overridden attack method to reflect the range of damage that a goblin can inflict (1 - 8 points)

UrukHai: the elite shock troopers in the orc ranks. They are also computer-controlled.

Minimum required attributes: a constant to reflect their increased size and fighting skill MAX_HIT_POINTS = 14, and another constant DEFAULT RANGE to reflect the fact that they carry bows and can damage opponents up to two units distant (up or down two rows, over two columns, all other humanoids that attack can only reach adjacent opponents).

Minimum required behaviors: similar to class Goblin there should be an overridden attack method to reflect the range of damage that an UrukHai can inflict (3 - 9 points).

In the event that an attacker is within range of multiple targets then the computer will randomly determine which target gets hit. Good guys (hobbits and humans) don't attack each other. Similarly the bad guys won't attack other. The player can determine which bad guy to attack.

Providing an overview of your program

You will need to create a UML class diagram that specifies the full information about the classes in your program (method signature and return value, method attributes) as well as the relationships/multiplicity between classes. Your diagram should not only include the classes that you implement yourself but also the required classes that I created.

Program features (you will also be graded on other criteria such as style and documentation as listed in the marking key):  After creating the above class definitions here are features that should be included in a working program.

Note: the marking key for this assignment will be set up so you can get a top grade without necessarily implementing each and every feature (there will be a margin of error built in). However in order to prepare yourself for the exam make sure you attempt (better yet complete) as many features as possible.

  1. Displays an introduction to the program that describes the rules. It only displays these instructions right after the program is run.
  1. Displays a sign off exit message when the program ends.
  1. Starting positions for the objects are initialized in the same positions as my program. The game world is an array that is an attribute of MEWorld. The contents of the array positions are set using the constructor (and it's helper methods) in the outline 'MEWorld.java' program included in the assignment directory.
  1. The game world displayed using a numbered grid - keep in mind that for the assignment your array should be references to MEObjects not characters. The current turn number will be displayed at the start of each turn (but not during each sub-turn unless there is some additional information to indicate when a new turn has began).
  1. Boromir's and the hobbit's current hit points are displayed each time that the world is displayed.
  1. The main (move) menu is displayed. This menu will show the movement options available for Boromir.
 
  1. The player can quit the game by entering a negative value at the main menu.
  1. During the attack sub-step, the player is presented with a directional menu similar to the movement menu. In this case the directions will indicate the directions of attack rather than movement.
  1. The player can invoke the cheat menu from either the movement or from the attack menu by entering the hidden '0' option.  When invoked the cheat menu will display options to: 1) toggle debugging mode 2) toggle cheat mode 3) quit the cheat menu and return back to the previous menu.
 
  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.  The status of cheat mode should be tracked by the appropriate attribute of class 'Mode'. 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 the hobbits are attacked it would probably be a good idea to show the following information: hobbit hit points before the attack, the amount of damage inflicted by a particular attack, an output message indicating if the hobbit took no damage from the attack (successful hide), hobbit hit points after the attack.
  1. Cheat mode implemented: To get credit for this feature, the player can toggle this mode from the cheat menu. The status of cheat mode should be tracked by the appropriate attribute of class 'Mode'. When cheat mode is turned on, Boromir will become invulnerable to all attacks. When cheat mode is turned off, Boromir will suffer damage from attacks as usual. Cheat mode has no effect on the hobbits. (Debug must show feature working)
  1. Boromir can move. The player can move Boromir in any of 4 cardinal compass directions (S, W, E, N: options on the main menu 2, 4, 6, 8), as well as the 4 inter-cardinal directions  (SW, SE, NW, NE: options on the main menu 1, 3, 7, 9) or have Boromir remain on the same square (option 5 on the main menu). You will progressively be awarded more marks as you implement the features below so that the movement is more refined. (Debug must show feature working)
  1. Boromir moves to any of the 8 adjacent squares.
  1. Boromir only moves onto empty squares.
  1. List bounds are checked during the move (Boromir won't move outside the game world).
  1. Hobbits move in the following fashion. They start out just right of the top left hand corner. (Boromir occupies the corner). From this position they will move along the top row to the top right corner. After reaching the corner, the Hobbits will move straight down the far right column until they reach the bottom right corner. You will progressively be awarded more marks as you implement the features below so that the movement is more refined. (Debug must show feature working)
  1. The hobbits can move in the directions described above.
  1. Hobbits will only move onto an empty square.
  1. The game is won when the hobbits reach the bottom right corner of the screen. The program needs to be able to detect when this has occurred and immediately end the game with a suitable congratulatory message. (Debug must show feature working)
  1. The game can detect when the game is lost (hobbits slain), the world should be displaying once more and ending with a suitable consolatory message. (Debug must show feature working)
  1. The player can get Boromir to attack one of the adjacent orcs (goblins or Uruk-Hai). During the attack step, the program show a directional menu that's similar in appearance to the move menu. You should implement the attack feature by overriding the attack method of class Humanoid so that humans inflict 4 - 13 points of damage. (Debug must show feature working)
  1. The hobbit will attack one opponent who is in an adjacent square. You should implement the attack feature by overriding the attack method of class Humanoid so that hobbits inflict 1 - 6 points of damage. (Debug must show feature working)
  1. The orcs (Uruk Hai and the goblins) will move to an adjacent square. You will progressively be awarded more marks as you implement the features below so that the movement is more refined.
    To make it simple you can use the following approach for determining what is the order of movements. Start at the left hand corner. If there is an orc there move it. Now based on the current state of the orc work along that row from left to right moving the ones that haven't moved. The new location of humanoids that have moved should be treated as being occupied to humanoids that move later (but it's source will be treated as empty i.e., ones near the top left move 'before' the ones near the bottom right). Then move down to the next row and again move from left to right. Repeat until the square at the bottom right has been reached.  Although all movements for all the orcs are all finished before the world is displayed, the program needs some rules for determining the order of movement (i.e., you can't have two humanoids moving into the same square during one turn). Consequently that means that movement is checked from top to bottom, left to right in order to resolve collision conflicts. (Debug must show feature working)
  1. The orcs can move to any of the 8 adjacent squares or stay on its existing square. In this case the choice of square should be randomly determined and the chance for each of above 9 possibilities is the same for each case.
  1. Orcs only moves onto empty squares. If the program's first choice for the destination square is occupied then the  program shouldn't immediately stop trying to move the orc it should at least try a reasonable number of randomly generated choices before determining that it won't move.
  1. List bounds are checked during the move (Orcs won't move outside the game world).
  1. Goblins will randomly attack one opponent that is within range. The attack range for goblins, same as with hobbits and humans is one so it will only attack opponents in one of the adjacent squares. You need to override the attack method of the parent class so that goblins inflict 1 - 8 points of damage per attack. (Debug must show feature working)
  1. Uruk Hai will randomly attack one opponent that is within range. You will progressively be awarded more marks as you implement the features below so that the attack  is more refined.  (Debug must show feature working)
  1. UrukHai will attack one opponent in one of the 8 adjacent squares. The attack method needs to be overridden so they inflict 3 - 9 points of damage.
 
  1. Because UrukHai carry bows their attack range is two (e.g., if an Uruk Hai were on row 4 it could attack opponents from row 2 to row 6).
  1. Hobbits have the ability to blend into their terrain so that for each attack there is a 50% chance that the attack will miss. (Debug must show feature working)
  1. Game detects when Boromir is slain (hit points zero or less). It should remove Boromir from the game world and no longer display the menus to the player. (Game runs on 'automatic'). The game doesn't end until the hobbits reach the exit or are slain. (Debug must show feature working)
  1. Game detects when the hobbit is slain (hit points zero or less). It should remove it from the game world. (It is possible that your game can detect when hobbit is slain but not detect that the lose game condition has been met so I'm listing them as two separate but related features). (Debug must show feature working)
  1. Game detects when a goblin has been vanquished. Again the game should remove the object from the game world. (Debug must show feature working)
  1. Game detects when an UrukHai has been vanquished. Again the game should remove the object from the game world. (Debug must show feature working)
  1. Additional bonus feature: instead of displaying the simulation in text, a graphical library (e.g., Java 2D etc. is used). The interface itself can still be console based (i.e., select a menu option via console input) just the output can be graphical. Your graphics don't have to be fancy (e.g., you can use different primitives, color and/or text to distinguish the runners) 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 each MEObject). You won't get a lot of extra marks for implementing this feature (because graphics isn't a key feature of this program) and you will either have to be familiar with a library or be willing to invest the time to learn one on your own. 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.
  1. Additional bonus feature: program adds sound effects. Your program should sparingly add appropriate sound effects. Similar to the previous feature, this feature won't be worth a lot of marks and will require familiarity with classes to play sound, or the willingness to learn how they work on your own.

Sub-steps of a turn

Resources:

They can be found in the UNIX directory: /home/219/assignments/assignment5 or /home/233/assignments/assignment5. Within this directory are two sub-directories: 'java_code' and 'sample_runs'. The first directory contains the dot-java files that are needed by your program. The second directory contains text files that show the output of my solution program under different situations. In the output you may see references to features that you aren't required to implement for this assignment (e.g., healing lembas) so you don't have to worry about coding those up - I just wanted to program a more entertaining game for my own edification!

Web shortcut to the A5 directory

Submitting your work:

  1. Assignments (source code/'dot-java' files) must be electronically submitted according to [the assignment submission requirements]. For this assignment you also need to create a UML class diagram that shows: the classes, their relationships, as well the attributes/methods and their access permissions. Individual classes must show the 'full information' for each method (parameter names and types as well as the return type). Class diagrams can be drawn by hand and scanned, or drawn electronically. Acceptable file formats for the class diagrams include: '.jpg', '.gif' and '.png'. You are to electronically submit the class diagram along with your source code (make sure you don't forgot to submit all the dot-Java files). Note: the 'showstuff -v' option only works with text files so you need to be extra careful to check the image before submitting it - when in doubt submit it again.
  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.

External libraries that can be used

  1. Libraries that allow for text-based (console) input and output.
  2. Classes used to generate random values such as Class Random or class Math.
  3. (If the last two features are implemented).  Java libraries that draw two-dimensional graphics or play audio files.

The Lord of the Rings trademark is owned by Tolkien Enterprises. References to the Trilogy are for education fair use only and are not meant as a copy write challenge.