import java.util.Scanner;
/*
Try and get all the computer's ships in the least amount of guesses
The board is 8 x 8
There are 3 ships,
- 1 long
- 2 long
- 3 long
GLHF
__/___
_____/______|
_______/_____\_______\_____
\ < < < |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
public class Main {
public static void main(String[] args) {
//won variable
boolean won = false;
//boat 1, 3 long
int x1a = (int)((Math.random()*8)+1);
int y1a = (int)((Math.random()*8)+1);
int x1b =x1a;
int y1b=y1a;
int x1c=x1a;
int y1c=y1a;
//hit status variable
boolean boat1aHit = false;
boolean boat1bHit =false;
boolean boat1cHit=false;
int rand1 = (int)((Math.random()*4)+1);
//gens second and third coord
if(rand1==1){
//add x
x1b = x1a+1;
x1c = x1b+1;
if(x1c>8){ //if out of range, add to other side
x1b=x1a-1;
x1c=x1b-1;
}
}else if (rand1==2){
//sub x
x1b=x1a-1;
x1c = x1b-1;
if(x1c<1){
x1b=x1a+1;
x1c=x1b+1;
}
}else if (rand1==3){
//add y
y1b=y1a+1;
y1c = y1b+1;
if(y1c>8){
y1b=y1a-1;
y1c=y1b-1;
}
}else{
//sub y
y1b=y1a-1;
y1c=y1b-1;
if(y1c<1){
y1b=y1a+1;
y1c=y1b+1;
}
}
//boat 2, 2 long
int x2a = (int)((Math.random()*8)+1);
int y2a = (int)((Math.random()*8)+1);
int x2b =x2a;
int y2b = y2a;
//hit status variable
boolean boat2aHit = false;
boolean boat2bHit =false;
int rand2 = (int)((Math.random()*4)+1);
//gens second and third coord
if(rand2==1){
//add x
x2b = x2a+1;
if(x2b>8){ //if out of range, add to other side
x2b=x2a-1;
}
}else if (rand2==2){
//sub x
x2b=x2a-1;
if(x2b<1){
x2b=x2a+1;
}
}else if (rand2==3){
//add y
y2b=y2a+1;
if(y2b>8){
y2b=y2a-1;
}
}else{
//sub y
y2b=y2a-1;
if(y2b<1){
y2b=y2a+1;
}
}
//makes sure none of the coords are the same
while(x2a==x1a && y2a==y1a ||
x2a==x1b && y2a==y1b ||
x2a==x1c && y2a==y1c ||
x2b==x1a && y2b==y1a ||
x2b==x1b && y2b==y1b ||
x2b==x1c && y2b==y1c){
//boat 2, 2 long
x2a = (int)((Math.random()*8)+1);
y2a = (int)((Math.random()*8)+1);
x2b =x2a;
y2b = y2a;
//gens second and third coord
if(rand2==1){
//add x
x2b = x2a+1;
if(x2b>8){ //if out of range, add to other side
x2b=x2a-1;
}
}else if (rand2==2){
//sub x
x2b=x2a-1;
if(x2b<1){
x2b=x2a+1;
}
}else if (rand2==3){
//add y
y2b=y2a+1;
if(y2b>8){
y2b=y2a-1;
}
}else{
//sub y
y2b=y2a-1;
if(y2b<1){
y2b=y2a+1;
}
}
}
//boat 3, 1 long
int x3a =(int)((Math.random()*8)+1);
int y3a=(int)((Math.random()*8)+1);
//hit status variable
boolean boat3aHit = false;
while(x3a==x1a && y3a==y1a ||
x3a==x1b && y3a==y1b ||
x3a==x1c && y3a==y1c ||
x3a==x2a && y3a==y2a ||
x3a==x2b && y3a==y2b){
x3a =(int)((Math.random()*8)+1);
y3a=(int)((Math.random()*8)+1);
}
int boat1aSqr=convert(x1a,y1a);
int boat1bSqr=convert(x1b,y1b);
int boat1cSqr=convert(x1c,y1c);
int boat2aSqr=convert(x2a,y2a);
int boat2bSqr=convert(x2b,y2b);
int boat3aSqr= convert(x3a,y3a);
boolean boat1aGot=false;//guessed this hit alr
boolean boat1bGot=false;
boolean boat1cGot=false;
boolean boat2aGot=false;
boolean boat2bGot=false;
boolean boat3aGot=false;
boolean saidBoat1 =false;
boolean saidBoat2 =false;
boolean saidBoat3 =false;
int numGuess = 0;
int[] misses = new int[200];
boolean[] guessSqr = new boolean[65];
int i=1;
boolean xGood=false;
//while not won yet
while(!won){
//input
Scanner myObj = new Scanner(System.in);
System.out.println("Which X value do you guess? range 1-8");
int xAns = myObj.nextInt();
if (xAns >0&&xAns<=8){
xGood=true;
while(xGood){
System.out.println("Which Y value do you guess? range 1-8");
int yAns = myObj.nextInt();
if (yAns >0&&yAns<=8){
int guess = convert(xAns,yAns);
if(guessSqr[guess]){
System.out.println("Already guessed");
xGood = false;
}else{
//ALL NEXT CODE GOES HERE
boolean printedSqr=false;
boolean boat1a=false;//been printed alr
boolean boat1b=false;
boolean boat1c=false;
boolean boat2a=false;
boolean boat2b=false;
boolean boat3a=false;
if (xAns == x1a && yAns == y1a ||
xAns == x1b && yAns == y1b ||
xAns == x1c && yAns == y1c ||
xAns == x2a && yAns == y2a ||
xAns == x2b && yAns == y2b ||
xAns == x3a && yAns == y3a){
System.out.println("Hit!");
if(xAns == x1a && yAns == y1a){
boat1aHit = true;
}else if(xAns==x1b && yAns == y1b){
boat1bHit = true;
}else if(xAns==x1c && yAns == y1c){
boat1cHit = true;
}else if(xAns==x2a && yAns == y2a){
boat2aHit = true;
}else if(xAns==x2b && yAns == y2b){
boat2bHit = true;
}else if(xAns==x3a && yAns ==y3a){
boat3aHit = true;
}
}else{
System.out.println("Miss");
}
if (boat1aHit && boat1bHit && boat1cHit && boat2aHit &&boat2bHit &&boat3aHit){
won = true;
}
numGuess++;
int num=1;
while(num<=64){
if (guess==boat1aSqr){
boat1a=true;
boat1aGot=true;
}
if (guess==boat1bSqr){
boat1b=true;
boat1bGot=true;
}
if (guess==boat1cSqr){
boat1c=true;
boat1cGot=true;
}if (guess==boat2aSqr){
boat2a=true;
boat2aGot=true;
}if (guess==boat2bSqr){
boat2b=true;
boat2bGot=true;
}if (guess==boat3aSqr){
boat3a=true;
boat3aGot=true;
}
if(boat1aGot&&boat1bGot&&boat1cGot&&!saidBoat1){
System.out.println("You sunk a ship!");
saidBoat1=true;
}
if(boat2aGot&&boat2bGot&&!saidBoat2){
System.out.println("You sunk a ship!");
saidBoat2=true;
}
if(boat3aGot&&!saidBoat3){
System.out.println("You sunk a ship!");
saidBoat3=true;
}
//if num is guess
if(num==guess){
guessSqr[guess] = true;
if(guess==boat1aSqr||
guess==boat1bSqr||
guess==boat1cSqr||
guess==boat2aSqr||
guess==boat2bSqr||
guess==boat3aSqr){
System.out.print("[x]");
printedSqr=true;
}
else {
if(!printedSqr){
System.out.print("[o]");
}
misses[i-1]=num;
i++;
}
}
//if num is guess ends
boolean noPrevHit=true;//this num has not been hit
//if the sqr being printed is one of the squares with a boat
//and this hasnt been printed yet
//and the corresponding sqr has been guessed alr
if(!printedSqr){
if(num==boat1aSqr&&!boat1a&&boat1aGot||
num==boat1bSqr&&!boat1b&&boat1bGot||
num==boat1cSqr&&!boat1c&&boat1cGot||
num==boat2aSqr&&!boat2a&&boat2aGot||
num==boat2bSqr&&!boat2b&&boat2bGot||
num==boat3aSqr&&!boat3a&&boat3aGot){
System.out.print("[x]");
noPrevHit=false;
}
if(num!=guess&&noPrevHit){
for(int k=0; k<misses.length; k++){
if(num==misses[k]){
System.out.print("[o]");
printedSqr=true;
}
}
if(!printedSqr){
System.out.print("[ ]");
}
}
}
if(num%8==0){
System.out.println();
}
printedSqr=false;
num++;
boat1a=false;
boat1b=false;
boat1c=false;
boat2a=false;
boat2b=false;
boat3a=false;
}
xGood=false;
}
}else{
System.out.println("Invalid input");
}
}
}else{
System.out.println("Invalid input");
}
}
System.out.println("You won!");
System.out.println("It took you "+numGuess +" guesses.");
}
public static int convert(int x, int y){
int ans;
if(y==1){
ans=x;
}else if (y==2){
ans = x+y+6;
}else if (y==3){
ans = x+y+13;
}else if (y==4){
ans = x+y+20;
}else if (y==5){
ans = x+y+27;
}else if (y==6){
ans = x+y+34;
}else if (y==7){
ans = x+y+41;
}else{
ans = x+y+48;
}
return ans;
}
}