Sunteți pe pagina 1din 6

CS 180.

4x

Project 16: !Battleship part A

In this Java programming assignment, you will practice using inheritance, abstract classes and polymorphism in order to
build a modern war game called !Battleship (we refer to it as Not Battleship, hence the exclamation point). This
game will resemble the classic two-person strategy game Battleship but will feature more complex behaviors for each
specific Boat type in the game.

We will break up the project into two pieces that will be tested separately. In part A, you will focus on the base Boat
class and how it interacts with the World and Coordinates classes. In part B, you will implement the inheritance
hierarchy, designing the classes and interface that make up the remainder of the game.

World Coordinates

Boat Attacker
PART A <abstract> <interface>

ScoutBoat
<abstract>

Cruiser Submarine AircraftCarrier Destroyer Battleship

I.Design a class called Coordinates in a filed called Coordinates.java. The class will hold two instance variables, two
constructors, and four other methods.

a. Create two private integer variables, x and y. These will represent coordinates within the world map.

b. Write two constructors. The default constructor should set both x and y to 0. The parameterized
constructor should take two integers as arguments and set the instance variables accordingly.
CS 180.4x

c. Write the following methods:


i. Write two accessors (getX and getY), one for each instance variable.
ii. Write a mutator (setCoordinates) that takes two integer arguments and sets the x and y
instance variables accordingly. You will not concern yourself with whether the owner of these
coordinates is out-of-bounds in this method.
iii. Write a toString method that returns the coordinates as a String representation of the current
position on the map. You will report the row in letters and column in numbers. You can assume
there will be no maps created that have more than 26 rows. An example is provided below.

C7 // x = 7, y = 2

II.Design a class called World in a file called World.java. This class will hold one instance variable, eight constants, one
constructor, and eight other methods. Please note that this class will reference the Boat object. You may wish to
work on the Boat class before completing the drawTeamMap function.

a. Create one private instance variable for your class (map) and one public set of static constants (NORTH =
0, NORTHEAST = 1, EAST = 2, SOUTHEAST = 3, SOUTH = 4, SOUTHWEST = 5, WEST = 6, NORTHWEST =
7). The constants can be declared static so that they may be referenced from the World class without
instantiating World. The map variable is a two-dimensional array of Object types. It will be used to keep
track of different boats and other objects in the game. This map will be a square matrix and position
(0,0) is assumed to be the upper left corner of the map, while the position (width, length) is assumed to
be the bottom right corner.

b. Write one constructor method for the World class. This constructor should take two arguments
specifying the width and height of the map. Both inputs have a minimum (4) and maximum (10) value.
Inputs above the maximum and below the minimum should be set to the maximum and minimum
respectively. Use these values to declare a two-dimensional array of Objects with size width and height.
Lastly, initialize all values in the two-dimensional array to null.

c. Write the following methods:


i. Write three accessors. The getWidth accessor should return the number of columns in the map.
The getHeight accessor should return the number of rows in the map. The getOccupant
accessor should take a Coordinates object as input and return the Object in that location of the
map (or null if unoccupied).
ii. The isLocationValid method should take a Coordinates object as an argument and return a
boolean value. If the location is within the boundaries of the map, return true; otherwise, false.
iii. The isLocationOccupied method should take a Coordinates object as an argument and return a
boolean value. Returns true if the location is not null; otherwise, false.
iv. The setOccupant method should take an Object and a Coordinates object as inputs, and return a
boolean value. If unoccupied, place the Object parameter in that location. Returns true if the
location was set; otherwise, false.
CS 180.4x

v. The getAdjacentLocation method takes a Coordinates object and an integer as inputs and
returns a Coordinates object. Returns the Coordinates of the location on the map adjacent to
the one provided and in the direction provided (using the constants defined above). See the
example below.

// assume a World w, and an Object b in position C7


w.getAdjacentLocation(b,World.SOUTHEAST); // returns D8

vi. The drawTeamMap method should take an array of Boat objects and an integer representing
the type of view to display. The method should return a String representing the map of the
board, showing only the items specified by the view. The map should have rows labeled with
capital letters (starting from A), and columns as numbers (starting from 1). The following
rules apply when determining what to draw for each space in the map array.
1. Each space in the map array will be displayed using three characters.
2. The map only shows the parts of the map that are visible by the current teams boats.
3. All occupied visible spaces should display information about the boat in that space
depending on the view parameter (see 6 for more details)
4. All unoccupied visible spaces should display open water (~~~).
5. All invisible spaces (outside a boats vision) should display as the fog of war (###).
6. There are three views that you should code, which follow the specific rules below.
a. When the map is displayed in between player turns (view == 1), all spaces
should be drawn as invisible spots (###).
b. When the user selects the display location option (view == 2), the map should
display a boat using the direction, boat type, and team number (B1). When
implementing direction, note that the Unicode character set has arrows, but
that they may require a change in the character set (UTF-8) used by the terminal
to display properly.
c. When the user selects the display health option (view == 3), the map should
display a boat using health, boat type and team number (3B1).

This is the most complicated method, but one that will be of major importance as we move on
to the second part of the project. See the examples on the following page for more information
on how the map should appear given the different view contexts.
CS 180.4x

@ 1 2 3 4 5 6 7 8 9 10 view == 1
A ##############################
B ##############################
C ##############################
D ##############################
E ##############################
F ##############################
G ##############################
H ##############################
I ##############################
J ##############################

@ 1 2 3 4 5 6 7 8 9 10 view == 2
A ########################~~~~~~ (note: A2 is visible only because
B ########################~~~D1 it is in the range of S1 and C1.
C ######~~~~~~~~~~~~~~~~~~~~~~~~
D ######~~~~~~~~~~~~~~~~~~~~~### The C1 boats vision (3) is
E ######~~~~~~~~~~~~~~~~~~~~~### displayed in green.
F ~~~~~~A2~~~~~~C1~~~~~~~~~###
G ~~~~~~~~~~~~~~~~~~~~~~~~~~~### The S1 boats vision (2) is
H ~~~S1~~~~~~A1~~~~~~~~~~~~### displayed in blue.
I ~~~~~~~~~~~~~~~~~~~~~~~~~~~###
J ~~~~~~~~~~~~B1~~~############

@ 1 2 3 4 5 6 7 8 9 10 view == 3
A ########################~~~~~~ (note: the only difference
B ########################~~~2D1 between view 2 and 3 is the
C ######~~~~~~~~~~~~~~~~~~~~~~~~ display of health instead of
D ######~~~~~~~~~~~~~~~~~~~~~### direction)
E ######~~~~~~~~~~~~~~~~~~~~~###
F ~~~~~~5A2~~~~~~3C1~~~~~~~~~###
G ~~~~~~~~~~~~~~~~~~~~~~~~~~~###
H ~~~3S1~~~~~~5A1~~~~~~~~~~~~###
I ~~~~~~~~~~~~~~~~~~~~~~~~~~~###
J ~~~~~~~~~~~~4B1~~~############
CS 180.4x

III.Design a class called Boat in a file called Boat.java. This class should be declared abstract. It will hold six instance
variables, one constructor, and fourteen other methods.

a. Create six private instance variables for your class (team, location, direction, health, strength, and
vision).
i. The team variable is an integer that refers to the team number of this Boat.
ii. The location variable is a Coordinates object and holds the position of the Boat.
iii. The direction variable is an integer, and refers to the direction the Boat is pointing in currently
(as defined by the constants in the World object).
iv. The health variable is an integer that holds the number of hits that the Boat can endure before
sinking.
v. The strength variable is an integer that holds the number of hits that the Boat can cause during
an attack.
vi. The vision variable is an integer that holds the number of spaces in any direction that the Boat
can view on the map. Vision is not dependent on which direction the Boat object is facing.

b. Write one constructor for the Boat class. This constructor should take six arguments, one for each of the
instance variables. Set them accordingly (note: you may want to use the methods of Coordinates to
make sure that the x and y are valid).

c. Write the following methods:


i. Write seven accessor methods (getTeam, getLocation, getDirection, getHealth, getStrength,
getVision, and getID). Each should return the data in the appropriate type from the list of
instance variables above, with the exception of getDirection, which should return the symbol
used to refer to the given direction (look for UTF-8 arrows on the Internet). The getID method
should be declared abstract and will return a character that each Boat will use to specify the
type of boat being mapped.
ii. The act method should be declared abstract, taking an integer array representing the users
choices and a World object as parameters. This method will return a String reporting on the
result of the action selected.
iii. The getActions method should be declared abstract. It takes no parameters, and returns a String
representing all of the options available to the Boat.
iv. The move method takes a World object as a parameter and returns a String reporting the results
of the action. First check to see if the Boat can move to the position on the map directly in front
of the Boat (based on the current location and direction). If it is open, set the map at the Boats
current coordinates to null, and then set the map at the destination to the Boat. This method
should then update the Boats location field.

"B1 moves from C2 to D3."


"B1 cannot move to D3 as it is occupied."
"B1 cannot move off the map."
CS 180.4x

v. The turn method takes an integer representing either a left (-1) or right (+1) turn as an
argument and returns a String reporting the results of the action.

"S1 turned left, now facing N"


"S1 turned right, now facing E"

vi. The takeHit method takes an integer representing the strength of the attack and returns a String
reporting on the result of the action. The Boat being attacked should be updated to reflect the
attack, with a change to the health field equal to the strength of the attack. If this value is
negative after the attack, set the value of this Boats health to zero.

"B1 has been sunk!"


"B1 takes 2 damage."

vii. The setLocation method takes a new Coordinates object and returns no value. This method
assumes the input location is valid, and adjusts the current Boats position accordingly.

viii. The toString method returns a String consisting of the ID of the boat and the team (see example
below).
"B1"

IV.Submit the classes you wrote (Coordinates.java, World.java, Boat.java) to Vocareum. Your method signatures
should be exactly as specified in this document. Make sure you test your program outside of Vocareum before
you submit it.

Good luck!

S-ar putea să vă placă și