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

Update Board.java

parent be5ba812
package battleship;
import Board.Cell;
import javafx.event.EventHandler;
import javafx.scene.Parent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
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
public Board(int boardsize, EventHandler<? super MouseEvent> handler) { // This is what detects the mouse press on a cell
for (int y = 0; y < boardsize; y++) {
HBox row = new HBox(); //This will add a row to an Hbox
for (int x = 0; x < boardsize; x++) {
Cell cell = new Cell(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);
}
public class Cell extends Rectangle{
public int xAxis; //position on the board
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) {
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);
}
public void shoot() {
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();
}
}
}
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
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
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 + shipLength; i++) {
Cell cell = getCell(i, yIn);
cell.setFill(Color.DARKGREY);
cell.isShip = true;
}
}
}
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
boolean shipBlock = 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 (int i = yIn; i < yIn + shipLength; 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 + shipLength <= Main.boardsize) {
for (int i = xIn; i < xIn + shipLength; 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 static Cell getCell(int x, int y) { //Calls the coordinates of cells
return (Cell)((HBox)rows.getChildren().get(y)).getChildren().get(x);
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
public Board(int boardsize, EventHandler<? super MouseEvent> handler) { // This is what detects the mouse press on a cell
for (int y = 0; y < boardsize; y++) {
HBox row = new HBox();
//This will add a row to an Hbox
for (int x = 0; x < boardsize; x++) {
Cell cell = new Cell(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);
}
public class Cell extends Rectangle{
public int xAxis;
//position on the board
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) {
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);
}
public void shoot() {
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();
}
}
}
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);
if (vertical) {
//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() {
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
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
}
while (enemyTarget.shoot) { //Repeats the stage if the cell generated has already been shot at
if (enemyTarget.isShip) {
enemyTarget.setFill(Color.ORANGE);
Main.HealthPlayer --;
} else {
enemyTarget.setFill(Color.BLUE); //Orange shows a hit, whereas Blue represents a miss
}
ComputerX = Main.random.nextInt(Main.boardsize);
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);
}
ComputerY = Main.random.nextInt(Main.boardsize);
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() {
}
}
//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