Commit be5ba812 authored by Patrick's avatar Patrick

Programming 02

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>battleship</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
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);
}
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);
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) {
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);
}
}
}
//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
package battleship;
import java.util.Random;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
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;
static Random random = new Random ();
public static boolean setup = false;
static int[] ships = new int[]{5,4,4,3,3,2,2,2}; //Array with all the ship lengths
static int shipnum = 0;
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
static int HealthEnemy = 25;
static Board BoardPlayer = new Board(boardsize, event -> {
Board.Cell cell = (Board.Cell) event.getSource();
xInteger = cell.xAxis;
yInteger = cell.yAxis;
if (event.getButton() == MouseButton.PRIMARY) {
vertical = true;
}
if (setup && !Board.obstructed(vertical, boardsize, xInteger, yInteger)) {
Board.place(boardsize, vertical, xInteger, yInteger);
shipnum ++;
}
});
static Board 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;
}
});
@Override
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");
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);
leftPane.setAlignment(Pos.CENTER_LEFT);
rightPane.setAlignment(Pos.CENTER_LEFT);
return workspace;
}
public static void enemySetup() {
shipnum=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();
if (setup && !Board.obstructed(vertical, boardsize, xInteger, yInteger)) {
if (!Board.obstructed(aiVert, ComputerX, ComputerY, ComputerY)) { //prevents placement in obstructed areas
Board.place(ComputerY, aiVert, ComputerX, ComputerY); //Places the ships from the board
}
}
}
public static void startGame(){
setup = false;
enemySetup();
}
}
\ 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