Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Over the semester we are going to make a Battleship game. If we have time, we will ultimately make it graphical, but before we do

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Over the semester we are going to make a Battleship game. If we have time, we will ultimately make it graphical, but before we do that we will make a version that uses the console for input and output. As a first step we'll make a class that models a Ship from both the player and the Opponent's perspective. You can see the following Paper Battleship Game e from wikipedia. And, because I'm going to test your Ship class by writing my own program that uses your class, I'm 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. and columns labeled 1 through 9 and Ships exist on a 10 x 10 battleship board with ten rows labeled A through the final column labeled to indicate the tenth column. ne We will refer to the Ship placed on the board at the origin which the pair Cr, 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 + OOO IIIIIIIII HITIMO i III III 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 1 0 + CHIOTM on> When the opponent has fired shots that have hit all the squares that belong to the ship, all the ship's squares values change to x and the ship's state changes to sunk. 1 2 3 4 5 6 7 8 9 0 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: A public Ship public Ship(char row, int col, Direction d, int 1) public void setViewer(Viewer w) public void clearDeckO 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 I) Constructs a ship at the origin specified by row and col with Direction d and length I. 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 void clearDeck() Clears any hits to any of the squares belonging to the ship. The ship state is now considered not sunk. 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 is Sunk() If the ship's 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 Illegal ArgumentException with the message Invalid coordinates . If the ship has the square for Crow,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 Crow,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 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 Crow,col) belongs to the ship. 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 Illegal ArgumentException 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 Illegal ArgumentException 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 clearDecko method on the ship after changing origin. 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=400)],[r=E,C=4(0)] 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 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. "12 3 4 5 6 7 8 9 0 A ---------- B ---------- C ---0------ D --------- E ---0------ F- -- - G - H - - l - - " -. . . - " You may implement private methods within your ship class to faciltate your public methods. 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; What to submit Submit the files Ship.java and TestShip.java to show your unit testing in the build of your ship. Testing You should test your program thoroughly. Write a program that tests every functionality described in this specification and try to discover inputs that might give your code trouble. Continue to do so until you are convinced that your program is robust and behaves exactly as your javadoc and this specification says. That's what I will do and if your program fails any of my tests, you will get no credit for your initial submission. Over the semester we are going to make a Battleship game. If we have time, we will ultimately make it graphical, but before we do that we will make a version that uses the console for input and output. As a first step we'll make a class that models a Ship from both the player and the Opponent's perspective. You can see the following Paper Battleship Game e from wikipedia. And, because I'm going to test your Ship class by writing my own program that uses your class, I'm 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. and columns labeled 1 through 9 and Ships exist on a 10 x 10 battleship board with ten rows labeled A through the final column labeled to indicate the tenth column. ne We will refer to the Ship placed on the board at the origin which the pair Cr, 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 + OOO IIIIIIIII HITIMO i III III 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 1 0 + CHIOTM on> When the opponent has fired shots that have hit all the squares that belong to the ship, all the ship's squares values change to x and the ship's state changes to sunk. 1 2 3 4 5 6 7 8 9 0 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: A public Ship public Ship(char row, int col, Direction d, int 1) public void setViewer(Viewer w) public void clearDeckO 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 I) Constructs a ship at the origin specified by row and col with Direction d and length I. 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 void clearDeck() Clears any hits to any of the squares belonging to the ship. The ship state is now considered not sunk. 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 is Sunk() If the ship's 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 Illegal ArgumentException with the message Invalid coordinates . If the ship has the square for Crow,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 Crow,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 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 Crow,col) belongs to the ship. 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 Illegal ArgumentException 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 Illegal ArgumentException 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 clearDecko method on the ship after changing origin. 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=400)],[r=E,C=4(0)] 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 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. "12 3 4 5 6 7 8 9 0 A ---------- B ---------- C ---0------ D --------- E ---0------ F- -- - G - H - - l - - " -. . . - " You may implement private methods within your ship class to faciltate your public methods. 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; What to submit Submit the files Ship.java and TestShip.java to show your unit testing in the build of your ship. Testing You should test your program thoroughly. Write a program that tests every functionality described in this specification and try to discover inputs that might give your code trouble. Continue to do so until you are convinced that your program is robust and behaves exactly as your javadoc and this specification says. That's what I will do and if your program fails any of my tests, you will get no credit for your initial submission

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions