Question
The program Problem3Demo.java is provided as a guide to the requirements. Make programs Problem3.java and Grid.java that allow a single person to play the game
The program Problem3Demo.java is provided as a guide to the requirements. Make programs Problem3.java and Grid.java that allow a single person to play the game Battleship. Students who are not familiar with the game can read more here: https://en.wikipedia.org/wiki/Battleship_(game). Include the following features:
a. Create a Grid class with the following public static methods:
A printGrid method receives a 2-D array for the players grid and returns nothing. It displays a text grid using different symbols to show where the user has searched and whether each search coordinate was a hit or miss. Include the row and column indexes with the grid. See sample output below. Use any values and symbols you wish. For example, the array values could be 0=not guessed, 1=hit, -1=miss; and the output symbols could be X=hit and O=miss.
A won method receives two 2-D arrays: one array with the players grid (see printGrid above) and another array of ship locations (for example, 0=no ship, 1=ship). It returns a boolean value of true if the players hits in the 1st array match all of the ship positions in the 2nd array. It returns false otherwise. Note that it is acceptable to use return within a for-loop.
b. Create an application class Problem3.java with a main method to play the game. It should do the following:
Create two arrays: the players grid and the ship locations. Initialize the ship locations using any values you wish. The players grid should have no guesses at the beginning.
Repeatedly ask for row and column indexes for a guess until either: (1) all of the ships have been found, or (2) the user enters a row index that is invalid. The sample output says -1 to quit.
After every guess, display the board using the printGrid method in (a).
After each guess is checked, update the players grid based on whether there is a ship located at the row and column entered by the user. Display Hit or Miss to confirm the result.
Below are examples of possible output using X to indicate a correct guess and O to indicate an incorrect guess. There are two ships: one is at coordinates (1,0) to (1,1), and the other is at coordinates (0,3) to (2,3).
Example #1: The program terminates because the user guesses everything. For columns 2 and 3, each continues from the previous column on the left. User input in bold.
Example #2: The program terminates because the user intentionally enters an invalid row to quit. For columns 2 and 3, each continues from the previous column on the left. Row values greater than 3 would also be invalid.
Problem3Demo.java
public class Problem3Demo {
public static void main(String[] args) {
// Create an array of ship positions (0=empty, 1=ship)
int[][] ships = {{0,0,0,1,0},
{1,1,0,1,0},
{0,0,0,1,0},
{0,0,0,0,0}};
// Create an array of player guesses (0=not guessed, 1=hit, -1=miss)
int[][] player = {{0,0,0,0,0},
{1,0,0,0,0},
{-1,0,0,0,0},
{0,0,0,0,0}};
// Print markers to visualize player's grid
Grid.printGrid(player);
// Check whether the player won the game by hitting all the ship positions
boolean result = Grid.won(player, ships);
}
}
01234 01234 01234 1XX 20 1XXO 20 Enter row (1 to quit):1 Enter row (-1 to quit): 1 Enter column: 0 Hit! Enter column: 2 Miss Enter row (1 to quit): 1 Enter column: 3 Hit! 01234 01234 0 X 1xXOX 20 01234 1X 1XXO 20 Enter row (1 to quit)2 Enter row (-1 to quit): Enter column: 0 Miss Enter column: 3 Hit! Enter row (-1 to quit): 2 Enter column: 3 Hit! 01234 01234 0 X 1xxox 20 X 01234 1X 20 1XXO 20 Enter row (-1 to quit):1 Enter row (1 to quit): o Enter column: 1 Hit! Congratulations! You won! Enter column: 4 MissStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started