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

 

CPSC 231: Full Assignment 2

Due March 3 at 4 PM

New concepts to be applied for this assignment

Introduction

You will create a text-based space combat simulator (Figure 1). There will be two ships: one is controlled by player (the 'H' in the figure) , the other is computer-controlled (the 'C' in the figure). The game 'galaxy' is modeled as 4 x 4 two-dimensional grid. Notice how rows and columns are numbered and each element is bound: above, below, to the left and right by lines. The game proceeds on a turn-by-turn basis until: the player quits, the game is won (computer ship destroyed), the game is lost (player ship destroyed) or there is a tie (both ships destroy each other)

Figure 1: The game world

Components of a turn

Repeat the following steps until one of the end game conditions has been met

Movement

Ships (player and computer) can only move to adjacent squares (Figure 2A).

Figure 2A: Nine possible movement destinations ("no movement" is counted as a destination)
 
Figure 2B: Movement menu (displayed when the player can move his/her ship)

Computer-controlled ships will randomly pick one of the nine possibilities (equal probability for each). The player will be prompted for the movement choice with a direction-oriented menu (a desktop keyboard mapped to the 4 cardinal (S=2, W=4, N=8, E=6, ) and inter-cardinal directions (NW=7, NE=9, SE=3, SW=1) - see Figure 2B. Selecting 5 indicates a 'pass' during that phase. The player can quit the game by selecting 0. Finally there is a hidden 'cheat' menu that will be invoked if the player enters a negative integer value (see the 'Cheat Menu' section). The player cannot 'ram' the opposing ship and will receive an appropriate error message with the movement prompt re-appearing again.

Attacking

Adjacent ships can attack each other. The computer ship will always attack the player. The player will be prompted with a directional menu (similar to the one shown in the movement menu) during the attack phase (again 5 is a pass, 0 will quit the game, negative values will invoke the cheat menu). Damage ranges from 1 - 100 points which is randomly determined each attack phase separately for the attacker and defender. The damage is deducted from the opponent's hull points. If the hull reaches a value of zero (or less) then the ship is destroyed. The player's ship always has a hull value of 400 while the computer's will be determined at run time by the user.

Ending the game

As mentioned the player can quit the game during the attack or movement phases. Also the game can end if one or both ships are destroyed (win, lose or draw for: computer ship destruction, player ship destroyed or both destroyed).

Regeneration

If after the attack phase the end game conditions haven't been met then the ships invoke their "auto-repair" capability (40 points restored up to but not exceeding the original maximum hull value of 400 points).

Cheat menu

It may be invoked during attack or movement. The cheat option is hidden (no clue should be displayed as to how it appears). There are two 'cheat' options. One toggles cheat mode, the other toggles debug mode (see Figure 3). Entering and exiting the cheat menu does cause time to elapse e.g., invoking it from the movement menu advances time so the player will then see the attack menu.

Figure 3: Cheat menu options invoked via a 'hidden' menu option (attack or movement menu)

Cheat mode: makes the player's ship invulnerable to attack.

Debug mode: The exact content of the debugging messages is up to you (see Figure 4 for some example output).  Generally the messages that are displayed onscreen are used to help the programmer determine where bugs are located and how to fix them e.g., when moving a starship your debugging messages may display the current row/column coordinates as well as the coordinates of where the ship is supposed to go. Another possibility will display messages as a method is called as well as it's state (values stored in parameters). This could allow you to perform a 'stack trace' and view the series of method calls.

Figure 4: Debug mode displays messages about the state of the program

Classes

StarShip

Min attributes: current hull value, appearance (a single character). You will probably want each ship to track the current (row/column) location in the game world (e.g., in Figure 4 that value would be (0,0) for the player's ship and (1,1) for the computer ship).

Min behaviors: methods that act on the attributes (accessors, mutators), as well as methods to: determine attack damage, regenerate ships, reduce hull value in response to an attack.

Galaxy

Min attributes: the current state of the game ‘world’ which is modeled as an array of references to StarShip objects. All but two locations are empty (null), the two occupied ones contain references to the two starships.

Min behaviors: display game world (each element bound by numbered grid: see Figure 1 & 4), modify game world (e.g., ship moves), determining state of the world (e.g., determining if ships are adjacent, is a particular square occupied, is a (row/column) location inside the bounds of the world etc.)

GameManager

The manager has two main responsibilities: all user interaction (e.g., prompts for and reacts to user input) - similar to the [GameInterface] example covered in tutorial, manages the state of the game (the running of each turn will occur here), sub-steps of each turn will also occur here in some form (not necessarily the actual code but instead the game manager may just call the appropriate method of Galaxy or StarShip).

/* Main program loop inside one of the methods of the game manager */

while (game not over) {

    // Run through the steps of a new turn

}

Likely the manager will be the largest class by far. It will contain many methods and at the very minimum it will have as an attribute an instance of class Galaxy (so when the player makes a selection such as movement direction the galaxy object can be sent a message as to how to update the game world)

SpaceSimulator

This is the ‘Driver’ class that contains the main() method. Likely it contains very little code, enough to instantiate a GameManager and to run a method that starts the game.

main () {

    GameManager aManger = newManager();

    aManager.start();

}

GameStatus

A static class that contains attributes that track the state of the game (at a minimum there will be two attributes that track if debug and cheat mode are on). You will probably want two more attributes that determine if the game has been won or lost. Aside from main() these attributes are the only exceptions to the prohibition on the use of the keyword static.

Using pre-written Java code

You will need to use the code in class Random. Beyond that (and common sense operators and operations such as those for input/output and mathematical operators), unless you are told otherwise, you will need to write your own code and cannot use other pre-written Java classes or operators. Also you need to use the starting code in the A2 directory [starting code]. It's quite minimalistic, the galaxy is initialized and displayed but there is no user interface, however the starting position of the players ship will always be in the same location and the computer ship location is randomized.

UML class diagram

Draw a UML diagram of class Galaxy, class StarShip and the relationship between them (including multiplicity). Each class diagram should show 'increased' details (parameter and return information for each method). You are not required to use any formal UML drawing tools such as [Rational Rose] or [Argo/UML]. Instead you can use any structured drawing program (even something like PowerPoint if you wish).  Diagrams can be hand drawn and scanned (if they are neat and legible). Acceptable file formats for the UML diagram include: gif, pdf, jpg or png.

Submitting your work:

  1. Assignments (source code/'dot-java' files) must be electronically submitted according to [the assignment submission requirements] using [submit]. Also the UML diagram must also be submitted electronically. (Since the showstuff command with the '-v' option doesn't work with non-text files you will have to extra careful that you submitted the correct file - 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 here is a [checklist] of items to be used in marking.

Sample execution

You can see sample output files in the A2 directory (/home/233/assignments/assignment2/output). Note that my version implements a feature that is not required in your program, the attack menu only displays if the ships are adjacent.