Create a program that allows the user to input coordinates o

Create a program that allows the user to input coordinates onto a 5x5 2D list in order to locate and destroy a hidden enemy ship. Make a game board similar to the one below:

A B C D E

1 - - - - -

2 - - - - -

3 - - - - -

4 - - - - -

5 - - - - -

The game begins with a blank board. Randomly create the location of a 2x2 enemy ship but do not display it on the board (Hint: create a list that stores the location of the upper left corner of the ship, you can calculates the location of the rest of the ship from that). The user fires a shot by specifying the row (1-5) and column (A-E) values. Check to make sure that the input is a valid entry, within the bounds of the list, and that it is not a location that has already been chosen. If the shot hits the ship then display a ‘*’ symbol in that location to designate a ‘hit’. If the shot does not hit the ship, then display an ‘x’ in that location to designate a ‘miss’. When the user has hit all four spots that the ship occupies then the user has won. Reset the board and re-randomize the ship so that the user can play again. Give the user the option to give up, which reveals the location of the enemy ship. Once the user has seen the solution, you should reset the game. Create the following functions at the top of your program:

1. displayBoard – display the board with row and column headings.

2. resetBoard – clear the board and reset position of ship [(0-3),(0-3)].

3. displayMenu – display the menu.

4. getMenuInput – get the menu input, return the valid input.

5. getRow/getColumn – get the row/col input, return valid input (0-4). 6. fireShot – using the user’s input, and ship location, place an x or * on the board. Check all user inputs for validity. Do not create global variables, instead, pass the lists and other variables to functions as needed, return values back to where they were called.

Solution

import java.util.Scanner; public class assignment17 { static Scanner in = new Scanner(System.in); public static void main(String[] args) { System.out.println(\" A B C D E\"); System.out.println(\" 1 - - - - -\"); System.out.println(\" 2 - - - - -\"); System.out.println(\" 3 - - - - -\"); System.out.println(\" 4 - - - - -\"); System.out.println(\" 5 - - - - -\"); final int ROWS = 6; final int COLS = 6; char [] [] values = { {\' \', \'A\', \'B\', \'C\', \'D\', \'E\'}, {\'1\', \'-\', \'-\', \'-\', \'-\', \'-\'}, {\'2\',\'-\', \'-\', \'-\', \'-\', \'-\'}, {\'3\',\'-\', \'-\', \'-\', \'-\', \'-\'}, {\'4\',\'-\', \'-\', \'-\', \'-\', \'-\'}, {\'5\',\'-\', \'-\', \'-\', \'-\', \'-\'}}; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { System.out.print(\" \" + values[row][col]); } System.out.println(\"\ \"); } final int shipROW = 2; final int shipCOL = 2; int [] [] shipValues = new int [(int)Math.random() * values.length][(int)Math.random() * values[0].length]; System.out.println(\"Enter a Row Number: \"); int guessRow = in.nextInt(); System.out.println(\"Enter a Column Letter: \"); int guessColumn = in.nextInt(); //I am having problem with this if statement if ([guessRow][guessColumn] = shipValues){ values [guessRow][guessColumn] = \'*\'; System.out.println(\"hit\"); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { System.out.print(\" \" + values[row][col]); } System.out.println(\"\ \"); } }else{ values [guessRow][guessColumn] = \'x\'; System.out.println(\"miss\"); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { System.out.print(\" \" + values[row][col]); } System.out.println(\"\ \"); } } } }
Create a program that allows the user to input coordinates onto a 5x5 2D list in order to locate and destroy a hidden enemy ship. Make a game board similar to t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site