JAVA - I need assistance completing these methods for a game of battleship /** *
ID: 3734151 • Letter: J
Question
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;
}
Explanation / Answer
public static int checkWater(char board[][], int xcoord, int ycoord, int len, boolean dir)throws ArrayIndexOutOfBoundsException
{
int canPlace = 1;
if (dir)
{
for(int i = ycoord; i < ycoord+len; i++)
{
try
{
if (board[i][xcoord] != Config.WATER_CHAR)
{
canPlace = -1;
}
}
//If we go out of bounds of the board an exception will be thrown
catch (ArrayIndexOutOfBoundsException e)
{
canPlace = -2;
}
}
}
else
{
for(int i = xcoord; i < xcoord+len; i++)
{
try
{
if (board[ycoord][i] != Config.WATER_CHAR)
{
canPlace = -1;
}
}
catch (ArrayIndexOutOfBoundsException e)
{
canPlace = -2;
}
}
}
return canPlace;
}
#################
public static boolean addShip(Scanner sc, char boardPrime[][], char boardOpp[][], int id, Random rand)
{
printBoard(boardPrime);
char ch = promptChar("Vertical or horizontal? (v/h) ");
boolean dir = ch == 'v' ? true : false;
if (dir)
{
int len = promptInt("Enter the length (" + Config.MIN_SHIP_LEN + " to " + boardPrime.length + "): ");
}
else
{
int len = promptInt("Enter the length (" + Config.MIN_SHIP_LEN + " to " + boardPrime[0].length + "): ");
}
String xcoordS = promptStr("Enter x-coordinate of the ship (0 to " + (board[0].length-len) + "): ");
int xcoord = coordAlphaToNum(xcoordS);
int ycoord = promptInt("Enter y-coordinate of the ship (0 to " + (board.length-len) + "): ");
boolean canPlace = checkWater(boardPrime, xcoord, ycoord, len, dir);
if (canPlace)
{
placeShip(boardPrime, xcoord, ycoord, len, dir, id);
boolean canPlaceOpp = placeRandomShip(boardOpp, xcoord, ycoord, len, dir, id);
if (!canPlaceOpp)
{
System.out.println("Unable to place ship of id: " + id);
return false;
}
}
else
{
System.out.println("No room for ship");
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.