Question: Please write in Java format. For this assignment, you will use the concept of multi - dimensional arrays and nested for loops to write a

Please write in Java format. For this assignment, you will use the concept of multi
-
dimensional arrays and nested for loops to write a program that can store and update the board for a game of battleship. There is some example code in the Code Samples module to help you use multi
-
dimensional arrays.
Your program must implement the following methods:
createBoard: This method will create a
2
D array of size
2
0
by
2
0
and initialize the values to zeros, such as this;
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
.
.
.
placeShip: This method will accept as parameters the starting coordinates of the ship, the type, and the direction.
(
vertical or horizontal
)
Then it will place the ship on the board by updating the array values to be dash characters
"
-
"
The ships:
One Battleship of length
8
One destroyer of length
2
Two cruisers of length
4
each
(
Tip: start by simply placing a dash at the given coordinate, then add code for the different ship types
/
lengths and orientations
)
fire: This method will accept two coordinates from the user. If there is a ship at that location, it will update the value to
"
X
"
for a hit. If there is no ship, it will update the character to
"
?
"
for miss.
printBoard: This method will output the contents of the board. Where there are ships, the dash character will be printed, where there are no ships, the zero character will be printed, as well as symbols for hits and misses.
A few exception cases to watch for:
If a ship starting coordinate is placed too close to the edge of the array, it will not fit
(
it would run off the end of the board
)
Ships should not collide
/
overlap

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 Programming Questions!