Commit 84f7fb66 authored by william.hill1's avatar william.hill1

Added music

parent 4a78b8e8
...@@ -12,232 +12,191 @@ import javafx.scene.control.Alert.AlertType; ...@@ -12,232 +12,191 @@ import javafx.scene.control.Alert.AlertType;
import javafx.scene.media.Media; import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaPlayer;
public class Main extends Application {
int[] legnth = { 5, 4, 4, 3, 3, 2, 2, 2 };
public class Main extends Application {
int[] legnth = {5, 4, 4, 3, 3, 2, 2, 2};
int counter = 0; int counter = 0;
int EnemyCounter = 25; int EnemyCounter = 25;
int PlayerCounter = 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];
MyRectangle[][] playerRects = new MyRectangle[Boardsize][Boardsize]; MyRectangle[][] playerRects = new MyRectangle[Boardsize][Boardsize];
MyRectangle[][] enemyRects = new MyRectangle[Boardsize][Boardsize]; MyRectangle[][] enemyRects = new MyRectangle[Boardsize][Boardsize];
Group group = new Group();
{
Group group = new Group (); for (int y = 0; y < 10; ++y) {
{ for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) MyRectangle r2 = new MyRectangle();
{ r2.setX((90 * x) + 900);
for (int x = 0; x < 10; ++x) r2.setY(90 * y);
{ r2.setWidth(90);
MyRectangle r2 = new MyRectangle (); r2.setHeight(90);
r2.setX((90 * x) + 900); r2.setFill(Color.TURQUOISE);
r2.setY(90 * y); r2.setStroke(Color.RED);
r2.setWidth(90); group.getChildren().add(r2);
r2.setHeight(90); enemyRects[x][y] = r2;
r2.setFill(Color.TURQUOISE);
r2.setStroke(Color.RED); MyRectangle r = new MyRectangle();
group.getChildren().add(r2); r.setX(90 * x);
enemyRects[x][y] = r2; r.setY(90 * y);
r.setWidth(90);
r.setHeight(90);
MyRectangle r = new MyRectangle (); r.setFill(Color.BLUE);
r.setX(90 * x); r.setStroke(Color.WHITE);
r.setY(90 * y); group.getChildren().add(r);
r.setWidth(90); playerRects[x][y] = r;
r.setHeight(90);
r.setFill(Color.BLUE); final int xc = x;
r.setStroke(Color.WHITE); final int yc = y;
group.getChildren().add(r); final Label l = new Label("");
playerRects[x][y] = r;
int max = 9;
final int xc = x; int min = 0;
final int yc = y; int range = max - min + 1;
final Label l = new Label("");
r2.setOnMouseClicked(event -> {
if (counter == legnth.length) {
int max = 9; if (enemyRects[xc][yc].getFill() == Color.TURQUOISE
int min = 0; || enemyRects[xc][yc].getFill() == Color.BLACK) {
int range = max - min + 1;
if (enemyRects[xc][yc].getNumber() > 0) {
enemyRects[xc][yc].setFill(Color.RED);
EnemyCounter -= 1;
r2.setOnMouseClicked(event -> { } else {
if(counter == legnth.length) { enemyRects[xc][yc].setFill(Color.GREY);
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
{ boolean placed2 = false;
playerRects[rand][rand2].setFill(Color.GREY); 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));
} }
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();
} }
if (PlayerCounter == 0) {
});
// Above: Enemy grid. Below: Player Grid
r.setOnMouseClicked(event -> {
if(counter < legnth.length) {
boolean colourcheck = false;
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++) {
String DusicFile = "loser.mp3";
Media dound = new Media(new File(DusicFile).toURI().toString());
MediaPlayer dediaPlayer = new MediaPlayer(dound);
System.out.println(legnth); dediaPlayer.play();
Alert alert2 = new Alert(AlertType.INFORMATION);
alert2.setTitle("You lost");
playerRects[xc+m][yc].setFill(Color.GREEN); alert2.setHeaderText(null);
playerRects[xc+m][yc].setNumber(legnth[counter]); alert2.setContentText("Better look next time!");
alert2.showAndWait();
System.exit(-1);
} }
if (EnemyCounter == 0) {
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) {
String musicFile = "cheering.mp3";
for(int p = 0;p < legnth[counter]; p++) { Media sound = new Media(new File(musicFile).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
enemyRects[rand+p][rand2].setFill(Color.BLACK); Alert alert3 = new Alert(AlertType.INFORMATION);
enemyRects[rand+p][rand2].setNumber(legnth[counter]); alert3.setTitle("You Won!!!");
alert3.setHeaderText(null);
alert3.setContentText("Congratulations!!!");
alert3.showAndWait();
System.exit(-1);
} }
placed=true;
} });
}
} while (placed == false); // Above: Enemy grid. Below: Player Grid
r.setOnMouseClicked(event -> {
if (counter < legnth.length) {
boolean colourcheck = false;
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++) {
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) {
for (int p = 0; p < legnth[counter]; p++) {
enemyRects[rand + p][rand2].setFill(Color.BLACK);
enemyRects[rand + p][rand2].setNumber(legnth[counter]);
}
placed = true;
}
}
} while (placed == false);
}
counter++;
}
} }
counter++;
}
}
});
});
}
} }
Scene scene = new Scene(group, 1800, 900);
stage.setScene(scene);
stage.show();
} }
Scene scene = new Scene(group, 1800,900);
stage.setScene(scene);
stage.show();
}
} }
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }
} }
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