Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simple Battleship You will make a game similar to the classic board game Battles

ID: 3689623 • Letter: S

Question

Simple Battleship

You will make a game similar to the classic board game Battleship. You will set up a 5 x 5, 2 dimensional array. In that array, you will use a random number generator to select 5 elements that will act as the placeholders for your "battleships". Your user will get 10 guesses to "seek and destroy" the battleships. After their 10 guesses, you will tell them how many ships they found. At some point during the program, you will need to display the game board (this is to assess your ability to traverse a 2 dimensional array).

Sample output (your program may vary in approach):

_______________________________

Welcome to Battleship! You have 10 chances to sink the 5 Battleships.

Enter a coordinate: 2 3
That is a miss!
Enter a coordinate: 4 1
That is a hit! There are 4 remaining ships.
Enter a coordinate: 5 5
That is a miss!
Enter a coordinate: 3 2
That is a miss!
Enter a coordinate: 1 1
That is a hit! There are 3 remaining ships.
Enter a coordinate: 2 4
That is a miss!
Enter a coordinate: 3 5
That is a miss!
Enter a coordinate: 4 5
That is a miss!
Enter a coordinate: 1 3
That is a hit! There are 2 remaining ships.
Enter a coordinate: 3 3
That is a miss!
You hit 3 out of 5 ships.
------------------------------------
2 0  2 0 0
0 0 -1 -1 0
0 -1 -1  1 -1
2 0 0 -1 0
0 0 0  1 -1

2 = Hit, 1 = Ship, -1 = Missed shot

Explanation / Answer

import java.util.Random; import java.util.Scanner; public class battleShip { public static void main(String[] args) { int[][] board = new int[5][5]; int[][] ships = new int[3][2]; int[] shoot = new int[2]; int attempts=0, shotHit=0; initBoard(board); initShips(ships); System.out.println(); do{ showBoard(board); shoot(shoot); attempts++; if(hit(shoot,ships)){ hint(shoot,ships,attempts); shotHit++; } else hint(shoot,ships,attempts); changeboard(shoot,ships,board); }while(shotHit!=3); System.out.println(" Battleship Java game finished! You hit 3 ships in "+attempts+" attempts"); showBoard(board); } public static void initBoard(int[][] board){ for(int row=0 ; row < 5 ; row++ ) for(int column=0 ; column < 5 ; column++ ) board[row][column]=-1; } public static void showBoard(int[][] board){ System.out.println(" 1 2 3 4 5"); System.out.println(); for(int row=0 ; row < 5 ; row++ ){ System.out.print((row+1)+""); for(int column=0 ; column < 5 ; column++ ){ if(board[row][column]==-1){ System.out.print(" "+"~"); }else if(board[row][column]==0){ System.out.print(" "+"*"); }else if(board[row][column]==1){ System.out.print(" "+"X"); } } System.out.println(); } } public static void initShips(int[][] ships){ Random random = new Random(); for(int ship=0 ; ship < 3 ; ship++){ ships[ship][0]=random.nextInt(5); ships[ship][1]=random.nextInt(5); //let's check if that shot was already tried //if it was, just finish the do...while when a new pair was randomly selected for(int last=0 ; last
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote