staticVBoxrows=newVBox();//This will be the board, it generates cells for the board so they will be able to react to mouse presses
importjavafx.scene.layout.VBox;
publicBoard(intboardsize,EventHandler<?superMouseEvent>handler){// This is what detects the mouse press on a cell
importjavafx.scene.paint.Color;
for(inty=0;y<boardsize;y++){
importjavafx.scene.shape.Rectangle;
HBoxrow=newHBox();//This will add a row to an Hbox
for(intx=0;x<boardsize;x++){
Cellcell=newCell(x,y);
publicclassBoardextendsParent{
cell.setOnMouseClicked(handler);
row.getChildren().add(cell);
VBoxrows=newVBox();
}
//This will be the board, it generates cells for the board so they will be able to react to mouse presses
rows.getChildren().add(row);// This adds all the hboxes into the bigger Vbox which is the board,so all rows will be set here
}
publicBoard(intboardsize,EventHandler<?superMouseEvent>handler){// This is what detects the mouse press on a cell
getChildren().add(rows);
}
for(inty=0;y<boardsize;y++){
publicclassCellextendsRectangle{
HBoxrow=newHBox();
publicintxAxis;//position on the board
//This will add a row to an Hbox
publicintyAxis;
publicbooleanisShip=false;
for(intx=0;x<boardsize;x++){
publicbooleanshoot=false;//if the cell has already been shot at
Cellcell=newCell(x,y);
publicCell(intxAxis,intyAxis){
super(30,30);
cell.setOnMouseClicked(handler);
this.xAxis=xAxis;
this.yAxis=yAxis;
row.getChildren().add(cell);
setFill(Color.GREY);//This will colour the cells and give them an outline
setStroke(Color.BLACK);
}
}
publicvoidshoot(){
rows.getChildren().add(row);// This adds all the hboxes into the bigger Vbox which is the board,so all rows will be set here
if(isShip==true){//if cell contains a ship
setFill(Color.ORANGE);
}
if(Main.HealthEnemy<1){
System.out.println("You Win!");// Once the player reduces the enemy ships or health (same thing) to zero then they win
getChildren().add(rows);
System.exit(0);
}else{
}
enemyTurn();
}
}else{
setFill(Color.BLUE);//Orange shows a hit, whereas Blue represents a miss
publicclassCellextendsRectangle{
enemyTurn();
}
publicintxAxis;
}
//position on the board
}
publicintyAxis;
publicstaticvoidplace(intshipLength,booleanvertical,intxIn,intyIn){// The array in main will make sure the ships placed will be in order of what I put them in the array and in length
publicbooleanisShip=false;
if(vertical){//checks selected orientation from the mouse press in this case horizontal or vertical
for(inti=yIn;i<yIn+shipLength;i++){//loops all the cells the ship would cover
publicbooleanshoot=false;
Cellcell=getCell(xIn,i);
//if the cell has already been shot at
cell.setFill(Color.DARKGREY);//colours the squares on the grid to show a ship
cell.isShip=true;//Marks the cells showing that a ship is present there
}
publicCell(intxAxis,intyAxis){
}else{
for(inti=xIn;i<xIn+shipLength;i++){
super(xAxis*30,yAxis*30,30,30);
Cellcell=getCell(i,yIn);
cell.setFill(Color.DARKGREY);
this.xAxis=xAxis;
cell.isShip=true;
}
this.yAxis=yAxis;
}
}
setFill(Color.GREY);
publicstaticbooleanobstructed(booleanvertical,intshipLength,intxIn,intyIn){//This checks to see if the ship can be placed in the requested location
//This will colour the cells and give them an outline
booleanblocked=true;//the return value
booleanshipBlock=false;//true when this collides with another ship
setStroke(Color.BLACK);
if(vertical){
}
if(yIn+shipLength<=Main.boardsize){//checks if placement fits into the boundaries of board
for(inti=yIn;i<yIn+shipLength;i++){//loops through cells that current ship would fill to check for already placed ships
Cellcell=getCell(xIn,i);
publicvoidshoot(){
if(cell.isShip){//if cells are already occupied stops the loop
shipBlock=true;
if(isShip==true){
break;
//if cell contains a ship
}
}
setFill(Color.ORANGE);
if(!shipBlock){//returns false if the checked cells are not occupied
blocked=false;
if(Main.HealthEnemy<1){
}
}
System.out.println("You Win!");// Once the player reduces the enemy ships or health (same thing) to zero then they win
}else{
if(xIn+shipLength<=Main.boardsize){
System.exit(0);
for(inti=xIn;i<xIn+shipLength;i++){// This repeats stages like above except for checking the horizontal placements
Cellcell=getCell(i,yIn);
}else{
if(cell.isShip){
shipBlock=true;
enemyTurn();
break;
}
}
}
if(!shipBlock){
}else{
blocked=false;
}
setFill(Color.BLUE);
}
//Orange shows a hit, whereas Blue represents a miss
}
enemyTurn();
returnblocked;
}
}
}
publicstaticCellgetCell(intx,inty){//Calls the coordinates of cells
publicvoidplace(intships,booleanvertical,intxIn,intyIn){// The array in main will make sure the ships placed will be in order of what I put them in the array and in length
intComputerX=Main.random.nextInt(Main.boardsize);//This will generate a random coordinate on the board