COSC 1010 Introduction to Software Problem Solving

Fall 2010

Homework Assignment #6

"Talent hits a target no one else can hit;
Genius hits a target no one else can see."
-- Arthur Schopenhauer
Due: Wednesday, October 27th, 11:00am CDT
Submit: Turn in all four of your Java source files using the D2L Dropbox:
Work may be completed in teams. The names of both partners must appear in a comment block at the top of your files for credit to be given.

Target Practice, Part II

Expand upon the target practice game in Project 5 to allow up to five Targets and five Cannons.

You should use arrays to represent your collection of targets and cannons.

The new and improved game should now be command driven, allowing the user to choose from a set of actions: add, fire, and exit.


Prompt the user for what component to add to the game -- either a target or a cannon. Then, enter coordinates as before. Creation of a cannon should now prompt for the number of initial shots to be loaded. Note that cannons can now be positioned at coordinates other than the origin.


The user should be prompted for which cannon to fire; cannons are numbered 0 through 4. After the impact is calculated, check each of the targets for a hit. Since targets could overlap, an impact could hit more than one target.


Use the Java method System.exit(0) to directly exit your program.

Under other circumstances, your program should exit if all of the cannons are empty (and there are more than zero cannons), or when all the targets are hit (and there are more than zero targets).


Given the greater flexibility of this project, it is important to check for a variety of errors in game logic. The following is a list of error messages that can occur; their causes should be self evident.
  • Error: unknown command
  • Error: already have 5 cannons
  • Error: already have 5 targets
  • Error: invalid cannon number
  • Error: cannot add
  • The last error message is for when the command to "add" is followed by something other "cannon" or "target".

    Example Run

    In the examples below, I use text in blue to distinguish the output of the program from the input I typed. This is for purpose of clarity only; your program will not print text in different colors.

    Project 6 - Target Practice, Part II
    Command (add, fire, exit)?
    (cannon, target)? target
    Enter first point for target:
    Y? 100
    Enter second point for target:
    Y? 200
    Target 0 at [(100,100,0):(200,200,0)]

    Cannon status:
    Target status:
      Target 0 at [(100,100,0):(200,200,0)]

    Command (add, fire, exit)?
    (cannon, target)? cannon
    Enter coordinate for new cannon 0:
    Y? 50
    Number of shots? 5
    Cannon 0 at (50,50,0), 5 shots remaining.

    Cannon status:
      Cannon 0 at (50,50,0), 5 shots remaining.
    Target status:
      Target 0 at [(100,100,0):(200,200,0)]

    Command (add, fire, exit)?
    Which cannon? 0
    Firing cannon 0:
    Declination? 70
    Impact at (165,165,0)
    Target 0 hit! Well done.

    Cannon status:
      Cannon 0 at (50,50,0), 4 shots remaining.
    Target status:
      Target 0 at [(100,100,0):(200,200,0)] HIT
    All targets hit. Well done!


  • The Professor has provided a reference implementation for you to compare against. Change the directory on Morbius: cd ~brylow/cosc1010/Demos/Project6/, and run java Project6.

  • Back
    [Revised 2010 Oct 13 11:46 DWB]