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

Write a program with the following specifications: The program should simulate a

ID: 3694595 • Letter: W

Question

Write a program with the following specifications: The program should simulate a one-player game of battleship. The user should be able to specify the board size and the number of ships that will be placed on the board. (Ships should be of varied sizes.) Once the board is generated with random ship placement, the user should be able to specify coordinates and have the game show them whether a "hit" or "miss" has occurred and display the resulting screen. Additional rules may be specified as needed.

Program structural requirements:

The board will be constructed with a dynamic array.

The ships will be constructed with a linked list.

Explanation / Answer

program :

import java.io.File;
import java.io.IOException;
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);
int ammo = Integer.parseInt(args[0]);
int seeds = Integer.parseInt(args[1]);
int boat;
boat = 0;
File file = new File(args[2]);
if (ammo < 0)
{
System.out.println("ERROR - AMMO NOT GREATER THAN ZERO");
}
if (seeds < 0)
{
System.out.println("ERROR - SEED LESS THAN ZERO");
}
if (!args[2].contains(".txt"))
{
System.out.println("ERROR - FILE DOES NOT END WITH .TXT");
}
Scanner sc = null;
try
{
sc = new Scanner(file);
}
catch (IOException e)
{
System.out.println("ERROR - FILE DOES NOT EXIST");
System.exit(0);
}
String line = sc.nextLine();
if (line.contains(", "))
{
String[] parts = line.split(", ");
String rowsize = (parts[0]);
String colsize = (parts[1]);
int rows = Integer.parseInt(rowsize);
int cols = Integer.parseInt(colsize);
char[][] myArray = new char[rows][cols];   
for(int k = 0; k < cols; k++)
{
System.out.print("=");
}
for(int i = 0; i < rows; i++)
{
System.out.println("");
for(int j = 0; j < cols; j++)
{
System.out.print("*");
}
}
System.out.println("");
for(int k = 0; k < cols; k++)
{
System.out.print("=");
}
while(sc.hasNextLine())
{
line = sc.nextLine();
if (line.length() != cols)
{
System.out.println("ERROR - COL MISMATCH");
System.exit(0); }
if (!line.matches("[=*B]+"))
{
System.out.println("ERROR - UNKNOWN SYMBOL");
System.exit(0); }
if (line.indexOf("B") != -1)
{
boat = ++boat;
}
}
if (boat < 1 )
{
System.out.println("ERROR - NO BOAT");
System.exit(0); }
}
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);
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 shoot(int[] shoot)
{
Scanner input = new Scanner(System.in);
System.out.print("Row: ");
shoot[0] = input.nextInt();
shoot[0]--;
System.out.print("Column: ");
shoot[1] = input.nextInt();
shoot[1]--;
}
public static boolean hit(int[] shoot, int[][] ships)
{
for(int ship=0 ; ship<ships.length ; ship++)
{
if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1])
{
System.out.printf("You hit a ship located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
return true;
}
}
return false;
}
public static void hint(int[] shoot, int[][] ships, int attempt){
int row=0,
column=0;
for(int line=0 ; line < ships.length ; line++)
{
if(ships[line][0]==shoot[0])
row++;
if(ships[line][1]==shoot[1])
column++;
}
System.out.printf(" Hint %d: Row %d -> %d ships " +
"Column %d -> %d ships ",attempt,shoot[0]+1,row,shoot[1]+1,column);
}
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
if(hit(shoot,ships))
board[shoot[0]][shoot[1]]=1;
else
board[shoot[0]][shoot[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