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