Documente Academic
Documente Profesional
Documente Cultură
4x
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>
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
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.
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.
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).
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.
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.
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!