This is some method for a battleship game in java. Please help /** * Prints the
ID: 3734419 • Letter: T
Question
This is some method for a battleship game in java. Please help
/**
* Prints the game boards as viewed by the user. This method is used to print the game boards as
* the user is placing their ships and during the game play.
*
* Some notes on the display: - Each column printed will have a width of Config.MAX_COL_WIDTH. -
* Each row is followed by an empty line. - The values in the headers and cells are to be right
* justified.
*
* @param board The board to print.
* @param caption The board caption.
*/
public static void printBoard(char board[][], String caption) {
// FIXME
}
/**
* 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;
}
/**
* Places a ship into a game board. The coordinate passed in the parameters xcoord and ycoord
* represent the top-left coordinate of the ship. The ship is represented on the game board by
* the Character representation of the ship id. (For this method, you can assume that the id
* parameter will only be values 1 through 9.)
*
* @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.
* @param id The ship id, assumed to be 1 to 9.
* @return false if the ship goes out-of-bounds of the board, true otherwise.
*/
public static boolean placeShip(char board[][], int xcoord, int ycoord, int len, boolean dir,
int id)
Explanation / Answer
You need to provide me the full code of this class (also any dependent code if any) to get a more precise solution of this problem. Meanwhile, I am providing a sample solution of this problem based on data available.
CODE
====================
/**
* Prints the game boards as viewed by the user. This method is used to print the game boards as
* the user is placing their ships and during the game play.
*
* Some notes on the display: - Each column printed will have a width of Config.MAX_COL_WIDTH. -
* Each row is followed by an empty line. - The values in the headers and cells are to be right
* justified.
*
* @param board The board to print.
* @param caption The board caption.
*/
public static void printBoard(char board[][], String caption) {
System.out.println(caption);
for(int i=0; i<board.length; i++) {
for(int j=0;j<board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
/**
* 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) {
int status = -2;
if(dir) {
for(int i=0; i<board.length; i++) {
int count1 = 0, count2 = 0;
boolean water = true;
for(int j=0; j<board[i].length; j++) {
if(board[j][i] == Config.WATER_CHAR)
count1 ++;
if(board[j][i] != Config.WATER_CHAR)
count2 ++;
}
if(count1 == board.length && count1 >= len) {
status = 1;
} else if(count2 == board.length && count2 >= len) {
status = -1;
}
}
} else {
for(int i=0; i<board.length; i++) {
int count1 = 0, count2 = 0;
boolean water = true;
for(int j=0; j<board[i].length; j++) {
if(board[i][j] == Config.WATER_CHAR)
count1 ++;
if(board[i][j] != Config.WATER_CHAR)
count2 ++;
}
if(count1 == board[i].length && count1 >= len) {
status = 1;
} else if(count2 == board[i].length && count2 >= len) {
status = -1;
}
}
}
return status;
}
/**
* Places a ship into a game board. The coordinate passed in the parameters xcoord and ycoord
* represent the top-left coordinate of the ship. The ship is represented on the game board by
* the Character representation of the ship id. (For this method, you can assume that the id
* parameter will only be values 1 through 9.)
*
* @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.
* @param id The ship id, assumed to be 1 to 9.
* @return false if the ship goes out-of-bounds of the board, true otherwise.
*/
public static boolean placeShip(char board[][], int xcoord, int ycoord, int len, boolean dir,
int id) {
if(checkWater(board, xcoord, ycoord, len, dir) != -2)
return true;
return false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.