Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

As a first step well make a class that models a Ship from both the Player and the Opponents perspective. You can see the following

As a first step well make a class that models a Ship from both the Player and the Opponents perspective. You can see the following Paper Battleship Game (Links to an external site.) from wikipedia. And, because Im going to test your Ship class by writing my own program that uses your class, Im going to specify the public methods that you must write and how they should behave.

Specification

Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the Battleship board.

Ships exist on a 10 x 10 battleship board with ten rows labeled A through J and columns labeled 1 through 9 and the final column labeled 0 to indicate the tenth column.

We will refer to the Ship placed on the board at the origin which the pair (r,c) where in the below example r is the row D and the column is 4. The example ship extends downward with Direction.VERTICAL with a length of 3. The origin of a Ship is considered at either (1) the left most square with the remainder extending horizontal Direction.HORIZONTAL and to the right, or (2) the top most square with the remainder extending vertical Direction.VERTICAL and down. Ships may not be placed diagonal. At any point, the ship must exist on the grid. The following example perspective is from the Viewer.PLAYER perspective.

 1 2 3 4 5 6 7 8 9 0 A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - o - - - - - - E - - - o - - - - - - F - - - o - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - -

When the opponent fires a shot with the call to the checkShot('E',4) method and it hits the ship, the square at coordinates that is hit changes to a +.

 1 2 3 4 5 6 7 8 9 0 A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - o - - - - - - E - - - + - - - - - - F - - - o - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - -

When the opponent has fired shots that have hit all the squares that belong to the ship, all the ships squares values change to x and the ships state changes to sunk.

 1 2 3 4 5 6 7 8 9 0 A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - x - - - - - - E - - - x - - - - - - F - - - x - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - -

The display of the ship also depends upon the perspective of the viewer. When the ship is viewed by the Viewer.OPPONENT, squares that have not been hit should return -. Any hits should appear as + and all the squares should appear as x for a ship whose state is sunk. The ship shown above with one hit appears as following when the viewer state is set to PLAYER.OPPONENT.

 1 2 3 4 5 6 7 8 9 0 A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - + - - - - - - F - - - - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - 

The following public methods should be supported in Ship:

public Ship() public Ship(char row, int col, Direction d, int l) public void setViewer(Viewer w) public void clearDeck() public char getSquare(char row, int col) public boolean isSunk() public boolean checkShot(char row, int col) public boolean hasSquare(char row, int col) public void setOrientation(Direction newDirection) public void setLength(int newLength) public void setOrigin(char row, int col) public String toString() public String gridView()

public Ship()

Constructs a ship at Row A and column 1 at direction Direction.HORIZONTAL and length of 2.

public Ship(char row, int col, Direction d, int l)

Constructs a ship at the origin specified by row and col with Direction d and length l. If constructor causes the ship or a portion of the ship to be off grid, it should throw an IllegalArgumentException with the message Ship is off grid.

public void setViewer(Viewer w)

This Viewer can be set to either Viewer.PLAYER or Viewer.OPPONENT which affects return values returned for squares that belong to the ship.

public char getSquare(char row, int col)

precondition: the row and col should be in valid coordinate range. If not, throw an IllegalArgumentException with the message Invalid coordinates. Returns the value for the square at coordinates (row,col) for the ship. If the square does not belong to the Ship return -. If the Viewer is set to Viewer.PLAYER and the square belongs to the ship and has not been hit, return o. If the Viewer is set to Viewer.OPPONENT and the square belongs to the ship and has not been hit, return -. If the square belongs to the ship and has been hit and the ship is not sunk, return +. If the square belongs to the ship and has been hit and the ship is sunk, return x.

public boolean isSunk()

If the ships state is sunk, return true. Otherwise, return false.

public boolean checkShot(char row, int col)

precondition: the row and col should be in valid coordinates range. If not, throw an IllegalArgumentException with the message Invalid coordinates. If the ship has the square for (row,col) set it to + and consider its state hit. The return value is true. If all the squares for the ship have been hit, set all the squares state to x and consider the state for the ship as sunk. The return value is true. If the square for (row,col) does not belong to the ship, return false.

public boolean hasSquare(char row, int col)

precondition: the row and col should be in valid coordinate range. If not, throw an IllegalArgumentException with the message Invalid coordinates. Returns true if and only if the square for (row,col) belongs to the ship.

public String toString()

Returns a string representation of the ship in the following format. Below are several examples of three Ships shown from both the Viewer.PLAYER and Viewer.OPPONENT view.

PLAYER[r=C,c=4(+)],[r=D,c=4(o)],[r=E,c=4(o)] OPPONENT[r=C,c=4(+)],[r=D,c=4(-)],[r=E,c=4(-)] OPPONENT[r=C,c=4(x)],[r=D,c=4(x)],[r=E,c=4(x)]

public String gridView()

Returns a string representing the Ship on the Battleship grid depending upon the value of the Viewer state. When the viewer is Viewer.OPPONENT, squares that belong to the Ship and have not been hit show as -. A square that has been hit, shows as + and a square for a ship with all squares hit shows as x. The following is an example return String.

" 1 2 3 4 5 6 7 8 9 0 A - - - - - - - - - - B - - - - - - - - - - C - - - o - - - - - - D - - - o - - - - - - E - - - o - - - - - - F - - - - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - "

You may implement private methods within your Ship class to facilitate your public methods.

Removed methods

The following methods were removed and not required.

public void clearDeck()

Clears any hits to any of the squares belonging to the ship. The ship state is now considered not sunk.

public void setOrientation(Direction newDirection)

Sets the orientation of the ship. If the change in origin causes the any of the corresponding squares belonging to the ship to go off grid, throw an IllegalArgumentException with the message Ship is off grid. Call the clearDeck() method on the ship after changing orientation.

public void setLength(int newLength)

Sets the length of the ship. If the change in origin causes the any of the corresponding squares belonging to the ship to go off grid, throw an IllegalArgumentException with the message Ship is off grid. Call the clearDeck() method on the ship after changing length.

public void setOrigin(char row, int col)

Sets the origin of the ship. If the change in origin causes the any of the corresponding squares belonging to the ship to go off grid, throw an IllegalArgumentException with the message Ship is off grid. Call the clearDeck() method on the ship after changing origin.

Additional Files

You will need the following files in additon to compile your program. Do not submit them.

public enum Viewer { PLAYER, OPPONENT; }
public enum Direction { HORIZONTAL, VERTICAL; }

Have the files Ship.java and TestShip.java to show your unit testing in the build of your ship.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

3rd Edition

0760049041, 978-0760049044

More Books

Students also viewed these Databases questions