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