Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help intializing the multi-dimensional array below (I\'ve commented the c

ID: 3733313 • Letter: I

Question

I need help intializing the multi-dimensional array below (I've commented the code that I have so far, but not sure what to do). I also need help printing the board and help with another method below that. Encoding is UTF-8. Thanks.

import java.util.Scanner;

import java.util.Random;

public class Battleship {

/**

   * Initializes a game board so that all the entries are Config.WATER_CHAR

(WATER_CHAR is '~')

   *

   * @param board The game board to initialize.

   */

public static void initBoard(char board[][]) {

// for ( int i = 0; i < board.length; i++)

// for ( int j = 0; j < board[ i ].length; j++)

// board[ i ][ j ] = Config.WATER_CHAR;

}

/**

   * 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;

}

Explanation / Answer

###########

/**

   * 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.printf("%-" + Config.MAX_COL_WIDTH + "s", ""+board[i][j]);

           }

           System.out.println();

       }

   }

########

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote