Commit 4a78b8e8 authored by william.hill1's avatar william.hill1

pretty much finished

parent 7a5b9d15
import java.awt.Label;
import java.io.File;
import javafx.application.Application; import javafx.application.Application;
import javafx.scene.Group; import javafx.scene.Group;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class Main extends Application {
public class Main extends Application {
int[] legnth = {5, 4, 4, 3, 3, 2, 2, 2};
int counter = 0;
int EnemyCounter = 25;
int PlayerCounter = 25;
public void start(Stage stage) { public void start(Stage stage) {
int Boardsize = 10; int Boardsize = 10;
int[][]PlayerShips = new int[Boardsize][Boardsize]; int[][]PlayerShips = new int[Boardsize][Boardsize];
Rectangle[][] playerRects = new Rectangle[Boardsize][Boardsize]; MyRectangle[][] playerRects = new MyRectangle[Boardsize][Boardsize];
MyRectangle[][] enemyRects = new MyRectangle[Boardsize][Boardsize];
...@@ -21,7 +34,18 @@ public class Main extends Application { ...@@ -21,7 +34,18 @@ public class Main extends Application {
{ {
for (int x = 0; x < 10; ++x) for (int x = 0; x < 10; ++x)
{ {
Rectangle r = new Rectangle (); MyRectangle r2 = new MyRectangle ();
r2.setX((90 * x) + 900);
r2.setY(90 * y);
r2.setWidth(90);
r2.setHeight(90);
r2.setFill(Color.TURQUOISE);
r2.setStroke(Color.RED);
group.getChildren().add(r2);
enemyRects[x][y] = r2;
MyRectangle r = new MyRectangle ();
r.setX(90 * x); r.setX(90 * x);
r.setY(90 * y); r.setY(90 * y);
r.setWidth(90); r.setWidth(90);
...@@ -33,33 +57,173 @@ public class Main extends Application { ...@@ -33,33 +57,173 @@ public class Main extends Application {
final int xc = x; final int xc = x;
final int yc = y; final int yc = y;
final Label l = new Label("");
int max = 9;
int min = 0;
int range = max - min + 1;
r2.setOnMouseClicked(event -> {
if(counter == legnth.length) {
if (enemyRects[xc][yc].getFill() == Color.TURQUOISE || enemyRects[xc][yc].getFill() == Color.BLACK) {
if (enemyRects[xc][yc].getNumber() > 0 ) {
enemyRects[xc][yc].setFill(Color.RED);
EnemyCounter -=1;
}
else
{
enemyRects[xc][yc].setFill(Color.GREY);
}
boolean placed2 = false;
do {
placed2 = false;
int rand2 = (int)(Math.random() * range) + min;
int rand = (int)(Math.random() * range) + min;
if (playerRects[rand][rand2].getFill() == Color.BLUE || playerRects[rand][rand2].getFill() == Color.GREEN ){
placed2 = true;
if (playerRects[rand][rand2].getNumber() > 0 ) {
playerRects[rand][rand2].setFill(Color.RED);
PlayerCounter -=1;
}
else
{
playerRects[rand][rand2].setFill(Color.GREY);
}
}
System.out.println(rand);
} while (placed2 == false && (PlayerCounter > 0 || EnemyCounter >0));
}
}
if (PlayerCounter == 0) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("You lost");
alert.setHeaderText(null);
alert.setContentText("Better look next time!");
alert.showAndWait();
String DusicFile = "loser.mp3";
Media dound = new Media(new File(DusicFile).toURI().toString());
MediaPlayer dediaPlayer = new MediaPlayer(dound);
dediaPlayer.play();
}
if (EnemyCounter == 0) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("You Won!!!");
alert.setHeaderText(null);
alert.setContentText("Congratulations!!!");
alert.showAndWait();
String musicFile = "cheering.mp3";
Media sound = new Media(new File(musicFile).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
}
});
// Above: Enemy grid. Below: Player Grid
r.setOnMouseClicked(event -> { r.setOnMouseClicked(event -> {
int legnth = 5; if(counter < legnth.length) {
boolean colourcheck = false;
System.out.println("click at " + xc + " " + yc); System.out.println("click at " + xc + " " + yc);
if (xc + legnth[counter] <= Boardsize) {
for(int q = 0;q < legnth[counter]; q++) {
if (playerRects[xc+q][yc].getFill() == Color.GREEN) {
colourcheck = true;
}
}
if (colourcheck == false) {
for(int m = 0;m < legnth[counter]; m++) {
for(int i = 0;i < legnth; i++) {
legnth--;
System.out.println(legnth); System.out.println(legnth);
playerRects[xc+m][yc].setFill(Color.GREEN);
playerRects[xc+m][yc].setNumber(legnth[counter]);
}
System.out.println("click at " + xc + " " + yc);
boolean numbercheck = false;
boolean placed = false;
do {
int rand2 = (int)(Math.random() * range) + min;
int rand = (int)(Math.random() * range) + min;
placed = false;
numbercheck = false;
System.out.println(rand + " " + rand2);
if (rand + legnth[counter] < Boardsize) {
for(int j = 0;j < legnth[counter]; j++) {
if (enemyRects[rand + j][rand2].getNumber() > 0) {
numbercheck = true;
}
}
if (numbercheck == false) {
playerRects[xc][yc].setFill(Color.GREEN); for(int p = 0;p < legnth[counter]; p++) {
playerRects[xc+i][yc].setFill(Color.GREEN);
enemyRects[rand+p][rand2].setFill(Color.BLACK);
enemyRects[rand+p][rand2].setNumber(legnth[counter]);
}
placed=true;
}
} }
} while (placed == false);
}
counter++;
}
}
}); });
} }
} }
Scene scene = new Scene(group, 900,900); Scene scene = new Scene(group, 1800,900);
stage.setScene(scene); stage.setScene(scene);
stage.show(); stage.show();
} }
......
import javafx.scene.shape.Rectangle;
public class MyRectangle extends Rectangle {
int number = 0;
public MyRectangle() {
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
File added
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