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

Make a BattleShip Game. There should be at least 2 ships of any size and the gam

ID: 3765244 • Letter: M

Question

Make a BattleShip Game. There should be at least 2 ships of any size and the game board should be at least 5x5. First the computer will decide where to put it’s two or more ships. Then the user gets to decide where to put his/her two or more ships (or vise versa). Don’t duplicate code for the human and the computer. Now the game starts and the computer randomly chooses a coordinate (like A1) to torpedo making sure the same spot isn’t torpedoed twice. Also if a hit is confirmed, have the computer smart enough to choose the right next move! Next have the user choose a move, making sure that it is a possible move and a move that hasn’t already been played. Be sure to use JOptionPane, Switches, Loops, StringBuilder and arrays. There should be at least three classes created like, GameBoard.java, Player.java and BattleShip.java (what about maybe Ship.java). Make it user friendly with a smart AI!

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[] hit = new int[2];
int chances=0,
shotHit=0;
  
initBoard(board);
initShips(ships);
  
System.out.println();
  
do{
showBoard(board);
hit(hit);
chances++;
  
if(hit(hit,ships)){
hint(hit,ships,chances);
shotHit++;
}
else
hint(hit,ships,chances);
  
changeboard(hit,ships,board);
  

}while(shotHit!=3);
  
System.out.println(" Battleship Java game finished! You hit 3 ships in "+chances+" chances");
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

for(int last=0 ; last < ship ; last++){
if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
do{
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
}while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
}
  
}
}

public static void hit(int[] hit){
Scanner input = new Scanner(System.in);
  
System.out.print("Row: ");
hit[0] = input.nextInt();
hit[0]--;
  
System.out.print("Column: ");
hit[1] = input.nextInt();
hit[1]--;
  
}
  
public static boolean hit(int[] shoot, int[][] ships){
  
for(int ship=0 ; ship<ships.length ; ship++){
if( hit[0]==ships[ship][0] && hit[1]==ships[ship][1]){
System.out.printf("You hit a ship located in (%d,%d) ",hit[0]+1,hit[1]+1);
return true;
}
}
return false;
}

public static void hint(int[] hit, int[][] ships, int attempt){
int row=0,
column=0;
  
for(int line=0 ; line < ships.length ; line++){
if(ships[line][0]==hit[0])
row++;
if(ships[line][1]==hit[1])
column++;
}
  
System.out.printf(" Hint %d: Row %d -> %d ships " +
"Column %d -> %d ships ",attempt,hit[0]+1,row,hit[1]+1,column);
}

public static void changeboard(int[] hit, int[][] ships, int[][] board){
if(hit(hit,ships))
board[hit[0]][hit[1]]=1;
else
board[hit[0]][hit[1]]=0;
}
}

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