Question
Any help appreciated, in java: As a first step well make a class that models a Ship from both the Player and the Opponents perspective.
Any help appreciated, in java:
As a first step well make a class that models a Ship from both the Player and the Opponents perspective. Like in Paper battleship game(shown in pic)
The following public methods should be supported in Ship:
1. public Ship()
Constructs a ship at Row A and column 1 at direction Direction.HORIZONTAL and length of 2.
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.
3. 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.
4. public void clearDeck()
Clears any hits to any of the squares belonging to the ship. The ship state is now considered not sunk.
5. 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. 6. public boolean isSunk()
If the ships state is sunk, return true. Otherwise, return false.
7. 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. 8. 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. 9. 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.
10. 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.
11. 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.
12. 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)]
13. 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 - - - - - - - - - - "
Steps:
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 - - - - - - - - - -
Additional Files
need the following files in additon to compile your program.
public enum Viewer { PLAYER, OPPONENT; }
public enum Direction { HORIZONTAL, VERTICAL; }
BATTLESHIPS player round 1 2 3 4 5 6 7 8 9 10 YOUR SHIPS SALVO COUNTER tarlara karlalang 2 3 4 5 6 7 8 9 10 mon D - - I ENEMY SHIPS by Samuel Bednar o SHORT RULES FLEET 26 size 5 1. Arrange your ships on "YOUR SHIPS' grid according to "FLEET" table. 2. Toke turns firing a salvo ot your enemy, calling out squares os "A3, B3", etc. Salvo - number of your ships you have left (use counter) / 1 shot (easy). 3. Mark salvos fired on "ENEMY SHIPS"grid marks water, marks hit). You must call out when your ship is sunk completely. 4. Sink 'em all! # 1x 1x 1x 2x 2x ship Aircraft Carrier Battleship Cruiser Destroyer Submarine 2 BATTLESHIPS player round 1 2 3 4 5 6 7 8 9 10 YOUR SHIPS SALVO COUNTER tarlara karlalang 2 3 4 5 6 7 8 9 10 mon D - - I ENEMY SHIPS by Samuel Bednar o SHORT RULES FLEET 26 size 5 1. Arrange your ships on "YOUR SHIPS' grid according to "FLEET" table. 2. Toke turns firing a salvo ot your enemy, calling out squares os "A3, B3", etc. Salvo - number of your ships you have left (use counter) / 1 shot (easy). 3. Mark salvos fired on "ENEMY SHIPS"grid marks water, marks hit). You must call out when your ship is sunk completely. 4. Sink 'em all! # 1x 1x 1x 2x 2x ship Aircraft Carrier Battleship Cruiser Destroyer Submarine 2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started