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

CPSC 233: Assignment 4 (Worth 7%)

 

Due Friday February 27

Writing a program with multiple classes: Star Trek © Paramount

Background

It's hard to believe but the "Trek" franchise has been around for almost 40 years.  Production of the first  episode "The Cage" began in late 1964.   It was given a budget by NBC of $640,000 and produced by Desilu Studios (which was since taken over by Paramount Studios).   Since that historic date, Star Trek has become a part of television and motion picture history and includes an six separate TV series and ten motion pictures.  

Your mission for this assignment

For this assignment you are to implement a simple Star Trek game.   The Trek galaxy (i.e., the milky way) will be modeled with a ten-by-ten two-dimensional array of references.  Each array element (called a "sector") will either be null (representing empty space) or contain a reference to a starship.   Starships will be represented either by the 'H' character (which is the ship that is controlled by the human player) or a 'C' (for the three computer-controlled ships).   When the game starts, the human player's starship will always start off in the same location, the "Terran sector", coordinates (0,0).    The coordinates of the three computer-controlled ships will be randomly determined (see Figure 1 for an example of the initial starting configuration of the Galaxy).

 

Figure 1: Example starting positions.

Starships are equipped with a faster-than-light engines known as "warp drive".  In terms of the game a ship can move into one of the adjacent sectors during the move phase (see Figure 2).   Even with warp engines a ship still cannot leave the bounds of the galaxy (i.e., exceed the bounds of the array).

Figure 2: Warp drive can take a ship to any of the adjacent sectors

Each starship equipped with a powerful (~1 Gigawatt) energy beam weapon called "phasers".   Phasers can be used by the human player to attack the computer-controlled ships or vice-versa (see Figure 3 to see some phasers in action).

Figure 3: A starship firing its phasers

Phasers have a limited range: only the ships in the adjacent sectors can be targeted.  In the example shown in Figure 1 the human player's ship can't shoot at the computer controlled ships nor can the computer controlled ships target the player's ship.   In Figure 4, the ship controlled by the human player at (1, 1) and the computer-controlled ship at (2, 1) are the only ships that can target each other with phasers.  Note: Computer-controlled ships will only target the ship controlled by the human player and never each other.  The ship controlled by the human player can never target itself.

Figure 4: Phasers have a limited range.

For defence starships are equipped with a energy shield, or "shields" for short,  that protects it from phaser hits (see Figure 5).  A ship's shields can absorb a great deal of attacks before the energy in the shields dissipates, the shields "fall" or go "down".  After that phaser hits will directly damage the ship's hull.  The hull of a ship can only absorb a limited amount of damage before the ship is destroyed.

Figure 5: A direct phaser hit on the forward shields!

Units for the phasers, shield and hull strength are measured in terms of "points".   The number of points available for each of the ship's systems in this version of the Trek game will be as follows:

Phaser damage is deducted from the opposing ship first from the shields (until they reach a value of zero) and then from the hull.    When the hull value of a ship reaches zero the ship is destroyed and is removed from the galaxy (that sector becomes empty).

Time passes in the game in terms of "turns".   During each turn the following sub-turns will take place.

  1. The current state of the galaxy is displayed.
  2. The computer player moves its three ships (A+ version).
  3. The human player is given a chance to move his or her starship.  The player is allowed to "pass" and not move the ship.
  4. The current state of the galaxy is displayed.
  5. The player's starship and any computer-controlled starships that are adjacent to the player's starship attack each other.  In this version of the game attacks occur simultaneously (i.e., during a turn a computer-controlled starship and the player's starship could end up destroying each other at the same time).  Again the player can pass on the attack phase although his or her ship will still be attacked by its computer controlled enemies.
  6. At the end of the program checks if the "game win" or "game lost" conditions apply.  The player wins the game by destroying all of the computer's starships.  The player loses the game when his or her ship is destroyed.

To help illustrate how combat in this works let's use the following example: we have played the game for period of time and we are faced with the following situation shown in Figure 6.  

Figure 6

At the beginning of turn we have the following statistics for the 3 starships:

Human player

Ship's system Points
Shields 925
Hull 400
   

Computer ship #1

Ship's system Points
Shields 3236
Hull 400
   

Computer ship #2

Ship's system Points
Shields 12
Hull 400
   

The human's starship targets the starship to its immediate right (Computer ship #2) and inflicts 792 points of damage.  The damage is deducted first from that ship's shields leaving 780 points.  The shield points for Computer ship #2 is now at zero (its shields are down).  Since the remaining 780 points of phaser damage exceeds the 400 points for Computer ship #2 that ship has been destroyed.   ("Got him sir!")

However because attacks occur simultaneously while the phaser beams for the human player's ship are hitting the computer player's ships, the computer controlled ships are also shooting back.   Suppose that Computer ship #1 phasers generates a mere 107 points of damage while Computer ship #2 hits back at 880 points for a total of a whopping 987 points.  Again the phaser damage is deducted from the shields until the shields fall.  In this case the ship controlled by the human player absorb a total of 925 points of damage before failing leaving 62 points of damage that effects the hull leaving it with 338 points.  So at the end of this round we are left with the following:

Human player

Ship's system Points
Shields Down
Hull 338
   

Computer ship #1

Ship's system Points
Shields 3236
Hull 400
   

Computer ship #2

Ship's system Points
Shields Down
Hull Destroyed
   

Grading: Working submissions

Classes for this assignment

Your program must implement the classes shown below.  Note: The list of attributes and mandatory behaviours are not meant to be as an exhaustive list (I had many more than the ones shown below and it is likely to be the same for your program).  Also to shorten things I excluded the obvious methods that are needed such as accessor and mutator methods as well as the constructors.

  1. class Trek: This is starting point for your program (contains the main method).  Aside from declaring a few local instances of some of the other classes and calling some of their methods, the definition for this class should be very short.

  1. Galaxy:  Models all information associated with the Galaxy.

    Attributes:

  Behaviours:

  1. StarShip:  For this game there will be four instances of this class: 3 that are controlled by the computer and 1 that is controlled by the human player.

    Attributes:

    Behaviours:

Figure 7: Status report of the player's ship after the attack phase is done.
  1. CommandProcessor: This class is responsible for getting input from the user and for displaying the menus of options that are available at a particular point in the game.   Methods of this class take different actions based on the user's input by invoking the methods of the other classes.

    Attributes:

    Behaviours:

Figure 8: Movement menu

 

Figure 9: Attack menu

 

Figure 10: The end game message if the player quits the program at any time.

 

In addition your program must include my GameStatus class (or you write an equivalent class yourself).

class GameStatus

{

        /* Required attributes */

        public static boolean debugModeOn;

        public static boolean cheatModeOn;

 

        /* You may also want to add two optional additional static attributes */

       public static boolean gameWon;

        public static boolean gameLost;

}

Required attributes.

Optional attributes.

Of course if one these optional attributes is true then the other cannot be true.  They can be used in different parts of your program to determine if it is time to end the game.

 

Basic submission (C-):  

Figure 11: Example output when debug mode is enabled


Extra features (completing all three can yield a grade of "A")

In order to receive a grade higher than a C-, you must first fulfill all the requirements of a C- assignment.  Unless otherwise stated the extra features can be completed in whatever combination that you want.

Figure 12: Cheat mode enabled.

A+ level assignment

To be eligible for an A+ you need to have completed all the features for a C- assignment, all the extra features above plus your program will randomly move the computer controlled starships during the movement phase.

Grading: Non-working submissions

D submissions:

The student has invested considerable time and effort into the assignment, the program does not fulfill any of the above requirements but it does compile.

D- submissions:

The student has invested considerable time and effort into the assignment, the program does not fulfill any of the above requirements and it does not compile.

 

Submission requirements for this assignment

In addition to having fulfill the generic assignment requirements, the requirements specific to this assignment include:

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 (e.g., "A" to "A-" for poor programming style such as employing poor naming conventions for identifiers).

2. Include a README file in your submission:  For this assignment your README file must indicate what features have been completed as well as the estimated grade level.   This will allow your marker to quickly determine what he or she must look for and speed up the marking process.  Be specific about the features, don't just list the letter grade. 

e.g., I completed the C- level requirements plus:

Total estimated grade = 1.7 + 4 steps = B.

As with previous assignments you must indicate if you use any external libraries (other than the tio or the ones produced by Sun). 

3. Assignments (source code and the README file) must be electronically submitted via submit.  In addition a paper print out of the source code and the README file must be handed into the assignment drop box for the lab 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.

 

New Concepts to be applied for the assignment

  1. Designing and implementing a moderately sized program
  2. Relationships between classes
  3. Message passing
  4. Learning a new debugging technique that is employed in actual practice (cheat mode)

 

External libraries that can be used

  1. Libraries that allow for text-based (console) input and output.
  2. Libraries that will randomly generate numbers (e.g., class Random or class Math).

 

Resources for further background information

   1.  The official Trek web site: www.startrek.com
   2.  To find out when your favourite episode will air on TV: www.spacecast.com
   3.  A nice description of the early beginnings of Star Trek: http://users.bigpond.net.au/adamdavini/History.html
   4.  A few sites in case you are really bored and have nothing to do:
  http://www.ship3.fsnet.co.uk/handweapons_how.htm
  http://www.rucus.ru.ac.za/~wolfman/Essays/Trek/trektalk.html

 

Future add-on that may apply for Assignment 5 (if it's used to extend assignment 4):

These are some things to keep in mind as you design this program because if you make your program too specific for this assignment without considering possible future extensions then you may have to rewrite major sections of your Assignment 4 code.

  1. Travel is possible at faster than warp speed (thus extending their movement range each turn).
  2. Cloaking devices have been invented (the invisibility allows opponents to hit you first).
  3. Opponents can adapt their shields to your weapon attacks (so your phasers do less and less damage to them over time).
  4. Weapons have been developed that can penetrate shields (and directly damage a ship's hull).
  5. Ships equipped with regenerative shielding (each turn the shields will increase in points).

 

Note: The use of the Star Trek © trademark was for educational purposes only and not meant as a copy write challenge.