Commit a28fb59d authored by patrick.gustard-sm's avatar patrick.gustard-sm

Update Board.java

parent be5ba812
package battleship; package battleship;
import Board.Cell;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
public class Board extends Parent{ public class Board extends Parent{
static VBox rows = new VBox(); //This will be the board, it generates cells for the board so they will be able to react to mouse presses
VBox rows = new VBox();
public Board(int boardsize, EventHandler<? super MouseEvent> 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 (int y = 0; y < boardsize; y++) {
HBox row = new HBox(); //This will add a row to an Hbox public Board(int boardsize, EventHandler<? super MouseEvent> handler) { // This is what detects the mouse press on a cell
for (int x = 0; x < boardsize; x++) {
Cell cell = new Cell(x,y);
cell.setOnMouseClicked(handler); for (int y = 0; y < boardsize; y++) {
row.getChildren().add(cell);
} HBox row = new HBox();
//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 (int x = 0; x < boardsize; x++) {
getChildren().add(rows);
} Cell cell = new Cell(x,y);
cell.setOnMouseClicked(handler);
public class Cell extends Rectangle{
public int xAxis; //position on the board row.getChildren().add(cell);
public int yAxis;
public boolean isShip = false; }
public boolean shoot = false; //if the cell has already been shot at
public Cell(int xAxis, int yAxis) { 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);
} }
public void shoot() {
if (isShip == true) { //if cell contains a ship
setFill(Color.ORANGE); public class Cell extends Rectangle{
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 public int xAxis;
System.exit(0); //position on the board
} else {
enemyTurn(); public int yAxis;
}
} else { public boolean isShip = false;
setFill(Color.BLUE); //Orange shows a hit, whereas Blue represents a miss
enemyTurn(); public boolean shoot = false;
} //if the cell has already been shot at
}
}
public Cell(int xAxis, int yAxis) {
public static void place(int shipLength, boolean vertical, int xIn, int yIn) { // 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 (int i = yIn; i < yIn + shipLength; i++) { //loops all the cells the ship would cover this.xAxis = xAxis;
Cell cell = 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 (int i = xIn; i < xIn + shipLength; i++) {
Cell cell = getCell(i, yIn); setStroke(Color.BLACK);
cell.setFill(Color.DARKGREY);
cell.isShip = true; }
}
}
} public void shoot() {
public static boolean obstructed( boolean vertical,int shipLength, int xIn, int yIn) { //This checks to see if the ship can be placed in the requested location
boolean blocked = true; //the return value if (isShip == true) {
boolean shipBlock = 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 (int i = yIn; i < yIn + shipLength; i++) { //loops through cells that current ship would fill to check for already placed ships if (Main.HealthEnemy < 1) {
Cell cell = 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 (int i = xIn; i < xIn + shipLength; i++) { // This repeats stages like above except for checking the horizontal placements
Cell cell = 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; }
}
} }
}
return blocked; public void place(int ships, boolean vertical, int xIn, int yIn) { // 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);
public static Cell getCell(int x, int y) { //Calls the coordinates of cells if (vertical) {
return (Cell)((HBox)rows.getChildren().get(y)).getChildren().get(x); //checks selected orientation from the mouse press in this case horizontal or vertical
for (int i = yIn; i < yIn + ships; i++) {
//loops all the cells the ship would cover
Cell cell = 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 (int i = xIn; i < xIn + ships; i++) {
Cell cell = 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
*/
public boolean obstructed( boolean vertical,int ships, int xIn, int yIn) {
//This checks to see if the ship can be placed in the requested location
System.out.println("obstructed " + ships);
boolean blocked = true;
//the return value
boolean shipBlock = false;
//true when this collides with another ship
if (vertical) {
if (yIn + ships <= Main.boardsize) {
//checks if placement fits into the boundaries of board
for (int i = yIn; i < yIn + ships; i++) {
//loops through cells that current ship would fill to check for already placed ships
Cell cell = 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 + ships <= Main.boardsize) {
for (int i = xIn; i < xIn + ships; i++) { // This repeats stages like above except for checking the horizontal placements
Cell cell = getCell(i, yIn);
if (cell.isShip) {
shipBlock = true;
break;
}
}
if (!shipBlock) {
blocked = false;
}
}
}
return blocked;
}
public Cell getCell(int x, int y) {
//Calls the coordinates of cells
return (Cell)((HBox)rows.getChildren().get(y)).getChildren().get(x);
}
public void enemyTurn() { public void enemyTurn() {
int ComputerX = Main.random.nextInt(Main.boardsize); //This will generate a random coordinate on the board
int ComputerY = Main.random.nextInt(Main.boardsize);
Cell enemyTarget = Main.BoardPlayer.getCell(ComputerX, ComputerY); int ComputerX = Main.random.nextInt(Main.boardsize);
//This will generate a random coordinate on the board
int ComputerY = Main.random.nextInt(Main.boardsize);
Cell enemyTarget = Main.BoardPlayer.getCell(ComputerX, ComputerY);
while (enemyTarget.shoot) { //Repeats the stage if the cell generated has already been shot at while (enemyTarget.shoot) { //Repeats the stage if the cell generated has already been shot at
ComputerX = Main.random.nextInt(Main.boardsize);
ComputerY = Main.random.nextInt(Main.boardsize);
enemyTarget = Main.BoardPlayer.getCell(ComputerX, ComputerY); //Sets the computers target to be the player board
}
if (enemyTarget.isShip) { ComputerX = Main.random.nextInt(Main.boardsize);
enemyTarget.setFill(Color.ORANGE);
Main.HealthPlayer --;
} else {
enemyTarget.setFill(Color.BLUE); //Orange shows a hit, whereas Blue represents a miss
}
if (Main.HealthPlayer < 1) { //When the player loses all ships or health (same thing) they lose ComputerY = Main.random.nextInt(Main.boardsize);
System.out.println("You lost, better luck next time");
System.exit(0); enemyTarget = Main.BoardPlayer.getCell(ComputerX, ComputerY); //Sets the computers target to be the player board
}
}
if (enemyTarget.isShip) {
enemyTarget.setFill(Color.ORANGE);
Main.HealthPlayer --;
} else {
enemyTarget.setFill(Color.BLUE);
//Orange shows a hit, whereas Blue represents a miss
}
if (Main.HealthPlayer < 1) { //When the player loses all ships or health (same thing) they lose
System.out.println("You lost, better luck next time");
System.exit(0);
} }
...@@ -144,4 +289,9 @@ public void enemyTurn() { ...@@ -144,4 +289,9 @@ public void enemyTurn() {
} }
}
//Cell.setfill put in if statement, to tell if player or enemy, if enemy do nothing, create variable in board, but set player or enemy in main //Cell.setfill put in if statement, to tell if player or enemy, if enemy do nothing, create variable in board, but set player or enemy in main
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment