Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Language : JAVA Write a class named Ship. Its purpose is to model a ship in the BattleShip game and its placement on the

Programming Language : JAVA

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.

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 +

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.

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.

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() 

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; 

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

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions