Commit 0fab2dc8 authored by patrick.gustard-sm's avatar patrick.gustard-sm

Update Main.java

parent a28fb59d
package battleship; package battleship;
import java.util.Random;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent; import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton; import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox; import javafx.scene.Parent;
import javafx.stage.Stage;
import javafx.scene.Scene;
public class Main extends Application {
import javafx.scene.input.MouseButton;
public static int boardsize = 10; //board size, locks the size to always being a square, if the number is changed will always be square
public static boolean vertical = false; import javafx.scene.layout.BorderPane;
static Random random = new Random ();
public static boolean setup = false; import javafx.scene.layout.VBox;
static int[] ships = new int[]{5,4,4,3,3,2,2,2}; //Array with all the ship lengths
static int shipnum = 0; import javafx.stage.Stage;
static int xInteger;
static int yInteger;
static int HealthPlayer = 25; // Sets the health for both enemy and player, this is the total squares for all ships public class Main extends Application {
static int HealthEnemy = 25;
static Board BoardPlayer = new Board(boardsize, event -> {
Board.Cell cell = (Board.Cell) event.getSource(); public static int boardsize = 10;
xInteger = cell.xAxis; //board size, locks the size to always being a square, if the number is changed will always be square
yInteger = cell.yAxis;
if (event.getButton() == MouseButton.PRIMARY) { public static boolean vertical = false;
vertical = true;
} static Random random = new Random ();
if (setup && !Board.obstructed(vertical, boardsize, xInteger, yInteger)) { public static boolean setup = true;
Board.place(boardsize, vertical, xInteger, yInteger);
shipnum ++; static int[] ships = new int[]{5,4,4,3,3,2,2,2}; //Array with all the ship lengths
}
}); static int shipnum = 0;
static Board BoardEnemy = new Board(boardsize, event -> {
Board.Cell cell = (Board.Cell) event.getSource(); static int enemyshipnum = 0;
if (!cell.shoot && !setup) { //Ignores if the selected cell has already been shot at static int xInteger;
cell.shoot();
} else { static int yInteger;
return;
} static int HealthPlayer = 25; // Sets the health for both enemy and player, this is the total squares for all ships
}); static int HealthEnemy = 25;
static Board BoardPlayer;
@Override static Board BoardEnemy;
public void start(Stage stage) throws Exception {
Scene gameWindow = new Scene(build()); //builds the scene, so sets everything up in the window
stage.setTitle("Battleship"); public static void main(String[] args) {
stage.setScene(gameWindow);
stage.show(); launch(args);
}
public Parent build() { }
BorderPane workspace = new BorderPane();
@Override
VBox leftPane = new VBox( BoardPlayer); //Sets where I want each of the boards, in this case next to each other public void start(Stage stage) throws Exception {
VBox rightPane = new VBox( BoardEnemy);
leftPane.setAlignment(Pos.CENTER_LEFT);
rightPane.setAlignment(Pos.CENTER_LEFT); BoardPlayer = new Board(boardsize, event -> {
return workspace;
} Board.Cell cell = (Board.Cell) event.getSource();
public static void enemySetup() { /* grid coordinates of click */
shipnum=0;
} xInteger = cell.xAxis;
public static void enemyPlace() {
int ComputerX = random.nextInt(boardsize); //Generates a random location on the board yInteger = cell.yAxis;
int ComputerY = random.nextInt(boardsize);
boolean aiVert = random.nextBoolean(); if (event.getButton() == MouseButton.PRIMARY) {
if (setup && !Board.obstructed(vertical, boardsize, xInteger, yInteger)) {
if (!Board.obstructed(aiVert, ComputerX, ComputerY, ComputerY)) { //prevents placement in obstructed areas vertical = true;
Board.place(ComputerY, aiVert, ComputerX, ComputerY); //Places the ships from the board
} }
}
} if (setup && !BoardPlayer.obstructed(vertical, ships[shipnum], xInteger, yInteger)) {
public static void startGame(){
setup = false; System.out.println("placing");
enemySetup();
} BoardPlayer.place(ships[shipnum], vertical, xInteger, yInteger);
}
shipnum ++;
// to-do: set setup to false when shipnum is too big? (i.e. when it's 8)
}
if (shipnum == 8) {
startGame();
}
});
BoardEnemy = new Board(boardsize, event -> {
Board.Cell cell = (Board.Cell) event.getSource();
if (!cell.shoot && !setup) {
//Ignores if the selected cell has already been shot at
cell.shoot();
} else {
return;
}
});
Scene gameWindow = new Scene(build());
//builds the scene, so sets everything up in the window
stage.setTitle("Battleship");
stage.setScene(gameWindow);
stage.show();
}
public Parent build() {
BorderPane workspace = new BorderPane();
VBox leftPane = new VBox( BoardPlayer); //Sets where I want each of the boards, in this case next to each other
VBox rightPane = new VBox( BoardEnemy);
workspace.setLeft(leftPane);
workspace.setRight(rightPane);
leftPane.setAlignment(Pos.CENTER_LEFT);
rightPane.setAlignment(Pos.CENTER_LEFT);
return workspace;
}
public static void enemySetup() {
enemyshipnum=0;
}
public static void enemyPlace() {
int ComputerX = random.nextInt(boardsize);
//Generates a random location on the board
int ComputerY = random.nextInt(boardsize);
boolean aiVert = random.nextBoolean();
System.out.println("maybe placing enemy ship at " + ComputerX + " " + ComputerY);
System.out.println("obstructed is " + BoardEnemy.obstructed(vertical, boardsize, xInteger, yInteger));
if (setup && !BoardEnemy.obstructed(vertical, ships[enemyshipnum], xInteger, yInteger)) {
if (!BoardEnemy.obstructed(aiVert, ComputerX, ComputerY, ComputerY)) {
//prevents placement in obstructed areas
BoardEnemy.place(ComputerY, aiVert, ComputerX, ComputerY);
//Places the ships from the board
//For Loop to repeat cycle, and something to cancel cycle when it reaches 8
for (;enemyshipnum < 7; enemyshipnum++);
}
}
}
public static void startGame(){
enemySetup();
enemyPlace();
setup = false;
}
}
\ 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