Question: JAVA Please edit the part of the code for Java battle ship program. 3. BOARD CLASS Board Class The computer will simulate a rectangular m

JAVA

Please edit the part of the code for Java battle ship program.

3. BOARD CLASS Board Class The computer will simulate a rectangular m n board. A Board object will contain the following: private int num_rows: indicates the number of rows a Board has private int num_columns: indicates the number of columns a Board has private int num_boats: indicates the number of Battleboat objects a Board has private Battleboat[] boats: array of all the Battleboat objects associated with a Board object private Cell[][] board: 2-dimensional Cell array required to represent the Board private boolean debugMode: flag to indicate if Board should be printed in debugMode The minimum Board size is 3 3 and the maximum is 12 12. Assume that the points in the Board range from (0, 0) to (m 1, n 1) inclusive. Each boat is represented by a line of consecutive squares on the board. Boats may not overlap other boats, extend outside the game board, or be placed diagonally. They may be horizontal or vertical. A boat is considered sunk when all the squares of the boat have been hit by the user. Examples: Valid coordinates for a boat of size 3 are {(0, 0), (0, 1), (0, 2)} and {(1, 1), (2, 1), (3, 1)}. Examples of invalid coordinates are {(0, 0), (0, 1)}, which is invalid because there are not enough points. {(0, 0), (0, 2), (0, 3)} is invalid because the points are not consecutive. {(1, 0), (0, 0), (1, 0)} is invalid because the first coordinate is out of bounds. Finally, two boats cannot contain the same point because they cannot overlap. After the gameboard has been sized, the program should place boats randomly on the board. This requires randomly generating a coordinate (x, y) where the boat will be placed as well as randomly choosing whether the boat should be horizontal or vertical. The quantity of boats is defined by the width and height of the game board. As mentioned prior, all boats should be of length 3. Recall the smallestBoardis 3 3 and the largestBoardis 12 12. Smallest Dimension Boat Quantity width == 3 or height == 3 1 3 < width <= 5 or 3 < height <= 5 2 5 < width <= 7 or 5 < height <= 7 3 7 < width <= 9 or 7 < height <= 9 4 9 < width <= 12 or 9 < height <= 12 6

Use the table above to determine how many boats to place. Recall that the Board may be rect- angular, so a Board that is 9 3 should have just one boat of length 3 (the first case). The user should be told how many boats are on the Board when the game begins. Hint: To randomly place a boat, consider the coordinate in the upper left. If the upper left corner was (0, 0), consider how the boat looks if it is horizontal or vertical. What upper left corner coordinates are invalid? The placing of the boats may be the most challenging aspect of this project: see what assumptions you can make to simplify it. Required functions: public Board(int m , int n, boolean debugMode): constructor for theBoardclass. This method should assign appropriate number of boats to num_boats variable, initialize theBoard as a 2-D Cell array, initialize boats as a Battleboat array, place Battleboat objects appro- priately on the Board, and add them to the boards boats public int guess(int r, int c): returns an int based on the guess for the cell. The statuses of the Cell must also be changed according reflect the statuses from the table in the Cell class portion of this writeup public int unsunkBoats(): calculates the number of unsunk boats on the Board

public class Board { private int num_rows; private int num_columns; private int num_boats; private Battleboat[] boats; private Cell[][] board; private boolean debugMode; // TODO: Assign appropriate number of boats to num_boats variable // TODO: Initialize the board as a 2-D Cell array // TODO: Initialize boats as a Battleboat array // TODO: Place Battleboats appropriately on board and add them to the board's boats

public Board(int m , int n, boolean debugMode){ num_rows = m; num_columns = n; num_boats = this.debugMode = debugMode; }

//Obscures a character if the game is not being played in debug mode private char debug(boolean debugMode, char c){ if(debugMode){ return c; } else{ switch(c){ case 'H': c = 'H'; break; case 'M': c = 'M'; break; default: c = ' '; break; } return c; } }

//Prints a Board object in a way that makes sense to the player public String toString(){

String boardString = "\t"; for (int j = 0; j < num_columns-1; j++){ boardString += j + " |" + "\t"; }

boardString += num_columns-1;

for(int i = 0; i < num_rows; i++){ boardString+= " " + i + "\t"; for (int j = 0; j < num_columns; j++){ boardString += debug(debugMode, board[i][j].get_status()) + "\t"; } }

boardString += " "; return boardString; }

// TODO: Return a int based on the guess for the cell/its status // TODO: Change the statuses of the cell if applicable public int guess(int r, int c){ if (){ return 0; //"Penalty: Out of Bounds"; } else if () { return 1; //"Miss"; } else if(){ return 2; //"Hit"; } else { return 3; //"Penalty: Redundant Guess"; } }

//TODO: write a function that calculates the number of unsunk boats public int unsunkBoats(){

} }

public class Battleboat { private int size; private boolean orientation; // false <-> horizontal, true <-> vertical private Cell[] spaces; public Battleboat(){ orientation = Math.floor(Math.random()*2) == 1; size = 3; spaces = new Cell[size]; } public boolean get_orientation(){ return orientation; } public int get_size(){ return size; } public Cell[] get_spaces(){ return spaces; } } 

public class Cell { private int row; private int col; private char status; // ' ': Empty, 'B': Boat, 'H': Hit; 'M': Miss public char get_status(){ return status; } public void set_status(char c){ if(c != 'B' && c != 'H' && c != 'M') { c = ' '; } this.status = c; } public Cell(int row, int col, char status){ this.row = row; this.col = col; set_status(status); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!