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

o The computer must create at least 3 ships. o The ships cannot all be the same

ID: 3747733 • Letter: O

Question

o The computer must create at least 3 ships. o The ships cannot all be the same size o The ships must be randomly placed within the array Provide information to the user about the number of hits and misses. Provide a message about whether each guess is a hit or miss. o If the user guesses a number outside of the acceptable range. provide an error message. o If the user hits all positons on all ships, the user wins. oIf the user gets more than the maximum number of allowed misses, the user looses. o After the user wins or loses, ask them if they want to play again.

Explanation / Answer

Here you go!

"battleshipMain.java"

package battleship.com;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Scanner;

import java.util.concurrent.ThreadLocalRandom;

public class battleshipMain {

public static void main(String[] args) {

//declaring ship sizes variables

int[] A = new int[3];

int[] Pos = new int[3];

ArrayList<Integer> list = new ArrayList<Integer>();

for (int i=1; i<11; i++) {

list.add(new Integer(i));

}

boolean playAgain = true;

String playAg;

Scanner input = new Scanner(System.in);

while (playAgain == true)

{

Collections.shuffle(list);

for (int i=0; i<3; i++) {

A[i] = list.get(i);

//System.out.println(A[i]);

}

  

///////for A[1]

//declaring minimum and maximum limits

int min = 0;

int max = 50;

int intArray[] = new int [51];

// initialize to zero

for (int p : intArray)

{

p=0;

}

int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1 - A[0]);

int pos1 = randomNum;

//System.out.println("pos1="+ pos1);

for (int q=randomNum;q<A[0]+randomNum; q++)

{

intArray[q] = 1;

}

int pos2 = 0;

int pos3 = 0;

if ((pos1 > A[1]))

{

randomNum = ThreadLocalRandom.current().nextInt(min, pos1 + 1 - A[1]);

pos2 = randomNum;

if (pos2 >A[2])

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(min, pos2 + 1 - A[2]);

//System.out.println("pos2 >A[2]");

pos3 = randomNum;

}

}

else if(pos1 - pos2+A[1] > A[2])

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(pos2+A[1]+1, pos1 + 1 -A[2]);

//System.out.println("pos1 - pos2+A[1] > A[2]");

pos3 = randomNum;

//System.out.println("pos3= " + pos3);

}

}

else

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(pos1+A[0]+1, max+1-A[2]);

//System.out.println("else1");

pos3 = randomNum;

//System.out.println("pos3= " + pos3);

}

}

}

else if (51 - (pos1+A[0]) > A[1])

{

randomNum = ThreadLocalRandom.current().nextInt((pos1+A[0])+1, max+1 -A[1]);

pos2 = randomNum;

if (51 - pos2 + A[1] >A[2])

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(pos2+A[1]+1, max + 1 - A[2]);

pos3 = randomNum;

}

//System.out.println("51 - pos2 + A[1] >A[2]");

}

else if(pos1 - pos2+A[1] > A[2])

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(pos1+A[0]+1, pos2 + 1 -A[2]);

pos3 = randomNum;

}

//System.out.println("pos1 - pos2+A[1] > A[2");

}

else

{

while (pos2==pos3)

{

randomNum = ThreadLocalRandom.current().nextInt(0, pos1-A[2]+1);

pos3 = randomNum;

}

//System.out.println("pos1 - pos2+A[1] > A[2");

}

  

}

for (int a1=pos2;a1<A[1]+pos2; a1++)

{

intArray[a1] = 1;

}

for (int a2=pos3;a2<A[2]+pos3; a2++)

{

intArray[a2] = 1;

}

//System.out.println("pos2="+pos2);

//System.out.println("pos3="+pos3);

// for (int z : intArray)

// {

// System.out.print(z);

// }

///////////////////Start of User Interaction////////////////////////////

int hit =0;

int miss = 0;

System.out.println("Welcome to Battleship! The sizes of the ship are:["+A[0]+","+A[1]+","+A[2]+"]");

System.out.println("Enter a number to fire at the ships. You only fot 10 misses!");

char [] hitsAndMisses = new char[51];

for (int i=0;i<=50;i++)

{

hitsAndMisses[i] = '-';

}

while (miss<10 && hit<3)

{

System.out.println("Here are your shots so far(H is a hit, M is a Miss!)");

for (int i=0;i<=50;i++)

{

System.out.print(i+" ");

}

System.out.println();

for (int i=0;i<=50;i++)

{

System.out.print(hitsAndMisses[i] +" ");

}

System.out.println();

System.out.println("Fire Away! Aim for a location between 0 and 50");

int number = input.nextInt();

if (number <= 50)

{

if (intArray[number] == 1)

{

System.out.println("Hit!");

hit++;

hitsAndMisses[number] = 'H';

}

else

{

System.out.println("Miss!");

miss++;

hitsAndMisses[number] = 'M';

}

}

else

{

System.out.println("Please enter a number between 0 and 50!");

}

}

if (hit==3)

{

System.out.println("You Win!");

}

if (miss == 10)

{

System.out.println("You Loose!");

}

  

System.out.println("Play Again? Enter y or n");

playAg = input.next();

if (playAg.equals("y"))

{

playAgain = true;

}

else

{

playAgain = false;

}

}

System.out.println("Thanks for playing!");

}

}