Write a program in java in which you will build theSink theS
Write a program in java in which you will build the“Sink theShipsGame”. The Game has a8 x 8 grid and threeships. Each ship takes up exactly three cells. In the game, it’s you against thecomputer, but unlike the real battleship game, you don’t place any ships on yourown. Instead, your job is to sink the computer’s ship in the fewest number of guess.
Goal:Sink all the computer’s ships(three in this case)in the fewest number ofguesses. You’re given a rating or level, based on how well you perform.Setup:When the game program is launched, the computerplaces three ships on a8x 8 grid(for an example, see the figure below).The ships can be placed eithervertically or horizontally (no other placement is valid)on empty cells of the grid.When that’scomplete the game asks for your first guess.
The computer will prompt you to enter a guess(a cell), that you will type at the command-line as “1 3”(where “1 3” means 3rdcell ofthe 1strow), “45”, etc.. In response to your guess, you will see a result at thecommand line,either “Hit”, “Miss”, “Kill” (or whatever the lucky battleship of the dayis!). When you have sank all three battleships, the game ends by printing out your rating.
Here is a sample run of the game:
 Your goal is to sink three ships.
 Battleship1, Battleship2, Battleship3
 Try to sink them all in the fewest number of guesses
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 3
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 2 1
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 2 2
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 1
 kill
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 3
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 7
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 3
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 4
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 5
 kill
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 5 1
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 61 1
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1
 miss
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 4
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 5
 hit
 Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 6
 kill
 All Ships are sank!
 It only took you 18 guesses.
 You got out before your options sank.
When player can finish the game within 20 guesses, print the following message
 (where xx is the number of guesses):
 All Ships are sank!
 It only took you XX guesses.
 You got out before your options sank.
 If it takes more than 20 guesses, print the following message
 All Ships are sank!
 Took you long enough. XX guesses.
 Fish are dancing with your options.
The java program should terminate after 25 guesses and it should reveal the
 positions of the ships and their status.
Solution
Please follow the code and comments for description :
CODE :
import java.util.Random; // required imports for the code
 import java.util.Scanner;
public class SinktheShipsGame { // class to run the code
public static void main(String[] args) { // driver method
 int[][] board = new int[8][8]; // required initialisations
 int[][] ships = new int[3][2];
 int[] shoot = new int[2];
 int guesses = 0, shotHit = 0;
   
boardPlacement(board); // calling the method to print the initial board
 shipsPlacement(ships); // method to print the ships and their repsective placements
   
 System.out.println();
do {
 showTheBoard(board); // print the board to console
 shoot(shoot); // checking the shot from the user
 guesses++; // incrementing the guesses
   
 if (hit(shoot, ships)) { // checking the shot given by the user
   
 System.out.printf(\"You hit a ship.!\ \"); // print the result
 shotHit++; // incrementing the shot hits
 } else {
 System.out.printf(\"You Missed a ship.!\ \");
 //hint(shoot, ships, guesses);
 }
 changeTheBoard(shoot, ships, board); // updating the board with the present rsult
   
 if(guesses >= 25) { // checking for the guess count
 break; // breaking the loop if is greater than 25
 }
} while (shotHit != 3); // iterating the loop till all the 3 ships are shot
 if(guesses < 20){ // print the result based on the guess count
 System.out.println(\"\ \ \ All Ships are sank!\ It only took you \" + guesses + \" guesses.\ You got out before your options sank.\");
 } else if (guesses >= 20) {
 System.out.println(\"\ \ \ All Ships are sank!\ Took You Long Enough. \" + guesses + \" guesses.\ Fish are dancing with your options.\");
 } else if (guesses > 25) {
 System.out.println(\"You Lost.!\ The Result is : \");
 showTheBoard(board);
 }
 }
public static void boardPlacement(int[][] board) { // method to place the board and the results cells
 for (int row = 0; row < 8; row++) {
 for (int column = 0; column < 8; column++) {
 board[row][column] = -1; // returns the desired values
 }
 }
 }
public static void showTheBoard(int[][] board) { // method to shoe the initial or the resultant board
 System.out.println(\"\ \\t1 \\t2 \\t3 \\t4 \\t5 \\t6 \\t7 \\t8\");
 System.out.println();
for (int row = 0; row < 8; row++) { // iterating over the rows and columns
 System.out.print((row + 1) + \"\");
 for (int column = 0; column < 8; column++) {
 if (board[row][column] == -1) {
 System.out.print(\"\\t\" + \"~\"); // print the respective results
 } else if (board[row][column] == 0) {
 System.out.print(\"\\t\" + \"*\");
 } else if (board[row][column] == 1) {
 System.out.print(\"\\t\" + \"X\");
 }
 }
 System.out.println();
 }
}
public static void shipsPlacement(int[][] ships) { // initial placement of the ships
 Random random = new Random();
for (int ship = 0; ship < 3; ship++) { // iterating over the ships count
 ships[ship][0] = random.nextInt(8); // getting the random numnber for the cell value
 ships[ship][1] = random.nextInt(8);
for (int last = 0; last < ship; last++) { // checking for the last placement of the cell
 if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1])) {
 do {
 ships[ship][0] = random.nextInt(8);
 ships[ship][1] = random.nextInt(8);
 } while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));
 }
 }
 }
 }
public static void shoot(int[] shoot) { // method that checks and updates the shoot value
 Scanner input = new Scanner(System.in);
 System.out.printf(\"Enter a guess [row (1 to 8)] [col (1 to 8)] : \");
   
 shoot[0] = input.nextInt(); // getting the values form the user
 shoot[0]--;
   
 shoot[1] = input.nextInt();
 shoot[1]--;
}
public static boolean hit(int[] shoot, int[][] ships) { // checking the method to see if the hit is a success or not
for (int ship = 0; ship < ships.length; ship++) {
 if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1]) {
 return true;
 }
 }
 return false;
 }
   
 public static void changeTheBoard(int[] shoot, int[][] ships, int[][] board) { // method that changes the board
 if (hit(shoot, ships)) {
 board[shoot[0]][shoot[1]] = 1; // updating the values
 } else {
 board[shoot[0]][shoot[1]] = 0;
 }
 }
 }
 OUTPUT :
    1    2    3    4    5    6    7    8
1   ~   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   ~   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   ~   ~   ~   ~
 5   ~   ~   ~   ~   ~   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   ~   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 1
 1
 You Missed a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   ~   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   ~   ~   ~   ~
 5   ~   ~   ~   ~   ~   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   ~   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 3
 1
 You hit a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   ~   ~   ~   ~
 5   ~   ~   ~   ~   ~   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   ~   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 5
 5
 You Missed a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   ~   ~   ~   ~
 5   ~   ~   ~   ~   *   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   ~   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 7
 7
 You Missed a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   ~   ~   ~   ~
 5   ~   ~   ~   ~   *   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   *   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 4
 5
 You hit a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   ~
 4   ~   ~   ~   ~   X   ~   ~   ~
 5   ~   ~   ~   ~   *   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   *   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 3
 8
 You Missed a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   *
 4   ~   ~   ~   ~   X   ~   ~   ~
 5   ~   ~   ~   ~   *   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   *   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 1
 1
 You Missed a ship.!
1 2 3 4 5 6 7 8
1   *   ~   ~   ~   ~   ~   ~   ~
 2   ~   ~   ~   ~   ~   ~   ~   ~
 3   X   ~   ~   ~   ~   ~   ~   *
 4   ~   ~   ~   ~   X   ~   ~   ~
 5   ~   ~   ~   ~   *   ~   ~   ~
 6   ~   ~   ~   ~   ~   ~   ~   ~
 7   ~   ~   ~   ~   ~   ~   *   ~
 8   ~   ~   ~   ~   ~   ~   ~   ~
 Enter a guess [row (1 to 8)] [col (1 to 8)] : 6
 6
 You hit a ship.!
All Ships are sank!
 It only took you 8 guesses.
 You got out before your options sank.
 Hope this is helpful.






