Part II -BATTLESHIP For this part of the lab you will be creating a one-player B
ID: 3678083 • Letter: P
Question
Part II -BATTLESHIP For this part of the lab you will be creating a one-player Battleship game played on a board that is nted as a vector. Your vector should begin containing all 'O' (capital O) characters. When the game starts, the player should see a menu asking them if they would like to play an Easy, Medium, or Hard game. Their selection affects (1) the size of the board and (2) how many boats are hidden What level would you Bke to play? Board Size Boats Level Easy Medium Hard 10 15 20 Easy Medium Hard After the user selects their level, you will need to (1) create the board and (2) place the boasts on the board Placing the boats should be done by generating a random index at which to "hide" each boat. You will need to make sure that there is at most one boat per square (there cannot be two boats on the same square) Once the boats are placed, you can play the game! The player gets one guess per turn and each guess can result in the following outcomes: The guess is out of bounds of the board: you print a message indicating this and the player loses their turr The guess is a repeat of a square that has already been guessed: print a message indicating this and the player loses their turn The guess is a miss: print a message indicating this and change the character stored in the board vector at the index of the guess from an 'O' to an X' . The guess is a hit: print a message indicating this and change the character stored in the board vector at the index of the guess to a you are playing with more than one boat, your program should also print a message indicating how many more boats are still hiddenExplanation / Answer
solution: Hope It will helps you to understand the logic of battleship.
Code to generate Battleship game in java.
Undestanding logic of battleship game:
Initially, the variables are created 'board[5][5]', which will store the game board, the variable 'ships[3][2]', which will store the position (row and column) of the 3 hidden ships on the board, the variable 'shot[2]' that will store the position (row and column) of the shot that the player will take each round, plus the variable 'attempt', which will store the number of attempts the player has to hit the 3 ships and, finally, the variable 'shotHit' that counts the number of ships you hit.
Method 'initBoard()' is triggered, to create the board with the number '-1' in all positions.
Once the method is 'initShips()', which will fill the position of 3 ships (row and column).
This method draws randomly two numbers between 0 and 4. Then it checks if this number is out because no two boats should be placed at the same position.
If it this position have already chosen, the method enter in a do ... while loop that will randomly select the numbers, and only comes out when draws another pair of numbers that is not the location of some ship.
After that, in 'main()', we start the game.
The games begin using a do ... while loop. In this case, the condition of the loop is "shotHit! = 3 '. That is, until you hit the 3 ships, the game goes on.
The first thing that occurs in the loop is to show the board, through the loop 'showBoard()'.
This method checks each position of the 'board[5][5]'.
If -1 ,it exhibits water, '~'.
If 0, it displays the shot was given and missed, '*', and if it is 1, it displays 'X' indicates that you hit a ship that position.
After showing the board, you will give your shot through the method 'shoot()', which takes two integers.
Note that the user will enter numbers from 1 to 5, because it counts from 1 to 5.
You, as a Java programmer, that counts from 0 to 4.
Therefore, after the user enters the row and column, SUBTRACT 1 of each of these value.
That is, if user has entered (1.1) in its Java board 5x5, it will represents the position (0.0).
After the shot was given, our game in Java will check if that shot hit a ship. This is done with the method 'hit()', which returns 'true' if set and 'false' case err.
In the method, it simply checks if the pair of values - 'shooting[2]' - that define your shot, hit with some of the values that define the position of ships - ships[3] [2].
If hit, the value of 'shotHit' increases.
Hitting or missing, 'attempts' increases, because an attempt was made.
Hitting or missing, a tip is also displayed. This tip is displayed through the method 'hint()', which will look at your 'shot[2]', and look up the row and column you tried to ship some over there, picking the 'ships[3][2] '.
Note that the row of your shot is 'shot [0]' and the column is 'shot [1]'.
The line of each ship is 'ships[x][0]' column and each ship is 'ships[x][1]', where 'x' is the number of the ship. In our case, are three ships, ie ranging from 0 to 2.
Hitting or missing, the board will be changed. The shot you took that spot will change the board, will appear as shot '*' or shot that hit 'X'.