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
This diff is collapsed.
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