Question: JAVA Can somebody help me finish writing my main method?! /** * This is the main method for the Battleship game. It consists of the

JAVA Can somebody help me finish writing my main method?!

/** * This is the main method for the Battleship game. It consists of the main game and play again * loops with calls to the various supporting methods. When the program launches (prior to the * play again loop), a message of "Welcome to Battleship!", terminated by a newline, is * displayed. After the play again loop terminiates, a message of "Thanks for playing!", * terminated by a newline, is displayed. * * The Scanner object to read from System.in and the Random object with a seed of Config.SEED * will be created in the main method and used as arguments for the supporting methods as * required. * * Also, the main method will require 3 game boards to track the play: - One for tracking the * ship placement of the user and the shots of the computer, called the primary board with a * caption of "My Ship". - One for displaying the shots (hits and misses) taken by the user, * called the tracking board with a caption of "My Shots"; and one for tracking the ship * placement of the computer and the shots of the user. - The last board is never displayed, but * is the primary board for the computer and is used to determine when a hit or a miss occurs * and when all the ships of the computer have been sunk. Notes: - The size of the game boards * are determined by the user input. - The game boards are 2d arrays that are to be viewed as * row-major order. This means that the first dimension represents the y-coordinate of the game * board (the rows) and the second dimension represents the x-coordinate (the columns). * * @param args Unused. */ public static void main(String[] args) {

System.out.println("Welcome to Battleship!"); Scanner sc = new Scanner(System.in); Random rand = new Random(Config.SEED);

int boardHeight = 0; int boardWidth = 0; char[][] myShip; char[][] myShot; char[][] lastBoard; char userInput;

do { boardHeight = promptInt(sc, "board height", Config.MIN_HEIGHT, Config.MAX_HEIGHT); boardWidth = promptInt(sc, "board width", Config.MIN_WIDTH, Config.MAX_WIDTH); System.out.println();

myShip = new char[boardHeight][boardWidth]; myShot = new char[boardHeight][boardWidth]; lastBoard = new char[boardHeight][boardWidth];

initBoard(myShip); initBoard(myShot); initBoard(lastBoard);

int id = promptInt(sc, "number of ships", Config.MIN_SHIPS, Config.MAX_SHIPS);

for (int i = 1; i <= id; ++i) { boolean addSuccess = addShip(sc, myShip, myShot, i, rand); if (!addSuccess) { userInput = promptChar(sc, "Error adding ships. Restart game? (y/n): "); break; } } printBoard(myShip, "My Ships"); myShot = new char[boardHeight][boardWidth]; initBoard(myShot); printBoard(myShot, "My Shots"); shootPlayer(sc, myShip, myShot);

userInput = promptChar(sc, "Would you like to play again? (y/n): "); } while (userInput == 'y');

System.out.println("Thanks for playing!"); }

}

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!