Commit 488def7c authored by christopher.spray's avatar christopher.spray

First Commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<component inherit-compiler-output="true" inheritJdk="true">
<output-test url="file://$MODULE_DIR$/out/test/Battleships"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="eclipse" classpath-dir="$MODULE_DIR$" type="JAVA_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<eclipse-userlibraries />
public class AI {
int x;
int y;
int prevX;
int prevY;
int dir;
int lastDir;
public Board board;
//smart AI works, just double fires sometimes, not enough time to fix it...
public void randomFire() {
y = (int) (Math.random() * board.gridY); //get a random number between 0 and gridY, add 1
x = (int) (Math.random() * board.gridX); //get a random number between 0 and gridX, add 1
if (board.firingPhase && board.AIturn) //if it's the firing phase, and AIturn
System.out.println("AI: fired at: " + x + " " + y);
if (board.tiles[y][x].isShip && !board.tiles[y][x].shotAt) {//if the tile is a ship, and hasn't been shot at
board.hitTruth = 2; //set hitTruth to 2 (This is so it calls findDir next turn)
board.tiles[y][x].shotAt = true; //set shotAt variable to true so it can't fire at that tile.
prevY = y; //store chosen y coord
prevX = x; //sore chosen x coord
board.tiles[y][x].setStyle("-fx-background-color: green; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
System.out.println("AI: Just hit your ship!"); //print ai hit your ship
board.tiles[y][x].shipOnTile.isHit(); //Call isHit function on the relevant ship
board.pShipsLeft--; //remove 1 pShipsleft
if (board.tiles[y][x].shipOnTile.isDestroyed) { //if the ship is destroyed
board.hitTruth = 1; //go back to random fire
System.out.println("AI: Just sunk your " + board.tiles[y][x].shipOnTile); //print is been destroyed
} else {
System.out.println("AI: Hit"); //print Hit
}
if (board.pShipsLeft == 0) { //if the player has no points left
System.out.println("You Lost!"); //display that they have lost
}
board.AIturn = false; //Ai turn ends
board.playerTurn = true; //player turn starts
}
else if (!board.tiles[y][x].shotAt && !board.tiles[y][x].isShip){ //if the tile hasnt been hit, and wasnt a ship.
board.hitTruth = 1; //set hitTruth to one (so another random fire is picked next turn)
board.tiles[y][x].setStyle("-fx-background-color: red; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these
board.tiles[y][x].shotAt = true; //set shotAt to true
System.out.println("AI: Missed!"); //display the ai missed every ship
board.AIturn = false; //end ai turn
board.playerTurn = true; //start PlayerTurn
}
else if(board.tiles[y][x].shotAt) { //else if tile has been shot at before, re-fire
randomFire();
}
}
//When a ship is hit
public void findDir() {
//1 = Left
//2 = right
//3 = Up
//4 = Down
dir = (int) (Math.random() * 4); //generate random number between 1-4.
if (dir == 1) { //if dir was 1 (left)
x = prevX - 1; //minus 1 from x co-ord (to go left)
if (x < 1){
System.out.println("Can't shoot left, shooting right instead");
x = prevX + 1;
}
y = prevY; //dont change y coord
}
if (dir == 2){ //if dir was 2 (right)
x = prevX + 1; //add 1 to x co-ord (to go right)
if (x > board.gridX){
System.out.println("Can't shoot right, shooting left instead");
x = prevX - 1;
}
y = prevY; //dont change y co-ord
}
if (dir == 3){ //if dir was 3 (up)
y = prevY - 1; //minus 1 from y co-ord (to go up)
if (y < 1){
System.out.println("Can't shoot up, shooting down instead");
y = prevY + 1;
}
x = prevX; //dont change x co-ord
}
if (dir == 4){ //if dir was 4 (Down)
y = prevY + 1; //add 1 to x co-ord (to go down)
if (y > board.gridY){
System.out.println("Can't shoot down, shooting up instead");
y = prevY - 1;
}
x = prevX; //dont change x co-ord
}
if (board.firingPhase && board.AIturn) //if it's the firing phase & AIturn
if (board.tiles[y][x].isShip && !board.tiles[y][x].shotAt) {//if the tile is a ship again, and hasn't been shot at
board.hitTruth = 3; //set hitTruth to 3 so keepGoing() is called next
lastDir = dir; //store dir
prevY = y; //store co-ords again
prevX = x; //store co-ords again
board.tiles[y][x].setStyle("-fx-background-color: green; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
System.out.println("AI: Just hit your ship!"); //print ai hit your ship
board.tiles[y][x].shotAt = true; //shot at is now true so it cant fire on it again
board.tiles[y][x].shipOnTile.isHit(); //Call isHit function on the relevant ship
if (board.tiles[y][x].shipOnTile.isDestroyed) { //if the ship is destroyed
board.hitTruth = 1; //if ship is destroyed, go back to random fire
board.pShipsLeft--; //remove 1 pShipsleft value
System.out.println("AI: Just sunk a " + board.tiles[y][x].shipOnTile); //print is been destroyed
} else {
System.out.println("AI: Hit"); //print Hit
}
if (board.pShipsLeft == 0) { //if there are no more ships on players board
System.out.println("You Lost!"); //display they lost
}
board.AIturn = false; //end AIturn
board.playerTurn = true; //start playerTurn
}
else if (!board.tiles[y][x].shotAt && !board.tiles[y][x].isShip){ //if the tile hasnt been hit, and wasnt a ship.
board.hitTruth = 2; //go back to findDir
board.tiles[y][x].setStyle(""); //apply these changes
board.tiles[y][x].shotAt = true; //set tile shotAt value to true
System.out.println("AI: Missed!"); //display ai missed
board.AIturn = false; //end AIturn
board.playerTurn = true; //start playerTurn
}
else{
board.hitTruth = 1;
board.AIturn=false;
board.playerTurn = true;
}
}
public void keepGoing() {
//maybe check if it would be in boundaries, if not, go 1 in opposite direction
if (lastDir == 1){ //if dir was 1 (left)
x = prevX - 1; //minus 1 from x co-ord (to go left)
y = prevY; //dont change y coord
}
if (lastDir == 2){ //if dir was 2 (right)
x = prevX + 1; //add 1 to x co-ord (to go right)
y = prevY; //dont change y co-ord
}
if (lastDir== 3){ //if dir was 3 (up)
y = prevY - 1; //minus 1 from y co-ord (to go up)
x = prevX; //dont change x co-ord
}
if (lastDir == 4){ //if dir was 4 (Down)
y = prevY + 1; //add 1 to x co-ord (to go down)
x = prevX; //dont change x co-ord
}
if (board.firingPhase && board.AIturn) //if it's the firing phase & AIturn
if (board.tiles[y][x].isShip && !board.tiles[y][x].shotAt) {//if the tile is a shipagain, and hasn't been shot at
board.hitTruth = 3; //set hitTruth to 3 so keepGoing() is called next
prevY = y; //store co-ords again
prevX = x; //store co-ords again
board.tiles[y][x].setStyle("-fx-background-color: green; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
System.out.println("AI: Just hit your ship!"); //print ai hit your ship
board.tiles[y][x].shotAt = true; //shot at is now true so it cant fire on it again
board.tiles[y][x].shipOnTile.isHit(); //Call isHit function on the relevant ship
if (board.tiles[y][x].shipOnTile.isDestroyed) { //if the ship is destroyed
board.hitTruth = 1; //if ship is destroyed, go back to random fire
board.pShipsLeft--; //remove 1 pShipsleft value
System.out.println("AI: Just sunk a " + board.tiles[y][x].shipOnTile); //print is been destroyed
} else {
System.out.println("Hit"); //print Hit
}
if (board.pShipsLeft == 0) { //if there are no more ships on players board
System.out.println("You Lost!"); //display they lost
}
board.AIturn = false; //end AIturn
board.playerTurn = true; //start playerTurn
}
else if (!board.tiles[y][x].shotAt && !board.tiles[y][x].isShip){ //if the tile hasnt been hit, and wasnt a ship.
board.hitTruth = 2; //go back to random fire
board.tiles[y][x].setStyle(""); //apply these changes
board.tiles[y][x].shotAt = true; //set tile shotAt value to true
System.out.println("AI: Missed!"); //display ai missed
board.AIturn = false; //end AIturn
board.playerTurn = true; //start playerTurn
}
}
}
\ No newline at end of file
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Popup;
import javafx.stage.Stage;
import java.util.ArrayList;
//TO DO:
public class Board extends Application {
public static void main(String[] args) {
launch(args);
}
public int gridX; //create everything needed
public int gridY;
private GridPane grid;
private GridPane gridAI;
public Tile[][] tiles;
private Tile[][] tilesAI;
private ArrayList<Ships> AIships;
private ArrayList<Ships> pShips;
private boolean placingShipPhase = true;
public boolean firingPhase = false;
public boolean playerTurn = true;
public boolean AIturn = false;
private int AIcurrentShipIndex;
private int pCurrentShipIndex;
private boolean isPlayer;
public int pShipsLeft;
private int AIshipsLeft;
public int hitTruth = 1;
public AI ai;
@Override
public void start(Stage Battleships) {
Battleships.setTitle("Battleships"); //set title to Battleships
/* GRID SETUP #*/
GridPane introLayout = new GridPane(); //Create gridpane
HBox layout = new HBox(); //create contianer GridPane
this.grid = new GridPane(); //Create a GridPane called Grid
this.gridAI = new GridPane();
Label prompt = new Label("How big do you want the board to be?"); //create buttons, labels and textfields for window
Label height = new Label("Height:");
Label width = new Label("Width:");
Label ships = new Label("How many ships of each type?");
Label Battleship = new Label("How many Battleships (4 long)?");
Label Carrier = new Label("How many Carriers (5 long)?");
Label Destroyer = new Label("How many Destroyers (3 long)?");
Label Patrol = new Label("How many Patrols (2 long)?");
TextField getBattleship = new TextField("");
TextField getCarrier = new TextField("");
TextField getDestroyer = new TextField("");
TextField getPatrol = new TextField("");
TextField getX = new TextField("");
TextField getY = new TextField("");
Button submit = new Button("Submit");
Popup error = new Popup();
Label command = new Label("Ensure both textfields are filled, and are 26 or below.");
Button reset = new Button("Reset Grid");
grid.getChildren().add(reset); //add reset button to grid
reset.setOnAction(e -> {
resetGrid(); //call resetGridFunction
System.out.println("Board Cleared!"); //print Board Cleared
});
layout.getChildren().addAll(grid, gridAI, reset); //add player grid, AIgrid, and reset to Hbox
error.getContent().add(command); //add "command" Label to the popup
introLayout.setPadding(new Insets(10, 10, 10, 10)); //set padding
introLayout.setHgap(10); //set gap between columns
introLayout.setVgap(10); //set gap between rows
introLayout.add(prompt, 0, 0); //add elements to specific position of "introintroLayout" gridPane
introLayout.add(height, 0, 1);
introLayout.add(width, 0, 2);
introLayout.add(getX, 1, 2);
introLayout.add(getY, 1, 1);
introLayout.add(submit, 0, 8);
introLayout.add(ships, 0, 3);
introLayout.add(Battleship, 0, 4);
introLayout.add(Carrier, 0, 5);
introLayout.add(Destroyer, 0, 6);
introLayout.add(Patrol, 0, 7);
introLayout.add(getBattleship, 1, 4);
introLayout.add(getCarrier, 1, 5);
introLayout.add(getDestroyer, 1, 6);
introLayout.add(getPatrol, 1, 7);
submit.setOnAction(event -> { //When the user clicks the "submit" button...
String userX = getX.getText(); //userX = what the user entered for X value
gridX = Integer.parseInt(userX); //pass String as Int variable
String userY = getY.getText(); //userY = what the user entered for Y value
gridY = Integer.parseInt(userY); //pass String as Int variable
CreateGrid(Battleships, introLayout, gridX, gridY); //create player grid with User inputs
CreateAIGrid(Battleships, introLayout, gridX, gridY); //create ai grid with same inputs
Battleships.setScene(new Scene(layout, 1000, 1000)); //Change the scene to grid
});
Battleships.setScene(new Scene(introLayout, 400, 400));
Battleships.show();
}
void StartPlacingShips() {
Ships pCarrier = new Ships(5); //create Ship Objects
Ships pBattleship = new Ships(4);
Ships pSubmarine = new Ships(3);
Ships pDestroyer = new Ships(2);
Ships AIcarrier = new Ships(5); //create Ship Objects
Ships AIbattleship = new Ships(4);
Ships AIsubmarine = new Ships(3);
Ships AIdestroyer = new Ships(2);
this.AIships = new ArrayList<Ships>(); //Create ArrayList of ship objects so you cna change the size (based off user input)
this.AIships.add(AIcarrier); //add each shop to the ArrayList
this.AIships.add(AIbattleship);
this.AIships.add(AIsubmarine);
this.AIships.add(AIdestroyer);
this.AIcurrentShipIndex = 0; //index of ArrayList, used when cycling through while placing ships
this.placingShipPhase = true; //Placing phase is true
this.pShips = new ArrayList<Ships>();
this.pShips.add(pCarrier); //add each shop to the ArrayList
this.pShips.add(pBattleship);
this.pShips.add(pSubmarine);
this.pShips.add(pDestroyer);
this.pCurrentShipIndex = 0; //index of ArrayList, used when cycling through while placing ships
this.placingShipPhase = true; //Placing phase is true
}
private void CreateGrid(Stage Battleships, GridPane introLayout, int gridX, int gridY) {
isPlayer = true;
this.tiles = new Tile[gridY][gridX];
String[] horVal = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; //Declare column headers
String[] verVal = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}; //Declare row headers
for (int x = 0; x < gridX; x++) { //for every row
Label horLab = new Label(horVal[x]); //create new label, using index of horVal array as content
this.grid.add(horLab, x + 1, 0); //add this to the first row of Player Board
// can I add this in here? this.gridAI.add(horLab, x+1, 0); //add this to the first row of ai Board
}
for (int y = 0; y < gridY; y++) { //for every column do
Label verLab = new Label(verVal[y]); //create new label, using index of verVal array as content
this.grid.add(verLab, 0, y + 1); //add this to the first column in player Grid
}
for (int y = 1; y <= gridY; y++) { //for every row do....
for (int x = 1; x <= gridX; x++) { //for every column do...
Tile pTile = new Tile(); //create button, set sizes
pTile.setMinWidth(40);
pTile.setMaxWidth(40);
pTile.setMinHeight(40);
pTile.setMaxHeight(40);
pTile.setStyle(" -fx-font-size: 30px; -fx-background-color: null; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
//Apply these to every tile added
pTile.setOnMouseEntered(new EventHandler<MouseEvent>() { //when mouse enters the tile...
@Override
public void handle(MouseEvent event) {
if (!pTile.isShip) { //if the tile is not a ship
pTile.setStyle("-fx-background-color: white; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
}
}
});
pTile.setOnMouseExited(new EventHandler<MouseEvent>() { //when mouse enters the tile...
@Override
public void handle(MouseEvent event) {
if (!pTile.isShip || pTile.shotAt) { //if the tile is not a ship, or has been shot at
pTile.setStyle("-fx-background-color: null; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
}
}
});
pTile.setOnAction(event -> { //When the user clicks the "tile"
if (placingShipPhase) { //if placingShipPhase is true
if (canBePlaced(pTile.x, pTile.y, pTile.x + pShips.get(pCurrentShipIndex).length, pTile.y, tiles, true)) { //if the tile can be places using these coords
placeShip(pShips.get(pCurrentShipIndex), pTile.x, pTile.y, true); //Place the current ship on tile
pCurrentShipIndex++; //cycle through ship array
if (pCurrentShipIndex >= pShips.size()) { //if the index is equalto or higher than amount of ships
placingShipPhase = false; //end placing phase
firingPhase = true; //start firing phase
playerTurn = true; //Make the first turn the players
System.out.println("All ships placed!"); //print all ships placed
System.out.println("Take a shot!"); //print Take a Shot!
System.out.println("==============");//break text up
}
}
}
});
pTile.x = x; //tile.x is equal to x
pTile.y = y; //tile.y is equal to y
this.tiles[y - 1][x - 1] = pTile; //add the tile to the relevant array coord
this.grid.add(pTile, x, y); //add the tile to the grid
}
}
StartPlacingShips(); //call StartPlacingShips
}
private void CreateAIGrid(Stage Battleships, GridPane introLayout, int gridX, int gridY) { //copy of Create Grid to make one for ai
this.ai = new AI();
ai.board = this;
this.tilesAI = new Tile[gridY][gridX]; //create Array for ai Tile
isPlayer = false;
String[] horVal = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; //Declare column headers
String[] verVal = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}; //Declare row headers
for (int x = 0; x < gridX; x++) { //for every row
Label horLab = new Label(horVal[x]); //create new label, using index of horVal array as content
this.gridAI.add(horLab, x + 1, 0); //add this to the first row gridPane
}
for (int y = 0; y < gridY; y++) { //for every column do
Label verLab = new Label(verVal[y]); //create new label, using index of verVal array as content
this.gridAI.add(verLab, 0, y + 1); //add this to the first column in gridPane
}
for (int y = 1; y <= gridY; y++) { ///for every row do....
for (int x = 1; x <= gridX; x++) { //for every column do...
Tile AItile = new Tile(); //create button, set sizes
AItile.setMinWidth(40);
AItile.setMaxWidth(40);
AItile.setMinHeight(40);
AItile.setMaxHeight(40);
AItile.setStyle(" -fx-font-size: 30px; -fx-background-color: null; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
//Apply these to every tile added
AItile.setOnAction(event -> { //When the user clicks the "tile"
if (firingPhase && playerTurn == true) { //if firingPhase is true
if (AItile.shotAt == false && AItile.isShip) {//and if the tile hasn't been shot at, and is a ship
AItile.setStyle("-fx-background-color: green; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black;"); //apply these changes
AItile.shotAt = true; //set the tile shot At to be true
AItile.shipOnTile.isHit(); //Call isHit function on the relevant ship
if (AItile.shipOnTile.isDestroyed) {
System.out.println("Player: You just sunk a " + AItile.shipOnTile);
AIshipsLeft--; //remove 1 from AIshipsLeft
if (AIshipsLeft == 0) { //if ai has no ships left
System.out.println("Player: You Won!");
}
} else {
System.out.println("Player: Hit"); //print Hit
}
playerTurn = false; //end playerTurn
AIturn = true; //start ai turn
ai.randomFire();
/*
if (hitTruth == 1) {
ai.randomFire(); //call RandomFire
System.out.println("random fire");
} else if (hitTruth == 2) {
ai.findDir(); //call findDir
System.out.println("find dir");
} else if (hitTruth == 3) {
ai.keepGoing(); //call keepGoing
System.out.println("keep going");
} */
} else if (AItile.shotAt == false || !AItile.isShip) { //else if the tile hasn't been shot at, and the tile isn't a ship
AItile.setStyle("-fx-background-color: red; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black;"); //apply these changes
AItile.shotAt = true; //set the tile shotAt to be true
System.out.println("Player: Miss"); //print Miss
playerTurn = false; //End the player turn
AIturn = true; //start the ai turn
ai.randomFire();
} else if (AItile.shotAt) {
System.out.print("Player: Already shot at this tile, please pick another");
}
/*
if (hitTruth == 1) {
ai.randomFire(); //call randomFire
} else if (hitTruth == 2) {
ai.findDir(); //call findDir
} else if (hitTruth == 3) {
ai.keepGoing(); //call keepGoing
} else {
ai.randomFire();
}
*/
} else { //else
System.out.println("Tile has already been shot at, please pick another tile"); //Tell user to shoot again
}
});
AItile.x = x; //tile.x = x coord
AItile.y = y; //tile.y = y coord
this.tilesAI[y - 1][x - 1] = AItile; //add the tile to the relevant x-y coord in the Array
this.gridAI.add(AItile, x, y); //Add the tile to the relevant x/y coord on the grid
}
}
StartPlacingShips();
if (placingShipPhase) { //if it is the placing ship phase
while (AIcurrentShipIndex < AIships.size()) { //while the index is within the boundaries of AIships length
int y = (int) (Math.random() * gridY) + 1; //get a random number between 0 and gridY, add 1
int x = (int) (Math.random() * gridX) + 1; //sane for x
if (canBePlaced(x, y, x + AIships.get(AIcurrentShipIndex).length, y, tilesAI, false)) { //if the ship can be placed on this tile and wont be out of bounds
placeShip(AIships.get(AIcurrentShipIndex), x, y, false); //place the ship there
AIcurrentShipIndex++; //iterate through the ship ArrayList
tilesAI[y - 1][x - 1].setStyle("-fx-background-color: null; -fx-font-size: 30px; -fx-border-width: 1px; -fx-border-color: black;");
if (AIcurrentShipIndex >= AIships.size()) { //If the index reaches the end of the ArrayList
}
}
}
}
}
private void resetGrid() {
for (int y = 0; y < tiles.length; y++) { //for every row do....
for (int x = 0; x < tiles[y].length; x++) { //for every column do...
tiles[y][x].isShip = false; //set each tile isSHip to false
tiles[y][x].shotAt = false; //set each tile shotAt to false
tiles[y][x].setStyle(" -fx-font-size: 30px; -fx-background-color: null; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
}
}
for (int y = 0; y < tilesAI.length; y++) { //for every row do....
for (int x = 0; x < tilesAI[y].length; x++) { //for every column do...
tilesAI[y][x].isShip = false; //set each tile isSHip to false
tilesAI[y][x].shotAt = false; //set each tile shotAt to false
tilesAI[y][x].setStyle(" -fx-font-size: 30px; -fx-background-color: null; -fx-border-width: 1px; -fx-border-color: black; "); //apply these style changes
}
}
pCurrentShipIndex = 0; //set player ship index to 0
AIcurrentShipIndex = 0; //set ai ship index to 0
placingShipPhase = true; //set placingshipphase to true
}
private boolean canBePlaced(int startX, int startY, int endX, int endY, Tile tile[][], boolean isPlayer) {
for (int x = startX; x <= endX; x++) { //for every row space the ship occupies
for (int y = startY; y <= endY; y++) { //for every column space the ship occupies
try { //try the following
if (tiles[y - 1][x - 1].isShip) { //if any of the tiles in the way are a ship
System.out.println("is ship");
return false;
}
} catch (ArrayIndexOutOfBoundsException e) { //catch an OutOfBounds error
System.out.println("off board"); //print off board
return false;
}
}
}
return true; //else return true
}
private void placeShip(Ships ship, int xPos, int yPos, boolean isPlayer) {
xPos--; //cycle down the xPos
yPos--; //cycle down the yPos
for (int x = xPos; x < xPos + ship.length; x++) {
if (isPlayer) {
tiles[yPos][x].isShip = true; //make each tile isShip value true
tiles[yPos][x].shipOnTile = ship; //make each tile shipOnTile value reference the ship
tiles[yPos][x].shotAt = false;
tiles[yPos][x].setStyle("-fx-background-color: black;"); //apply these changes
pShipsLeft++;
} else {
AIshipsLeft++;
tilesAI[yPos][x].isShip = true; //make each tile isShip value true
tilesAI[yPos][x].shipOnTile = ship; //make each tile shipOnTile value true
tilesAI[yPos][x].shotAt = false; //make each tile shipOnTile value true
}
}
}
}
\ No newline at end of file
import java.util.ArrayList;
public class Ships {
int health;
int length;
int endX;
int endY;
boolean isDestroyed = false;
public Ships(int length) {
this.length = length; //give each Ship a diff length variable
this.health = this.length; //set the health fo the ship to be the length
this.isDestroyed = false; //ship is alive on placement
}
public boolean isHit() {
this.health--; //take 1 health off when the ship is hit
if(this.health == 0) { //if the health hits 0
this.isDestroyed = true; //destroyed is true
}
return this.isDestroyed; //return destroyed
}
}
import javafx.scene.control.Button;
public class Tile extends Button { //all base tiles on creation
public boolean isShip = false; //aren't a ship
public boolean shotAt = false; //hasn't been shot at
public Ships shipOnTile; //create an empty variable to be filled later
public int x = 0; //assign an x value to tile
public int y = 0; //assign a y value to tile
}
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