Question: This is how your program should behave after it starts. Print out the board with 5 ships randomly placed. Please solve the problem ( s
This is how your program should behave after it starts.
Print out the board with ships randomly placed.
Please solve the problems alone. Write the algorithm before writing the code and include a short writeup of algorithm in a text file or PDF document along with the code. Remember to test your solution thoroughly. Code that does not work correctly or does not compile will lose credit. Please submit a digital copy, by the due date and time, via Canvas. The digital copy of your code should be a zip file containing the project folder named with your last name followed by the project number. For example, canettizip would be my zip file for project Good luck!
In this project, you will implement a variation of the board game Battleship You may have played this game as a kid or an adult In Battleship, ships are placed on a x grid and then you and your opponent take turns calling out a rowcolumn until all the ships on your opponents grid are hit and sunk The first player to sink all of hisher opponent's ships wins.
In this variation, we will use a twodimensional arrayor list to represent your game board, randomly place ships and then simulate an opponent trying to hit your ships by entering one columnrow at a time from the keyboard until all your ships are sunk. For simplicity, each ship will fill a single square on the grid.
You will need to implement the following functions in addition to main:
A function drawBoard that takes one parameter as input: a x arrayor list of chars that represents the board. The function then displays the contents of the arrayor list columns to the right, rows down see my sample output The numbers should be displayed along the top and left sides of the drawing to easily identify each columnrow on the board. Each square should display one of four possible characters:
A dot for any square that does not contain a ship and that has not yet been called out by the user at the keyboardThe letter S to represent one of the ships on the boardThe letter O to represent a rowcolumn that was called out but did not contain a ship ie a missThe letter X to represent a rowcolumn that was called out and there was a ship there ie a hit
A function isGameOver that takes one parameter as input: a x arrayor list of chars that represents the board. The function should return true if all the ships on the board have been hitsunk ie all of the S squares have been replaced by X and false otherwise.
A function setupBoard The function should create a twodimensional arrayor list that represents the board with each square initialized to a dot Then there should be an S for each of the number of ships specified by the NUMSHIPS constant. The ships should be placed on the board randomly using the Random class to pick a random row and column. If the same rowcolumn is picked more than once, the function should continue to loop until all the ships are placed on different squares. So if NUMSHIPS there should be different squares with an S and the rest of the squares should contain a dot character.
A function checkHitOrMiss that takes three parameters as input: a x arrayor list of chars that represents the board, and the column and row. The column and row represent the column and row entered by the user on the keyboard. The function should check that column and row on the board and do the following:
If the square contains a ship S that square on the board should be updated to an X to represent a hit and return the String HITIf the square already contains a sunk ship X simply return the String HITOtherwise that square on the board should be updated to an O to represent a miss and return the String MISS
Your main function should do the following:
Set up the game board
Draw the board and prompt the user to enter a column X
If the column is not a valid column on the board ie less than or greater than or equal to display Invalid column and go back and ask the user to try again without checking the board
Draw the board and prompt the user to enter a row Y
If the row is not a valid row on the board ie less than or greater than or equal to display Invalid row and go back and ask the user to try again without checking the board
If the column and row are valid, check if the columnrow is a hit or miss and print out the result to the user MISS or HIT
Update the board with the appropriate character for the square that was entered OX
If all the ships have been HIT and sunk, draw the board one last time then print GAME OVER! and end the game
Otherwise, loop back to Step and repeat until the game is over. This is what I have so far but Im missing something to make it look like the picture.
How can I add the and to my code to look like the picture?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
