thisSeaCom.setFill(Color.RED);// colours red to indicate a hit
thisSeaCom.toFront();
}
elseif(gameOn==true&&cpuBoard[thisSeaCom.x][thisSeaCom.y]==0)// triggers if sea is empty
{
thisSeaCom.setX((50*thisSeaCom.x)+850);
thisSeaCom.setY((50*thisSeaCom.y)+50);
thisSeaCom.setWidth(45);
thisSeaCom.setHeight(45);
thisSeaCom.setCursor(Cursor.HAND);
thisSeaCom.setFill(Color.DARKGREY);// colours to show the tile has been shot, and missed
thisSeaCom.toFront();
}
if(gameOn==true){
BooleanshotCheck=false;
Randomrng=newRandom();
while(shotCheck==false)
{
currentX=rng.nextInt(10);// generate random sea for cpu to shoot for x and y
currentY=rng.nextInt(10);
if(cpuShotsBoard[currentX][currentY]==0)// checks if hasn't been shot before, with array holding previous shots
{
shotCheck=true;// flag for legal space to shoot
}
}
cpuShotsBoard[currentX][currentY]=1;// fills in array so sea wont get shot again
SeashotCpuSquare=newSea();
if(board[currentX][currentY]!=0)// triggers if the com has a hit
{
shotCpuSquare.setX((50*currentX)+50);// keeps setting up sea tiles accordingly
shotCpuSquare.setY((50*currentY)+50);
shotCpuSquare.setWidth(45);
shotCpuSquare.setHeight(45);
shotCpuSquare.setCursor(Cursor.HAND);
shotCpuSquare.setFill(Color.RED);// sets colour to red to tell the user a ship has been hit
shotCpuSquare.toFront();
group.getChildren().add(shotCpuSquare);
}
elseif(board[currentX][currentY]==0)
{
shotCpuSquare.setX((50*currentX)+50);
shotCpuSquare.setY((50*currentY)+50);
shotCpuSquare.setWidth(45);
shotCpuSquare.setHeight(45);
shotCpuSquare.setCursor(Cursor.HAND);
shotCpuSquare.setFill(Color.DARKGREY);// sets colour to tell the user the com missed
shotCpuSquare.toFront();
group.getChildren().add(shotCpuSquare);
}
}
});
group.getChildren().add(thisSeaCom);
}
}
//back in the prep phase
while(shipsPlacedCom<8)// runs until com has placed its max number of ships
{// the ships placed will be random
Randomrng=newRandom();// sets up rng
intx=rng.nextInt(10);// generates random coordinates
inty=rng.nextInt(10);
BooleancpuHorizontal=rng.nextBoolean();// randomly desides if current ship being placed is vertical or horizontal
BooleanpotentialLegalSpace=false;
BooleanlegalSpace=false;
intlegalSpaceCount=0;
if(carriersCom>0)// variant of code above, checks each ship type and places accordingly
{
lengthCPU=5;
carriersCom=carriersCom-1;
boatHPCom[shipsPlacedCom+1]=5;
}
elseif(battleshipsCom>0)
{
lengthCPU=4;
battleshipsCom=battleshipsCom-1;
boatHPCom[shipsPlacedCom+1]=4;
}
elseif(destroyersCom>0)
{
lengthCPU=3;
destroyersCom=destroyersCom-1;
boatHPCom[shipsPlacedCom+1]=3;
}
elseif(patrolsCom>0)
{
lengthCPU=2;
patrolsCom=patrolsCom-1;
boatHPCom[shipsPlacedCom+1]=2;
}
if(cpuHorizontal==true&&x+lengthCPU<=10)// legal space validation is the same with the computers variables
{
potentialLegalSpace=true;
}
elseif(cpuHorizontal==false&&y+lengthCPU<=10)// checks are the same as above swapping out the user input isShiftDown for a variable to determine the same
{
potentialLegalSpace=true;
}
if(potentialLegalSpace==true)
{
for(intk=0;k<lengthCPU;k++)
{
if(cpuHorizontal==true)
{
if(cpuBoard[x+k][y]==0)
{
legalSpaceCount++;
}
}
else
{
if(cpuBoard[x][y+k]==0)
{
legalSpaceCount++;
}
}
}
if(legalSpaceCount==lengthCPU)
{
legalSpace=true;
}
//validation complete, last thing is to create the coms ships