Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA - I need assistance completing these methods for a game of battleship /** * Determines if a sequence of cells of length len in

JAVA - I need assistance completing these methods for a game of battleship

/**

* Determines if a sequence of cells of length len in a game board is clear or not. This is used

* to determine if a ship will fit on a given game board. The x and y coordinates passed in as

* parameters represent the top-left cell of the ship when considering the grid.

*

* @param board The game board to search.

* @param xcoord The x-coordinate of the top-left cell of the ship.

* @param ycoord The y-coordinate of the top-left cell of the ship.

* @param len The length of the ship.

* @param dir true if the ship will be vertical, otherwise horizontal

* @return 1 if the cells to be occupied by the ship are all Config.WATER_CHAR, -1 if the cells

* to be occupied are not Config.WATER_CHAR, and -2 if the ship would go out-of-bounds

* of the board.

*/

public static int checkWater(char board[][], int xcoord, int ycoord, int len, boolean dir) {

// FIXME

return 0;

}

/**

* This method interacts with the user to place a ship on the game board of the human player and

* the computer opponent. The process is as follows: 1 - Print the user primary board, using the

* printBoard. 2 - Using the promptChar method, prompt the user with "Vertical or horizontal?

* (v/h) ". A response of v is interpreted as vertical. Anything else is assumed to be

* horizontal. 3 - Using the promptInt method, prompt the user for an integer representing the

* "ship length", where the minimum ship length is Config.MIN_SHIP_LEN and the maximum ship

* length is width or height of the game board, depending on the input of the user from step 1.

* 4 - Using the promptStr method, prompt the user for the "x-coord". The maximum value should

* be calculated based on the width of the board and the length of the ship. You will need to

* use the coordAlphaToNum and coordNumToAlpha methods to covert between int and String values

* of coordinates. 5 - Using the promptInt method, prompt the user for the "y-coord". The

* maximum value should be calculated based on the width of the board and the length of the

* ship. 6 - Check if there is space on the board to place the ship. 6a - If so: - Place the

* ship on the board using placeShip. - Then, call placeRandomShip to place the opponents ships

* of the same length. - If placeRandomShip fails, print out the error message (terminated by a

* new line): "Unable to place opponent ship: id", where id is the ship id, and return false. 6b

* - If not: - Using promptChar, prompt the user with "No room for ship. Try again? (y/n): " -

* If the user enters a 'y', restart the process at Step 1. - Otherwise, return false.

*

* @param sc The Scanner instance to read from System.in.

* @param boardPrime The human player board.

* @param boardOpp The opponent board.

* @param id The ship id, assumed to be 1 to 9.

* @param rand The Random object.

* @return true if ship placed successfully by player and computer opponent, false otherwise.

*/

public static boolean addShip(Scanner sc, char boardPrime[][], char boardOpp[][], int id,

Random rand) {

return false;

}

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

Students also viewed these Databases questions

Question

which of the following commands gives the response: A = 8 5 2

Answered: 1 week ago

Question

Describe new developments in the design of pay structures. page 475

Answered: 1 week ago