I've learned JavaFX recently (with Scene Builder) and made a MineSweeper game, would love to receive feedback and tips for improvement! Thanks.
Below is mines class, I've commented out some lines for the GUI part to work:
import java.util.Random;
//import java.util.Scanner;
public class Mines {
private int height /* rows */, width /* columns */, numMines, totalToReveal /* excluding mines */;
public String[][] board;
private Random rand = new Random(); // Generate random numbers
private int h, w; // Assist with random received values
static int winLose=-1;
// @SuppressWarnings("resource")
public Mines(int height, int width, int numMines) {
/* int newMines = -1; */
// boolean checkMinesNum = false; // If number of mines is legal
// Scanner sc = new Scanner(System.in);
this.height = height;
this.width = width;
/*
* if (numMines < width * height && numMines >= 0) checkMinesNum = true; while
* (checkMinesNum != true) {
* System.out.println("Please enter a valid number of mines (lower than " +
* height * width + "):"); newMines = sc.nextInt(); if (newMines < width *
* height && newMines >= 0) { checkMinesNum = true; numMines = newMines; } } //
* sc.close(); // Can't do, because I need to close at the end of Main class
*/
this.numMines = numMines;
totalToReveal = (height * width) - this.numMines;
board = new String[height][width]; // Create game's board with nulls initialized
for (int i = 0; i < height; i++) // Initialize all slots in the board
for (int j = 0; j < width; j++)
board[i][j] = "NF."; // Nothing ON slot, False none is revealed yet and . as default
while (numMines != 0) {
h = rand.nextInt(height - 1); // Generate random number for height
w = rand.nextInt(width - 1); // Generate random number for width
if (board[h][w].charAt(2) != 'M') { // Make sure place wasn't used before
board[h][w] = board[h][w].substring(0, 2) + "M"; // Mark a place containing a mine with M
numMines--;
}
}
for (int i = 0; i < height; i++) // Initialize slot values
for (int j = 0; j < width; j++)
board[i][j] = board[i][j].substring(0, 2) + get(i, j);
}
public int getCol() {
return width;
}
public int getRow() {
return height;
}
private void checkPlace(int i, int j) { // Is it a legal move? (borders)
if (i >= height || j >= width || i < 0 || j < 0)
throw new IllegalArgumentException("Illegal move on board!");
}
private boolean inBoard(int i, int j) { // Is move legal? (make move or not)
if (i >= height || j >= width || i < 0 || j < 0)
return false;
return true;
}
public boolean addMine(int i, int j) {
checkPlace(i, j);
if (board[i][j].contains("M")) {
System.out.println("Adding a mine has failed, slot already contains one!");
return false;
}
board[i][j] = board[i][j].substring(0, 2) + "M";
numMines++; // Mine was added
totalToReveal--; // Subtract one slot (occupied by mine)
for (int r = 0; r < height; r++) // Initialize slot values
for (int c = 0; c < width; c++)
board[r][c] = board[r][c].substring(0, 2) + get(r, c);
return true;
}
public boolean open(int i, int j) {
checkPlace(i, j);
if (board[i][j].charAt(1) == 'T') // Already revealed
return true;
if (board[i][j].charAt(2) == 'M') { // Slot contains a mine (user lost)
board[i][j] = board[i][j].substring(0, 1) + "T" + "B"; // B for boom
totalToReveal = 0; // Finish game
isDone();
System.out.println("Game over, boy");
winLose=0;
return true;
}
if (board[i][j].charAt(2) != 'E' && board[i][j].charAt(2) != 'M' && board[i][j].charAt(2) != 'B') {
board[i][j] = board[i][j].substring(0, 1) + "T" + board[i][j].substring(2, 3);
totalToReveal--;
if (isDone()) {
System.out.println("You win, boy");
winLose=1;
}
return true;
}
if (board[i][j].charAt(2) == 'E') {
board[i][j] = board[i][j].substring(0, 1) + "T" + board[i][j].substring(2, 3);
totalToReveal--;
for (int r = -1; r < 2; r++) {
for (int c = -1; c < 2; c++) {
if (!(r == 0 && c == 0) && inBoard(i + r, j + c)) {
if (isDone()) {
System.out.println("You win, boy");
winLose=1;
}
open(i + r, j + c);
}
}
}
return true;
}
return false;
}
public void toggleFlag(int x, int y) {
checkPlace(x, y);
if (board[x][y].contains("T"))
return;
board[x][y] = "D" + board[x][y].substring(1, 3); // Overwrite it with flag mark (D)
}
public void toggleQM(int x, int y) {
checkPlace(x, y);
if (board[x][y].contains("T"))
return;
board[x][y] = "?" + board[x][y].substring(1, 3); // Overwrite it with question mark (?)
}
public boolean isDone() {
if (totalToReveal != 0)
return false;
setShowAll();
return true; // True if no more slots to reveal (won the game)
}
public String get(int i, int j) {
checkPlace(i, j);
Integer countMines = 0; // To use the object's toString method
if (board[i][j].contains("F")) { // Slot not revealed
if (board[i][j].contains("D")) // Slot has flag
return "F"; // F for flag
else if (board[i][j].contains("?")) // Slot is marked as question mark (unsure of slot's content)
return "?";
} // Remaining slots are revealed
if (board[i][j].contains("M"))
return "M";
else {
for (int r = -1; r < 2; r++) {
for (int c = -1; c < 2; c++) {
if (!(r == 0 && c == 0) && inBoard(i + r, j + c) && board[i + r][j + c].contains("M")) {
countMines++;
if (countMines > 8 || countMines < 0)
throw new IllegalArgumentException("Something went wrong - mines count is illegal");
}
}
}
}
if (countMines == 0)
return "E";
else
return countMines.toString();
}
public void setShowAll() { // Set all slots to be revealed when game is over (win\lose)
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
if (!board[i][j].contains("T"))
board[i][j] = board[i][j].substring(0, 1) + "T" + board[i][j].substring(2, 3);
}
@Override
public String toString() {
String s = "";
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
if (board[r][c].charAt(1) == 'T')
s += board[r][c].substring(2, 3);
else { // Meaning slot isn't revealed
if (board[r][c].contains("D") || board[r][c].contains("?"))
s += get(r, c);
else
s += "X";
}
if (c == width - 1) // New row
s += '\n';
}
}
return s;
}
}
the mainFX:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;
public class MainFX extends Application {
static Media h = new Media("https://www.mboxdrive.com/Flavour-Wataboi.mp3");
static MediaPlayer mediaPlayer = new MediaPlayer(h);;
static int onOoff = 0;
static int check = 0, check2 = 0;
@Override
public void start(Stage primaryStage) {
try {
Image backgroundColor = new Image("https://i.ibb.co/1KvhFmp/menu.jpg");
BackgroundSize backgroundSize = new BackgroundSize(1280, 720, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("MainMenuFXML.fxml")); // fxml location
VBox root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/menu.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
// primaryStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
primaryStage.setTitle("BOOM Beach"); // Set stage's title
// primaryStage.setMinWidth(400); // Won't be allowed to make width/height smaller
// primaryStage.setMinHeight(350);
// primaryStage.setMaxWidth(600); // It will put constraints on the new scenes!!
// primaryStage.setMaxHeight(450);
primaryStage.setScene(scene); // Set scene to stage
primaryStage.show(); // Show stage
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public static boolean music() {
check = onOoff;
check2 = check % 2;
if (check2 == 0) {
mediaPlayer.play();
onOoff++;
return true;
} else {
mediaPlayer.stop();
onOoff++;
return false;
}
}
}
main menu controller:
import java.io.IOException;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.stage.Stage;
public class MainMenuCONTROLLER {
@FXML
private Button NewGame;
@FXML
private Button QuitGame;
@FXML
private Button Extras;
@FXML
private Tooltip ExtrasTT;
@FXML
private Button ExtraExtras;
@FXML
void ExtraExtras(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1Gr7PhP/Mandalorian.png");
BackgroundSize backgroundSize = new BackgroundSize(100, 100, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("ExtraExtrasFXML.fxml"));
AnchorPane root = loader.load();
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/Secret.jpg\")");
Scene scene = new Scene(root);
Stage extrasStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
extrasStage.setTitle("Any guesses?");
// extrasStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
/*
* extrasStage.setMinHeight(700); extrasStage.setMinWidth(500);
* extrasStage.setMaxHeight(700); extrasStage.setMaxWidth(500);
*/
extrasStage.setScene(scene);
extrasStage.show();
}
@FXML
void Extras(ActionEvent event) {
Extras.setOnAction(e -> {
boolean check = MainFX.music();
if (check)
Extras.setText("ON");
else
Extras.setText("OFF");
});
}
@FXML
void NewGame(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1Gr7PhP/Mandalorian.png");
BackgroundSize backgroundSize = new BackgroundSize(1280, 853, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("NewGameFXML.fxml"));
AnchorPane root = loader.load();
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/Mandalorian.png\")");
Scene scene = new Scene(root);
Stage GameStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// GameStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
// GameStage.setMinWidth(1000); // Won't be allowed to make width/height smaller
// GameStage.setMinHeight(500);
// GameStage.setMaxWidth(2000);
// GameStage.setMaxHeight(1000);
GameStage.setTitle("BOOM Beach");
GameStage.setScene(scene);
GameStage.show();
}
@FXML
void QuitGame(ActionEvent event) {
Platform.exit(); // Quit game
}
}
main menu fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Tooltip?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.MainMenuCONTROLLER">
<children>
<Button fx:id="NewGame" mnemonicParsing="false" onAction="#NewGame" prefHeight="20.0" prefWidth="140.0" text="New Game">
<VBox.margin>
<Insets bottom="30.0" right="10.0" top="20.0" />
</VBox.margin>
<font>
<Font size="20.0" />
</font>
</Button>
<Button mnemonicParsing="false" prefHeight="50.0" prefWidth="140.0" text="ScoreBoard (later)">
<VBox.margin>
<Insets bottom="30.0" right="10.0" top="10.0" />
</VBox.margin>
<tooltip>
<Tooltip contentDisplay="CENTER" height="40.0" text="In the next update..." textAlignment="CENTER" width="150.0">
<font>
<Font name="Arial Black" size="9.0" />
</font>
</Tooltip>
</tooltip>
</Button>
<Button fx:id="QuitGame" mnemonicParsing="false" onAction="#QuitGame" prefHeight="50.0" prefWidth="140.0" text="Quit Game">
<VBox.margin>
<Insets bottom="10.0" right="10.0" top="10.0" />
</VBox.margin>
<font>
<Font size="20.0" />
</font>
</Button>
<HBox alignment="CENTER" prefHeight="60.0" prefWidth="600.0">
<children>
<Button fx:id="Extras" mnemonicParsing="false" onAction="#Extras" prefHeight="30.0" prefWidth="110.0" text="Check this out">
<HBox.margin>
<Insets bottom="10.0" right="10.0" />
</HBox.margin>
<font>
<Font size="14.0" />
</font>
<tooltip>
<Tooltip fx:id="ExtrasTT" contentDisplay="CENTER" height="40.0" text="Not really..." textAlignment="CENTER" width="150.0">
<font>
<Font name="Arial Black" size="9.0" />
</font>
</Tooltip>
</tooltip>
</Button>
<Button fx:id="ExtraExtras" mnemonicParsing="false" onAction="#ExtraExtras" prefHeight="30.0" prefWidth="110.0" text="Top secret">
<HBox.margin>
<Insets bottom="10.0" left="10.0" right="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
</children>
</VBox>
extra extras controller:
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ExtraExtrasCONTROLLER {
@FXML
private AnchorPane ExtraExtrasPIC;
@FXML
private Button BackMainMenu;
@FXML
void MainMenu(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1KvhFmp/menu.jpg");
BackgroundSize backgroundSize = new BackgroundSize(1280, 720, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("MainMenuFXML.fxml")); // fxml location
VBox root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/menu.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// primaryStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
primaryStage.setTitle("BOOM Beach"); // Set stage's title
// primaryStage.setMinWidth(400); // Won't be allowed to make width/height smaller
// primaryStage.setMinHeight(350);
// primaryStage.setMaxWidth(600);
// primaryStage.setMaxHeight(450);
primaryStage.setScene(scene); // Set scene to stage
primaryStage.show(); // Show stage
}
}
extra extras fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="ExtraExtrasPIC" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.ExtraExtrasCONTROLLER">
<children>
<Button fx:id="BackMainMenu" alignment="BOTTOM_LEFT" layoutX="14.0" layoutY="600.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#MainMenu" prefHeight="30.0" prefWidth="120.0" text="Back to menu" textAlignment="CENTER" AnchorPane.bottomAnchor="65.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="365.0" AnchorPane.topAnchor="600.0" />
</children>
</AnchorPane>
new game controller:
import java.io.IOException;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class NewGameCONTROLLER {
static Mines game;
static int rows, columns, mines; // To access them even when resetting board
Button b;
static int mouseClick = -1;
@FXML
private TextField NumRows;
@FXML
private TextField NumCols;
@FXML
private TextField NumM;
@FXML
private Button StartGame;
@FXML
private Button BackMainMenu;
@FXML
private Button RandomGame;
@FXML
private Button Music;
@FXML
void BackMainMenu(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1KvhFmp/menu.jpg");
BackgroundSize backgroundSize = new BackgroundSize(1280, 720, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("MainMenuFXML.fxml")); // fxml location
VBox root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/menu.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// primaryStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
primaryStage.setTitle("BOOM Beach"); // Set stage's title
// primaryStage.setMinWidth(400); // Won't be allowed to make width/height smaller
// primaryStage.setMinHeight(350);
// primaryStage.setMaxWidth(600);
// primaryStage.setMaxHeight(450);
primaryStage.setScene(scene); // Set scene to stage
primaryStage.show(); // Show stage
}
@FXML
void RandomGame(ActionEvent event) throws IOException {
Random rand = new Random();
int low = 3, high = 15;
int minesLow = 1, minesHigh;
rows = rand.nextInt(high - low) + low;
columns = rand.nextInt(high - low) + low;
if (rows <= 0 || columns <= 0) {
rows = rand.nextInt(5) + 3;
columns = rand.nextInt(5) + 3;
}
minesHigh = (rows * columns) - 1;
mines = rand.nextInt(minesHigh - minesLow) + minesLow;
if (mines >= columns * rows)
mines = (columns * rows) - 1;
game = new Mines(rows, columns, mines);
Image backgroundColor = new Image("https://i.ibb.co/1Gr7PhP/Mandalorian.png");
BackgroundSize backgroundSize = new BackgroundSize(1280, 853, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("BoardFXML.fxml")); // fxml location
AnchorPane root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/Mandalorian.png\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage gameStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// gameStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
gameStage.setTitle("BOOM Beach"); // Set stage's title
// gameStage.setMinWidth(1000); // Won't be allowed to make width/height smaller
// gameStage.setMinHeight(500);
// gameStage.setMaxWidth(2000);
// gameStage.setMaxHeight(1000);
gameStage.setScene(scene); // Set scene to stage
BoardCONTROLLER bCont = loader.getController(); // Prepare board in BoardCONTROLLER
bCont.winLose.setVisible(false);
for (int i = 0; i < columns; i++) {
for (int j = 0; j < NewGameCONTROLLER.rows; j++) {
b = new Button(" ");
b.setMinSize(30, 30);
b.setMaxSize(30, 30);
b.setStyle("-fx-font-size:11");
bCont.TheBoard.add(b, i, j);
GridPane.setMargin(b, new Insets(5, 5, 5, 5));
GridPane.setHalignment(b, HPos.CENTER);
GridPane.setValignment(b, VPos.CENTER);
}
}
for (int i = 0; i < bCont.TheBoard.getChildren().size(); i++) {
((ButtonBase) bCont.TheBoard.getChildren().get(i)).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int x, y;
y = (int) ((Button) event.getSource()).getProperties().get("gridpane-column");
x = (int) ((Button) event.getSource()).getProperties().get("gridpane-row");
if (event.getButton().equals(MouseButton.PRIMARY))
game.open(x, y);
else if (event.getButton().equals(MouseButton.SECONDARY))
game.toggleFlag(x, y);
else if (event.getButton().equals(MouseButton.MIDDLE))
game.toggleQM(x, y);
for (Node child : bCont.TheBoard.getChildren()) {
int j = (int) ((Button) child).getProperties().get("gridpane-column");
int i = (int) ((Button) child).getProperties().get("gridpane-row");
if (game.board[i][j].charAt(1) == 'T') {
((Button) child).setText(game.board[i][j].substring(2, 3));
}
if (game.board[i][j].charAt(1) == 'F' && game.board[i][j].charAt(0) == 'D')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GtnDxdQ/flag.png)");
if (game.board[i][j].charAt(1) == 'F' && game.board[i][j].charAt(0) == '?')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/fnw3Hr4/QM.jpg)");
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'E') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/txvBhCS/empty.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'M') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/6Jq9S3g/mine.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'B') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wgRt8v9/boom.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '1') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/5Gd2H0Y/1.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '2') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GHGj0KM/2.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '3') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/sww9b1n/3.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '4') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/nrTjpqp/4.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '5') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/gZ6Rjrz/5.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '6') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wh9XnfT/6.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '7') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/k1905rT/7.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '8') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/RCs2s2H/8.jpg)");
((Button) child).setText(" ");
}
if (Mines.winLose==1) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU WIN!");
}
if (Mines.winLose==0) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU LOSE! Try again...");
}
Mines.winLose=-1;
}
}
});
}
gameStage.show(); // Show stage
}
@FXML
void StartGame(ActionEvent event) throws IOException {
rows = Integer.parseInt(NumRows.getText());
columns = Integer.parseInt(NumCols.getText());
mines = Integer.parseInt(NumM.getText());
game = new Mines(rows, columns, mines);
Image backgroundColor = new Image("https://i.ibb.co/1Gr7PhP/Mandalorian.png");
BackgroundSize backgroundSize = new BackgroundSize(1280, 853, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("BoardFXML.fxml")); // fxml location
AnchorPane root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/Mandalorian.png\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage gameStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// gameStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
gameStage.setTitle("BOOM Beach"); // Set stage's title
// gameStage.setMinWidth(1000); // Won't be allowed to make width/height smaller
// gameStage.setMinHeight(500);
// gameStage.setMaxWidth(2000);
// gameStage.setMaxHeight(1000);
gameStage.setScene(scene); // Set scene to stage
BoardCONTROLLER bCont = loader.getController(); // Prepare board in BoardCONTROLLER
bCont.winLose.setVisible(false);
for (int i = 0; i < columns; i++) {
for (int j = 0; j < NewGameCONTROLLER.rows; j++) {
b = new Button(" ");
b.setMinSize(30, 30);
b.setMaxSize(30, 30);
b.setStyle("-fx-font-size:11");
bCont.TheBoard.add(b, i, j);
GridPane.setMargin(b, new Insets(5, 5, 5, 5));
GridPane.setHalignment(b, HPos.CENTER);
GridPane.setValignment(b, VPos.CENTER);
}
}
for (int i = 0; i < bCont.TheBoard.getChildren().size(); i++) {
((ButtonBase) bCont.TheBoard.getChildren().get(i)).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int x, y;
y = (int) ((Button) event.getSource()).getProperties().get("gridpane-column");
x = (int) ((Button) event.getSource()).getProperties().get("gridpane-row");
if (event.getButton().equals(MouseButton.PRIMARY))
game.open(x, y);
else if (event.getButton().equals(MouseButton.SECONDARY))
game.toggleFlag(x, y);
else if (event.getButton().equals(MouseButton.MIDDLE))
game.toggleQM(x, y);
for (Node child : bCont.TheBoard.getChildren()) {
int j = (int) ((Button) child).getProperties().get("gridpane-column");
int i = (int) ((Button) child).getProperties().get("gridpane-row");
if (game.board[i][j].charAt(1) == 'T') {
((Button) child).setText(game.board[i][j].substring(2, 3));
}
if (game.board[i][j].charAt(1) == 'F' && game.board[i][j].charAt(0) == 'D')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GtnDxdQ/flag.png)");
if (game.board[i][j].charAt(1) == 'F' && game.board[i][j].charAt(0) == '?')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/fnw3Hr4/QM.jpg)");
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'E') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/txvBhCS/empty.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'M') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/6Jq9S3g/mine.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == 'B') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wgRt8v9/boom.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '1') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/5Gd2H0Y/1.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '2') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GHGj0KM/2.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '3') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/sww9b1n/3.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '4') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/nrTjpqp/4.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '5') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/gZ6Rjrz/5.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '6') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wh9XnfT/6.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '7') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/k1905rT/7.jpg)");
((Button) child).setText(" ");
}
if (game.board[i][j].charAt(1) == 'T' && game.board[i][j].charAt(2) == '8') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/RCs2s2H/8.jpg)");
((Button) child).setText(" ");
}
if (Mines.winLose==1) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU WIN!");
}
if (Mines.winLose==0) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU LOSE! Try again...");
}
Mines.winLose=-1;
}
}
});
}
gameStage.show(); // Show stage
}
@FXML
void Music(ActionEvent event) {
Music.setOnAction(e -> {
boolean check = MainFX.music();
if (check)
Music.setText("ON");
else
Music.setText("OFF");
});
}
}
new game fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="853.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.NewGameCONTROLLER">
<children>
<VBox alignment="TOP_CENTER" prefHeight="171.0" prefWidth="980.0" AnchorPane.bottomAnchor="444.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<children>
<Text fill="RED" strokeType="OUTSIDE" strokeWidth="0.0" text="You can finally go on your long awaited vacation!!" textAlignment="CENTER">
<VBox.margin>
<Insets bottom="10.0" top="20.0" />
</VBox.margin>
<font>
<Font name="System Bold" size="20.0" />
</font>
</Text>
<Text fill="RED" strokeType="OUTSIDE" strokeWidth="0.0" text="The resort is just across the beach" textAlignment="CENTER">
<font>
<Font name="System Bold" size="20.0" />
</font>
<VBox.margin>
<Insets bottom="10.0" top="20.0" />
</VBox.margin>
</Text>
<Text fill="RED" strokeType="OUTSIDE" strokeWidth="0.0" text="DANGER! You entered BOOM Beach... You know what to do..." textAlignment="CENTER">
<font>
<Font name="System Bold" size="25.0" />
</font>
<VBox.margin>
<Insets top="30.0" />
</VBox.margin>
</Text>
</children>
</VBox>
<VBox alignment="CENTER" layoutX="396.0" layoutY="205.0" prefHeight="410.0" prefWidth="980.0" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="205.0">
<children>
<HBox alignment="TOP_CENTER" prefHeight="40.0" prefWidth="979.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Number of rows:">
<font>
<Font name="System Bold" size="18.0" />
</font>
<HBox.margin>
<Insets />
</HBox.margin>
</Text>
<TextField fx:id="NumRows" prefHeight="23.0" prefWidth="137.0" promptText="2 and above">
<font>
<Font size="10.0" />
</font>
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox alignment="TOP_CENTER" prefHeight="40.0" prefWidth="979.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Number of columns:">
<font>
<Font name="System Bold" size="18.0" />
</font>
<HBox.margin>
<Insets />
</HBox.margin>
</Text>
<TextField fx:id="NumCols" prefHeight="23.0" prefWidth="117.0" promptText="2 and above">
<font>
<Font size="10.0" />
</font>
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox alignment="TOP_CENTER" prefHeight="40.0" prefWidth="979.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Number of mines:">
<font>
<Font name="System Bold" size="18.0" />
</font>
<HBox.margin>
<Insets />
</HBox.margin>
</Text>
<TextField fx:id="NumM" prefHeight="23.0" prefWidth="133.0" promptText="1 and above">
<font>
<Font size="10.0" />
</font>
<HBox.margin>
<Insets left="5.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox alignment="TOP_CENTER" prefHeight="30.0" prefWidth="979.0">
<children>
<Button fx:id="StartGame" mnemonicParsing="false" onAction="#StartGame" prefHeight="30.0" prefWidth="140.0" text="Start" textFill="RED">
<font>
<Font name="System Bold" size="20.0" />
</font>
<HBox.margin>
<Insets />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</HBox>
<HBox alignment="TOP_CENTER" prefHeight="30.0" prefWidth="979.0">
<children>
<Button fx:id="BackMainMenu" mnemonicParsing="false" onAction="#BackMainMenu" prefHeight="30.0" text="Back to menu">
<HBox.margin>
<Insets />
</HBox.margin>
<font>
<Font size="20.0" />
</font>
</Button>
<Button fx:id="RandomGame" mnemonicParsing="false" onAction="#RandomGame" prefHeight="30.0" text="Random board" textFill="RED">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="20.0" />
</font>
</Button>
</children>
<VBox.margin>
<Insets bottom="10.0" />
</VBox.margin>
</HBox>
<Button fx:id="Music" alignment="TOP_CENTER" mnemonicParsing="false" onAction="#Music" prefHeight="30.0" text="Music">
<VBox.margin>
<Insets right="10.0" />
</VBox.margin>
<font>
<Font size="16.0" />
</font>
</Button>
</children>
<padding>
<Insets right="1.0" />
</padding>
</VBox>
</children>
</AnchorPane>
board controller:
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBase;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundImage;
import javafx.scene.layout.BackgroundPosition;
import javafx.scene.layout.BackgroundRepeat;
import javafx.scene.layout.BackgroundSize;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BoardCONTROLLER {
Button b;
@FXML
public GridPane TheBoard;
@FXML
public Label winLose;
@FXML
private Button BackMenu;
@FXML
private Button Music;
@FXML
private Button ResetBoard;
@FXML
void BackMenu(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1KvhFmp/menu.jpg");
BackgroundSize backgroundSize = new BackgroundSize(1280, 720, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("MainMenuFXML.fxml")); // fxml location
VBox root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/menu.jpg\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage primaryStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// primaryStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
primaryStage.setTitle("BOOM Beach"); // Set stage's title
// primaryStage.setMinWidth(400); // Won't be allowed to make width/height smaller
// primaryStage.setMinHeight(350);
// primaryStage.setMaxWidth(600);
// primaryStage.setMaxHeight(450);
primaryStage.setScene(scene); // Set scene to stage
primaryStage.show(); // Show stage
}
@FXML
void Music(ActionEvent event) {
Music.setOnAction(e -> {
boolean check = MainFX.music();
if (check)
Music.setText("ON");
else
Music.setText("OFF");
});
}
@FXML
void ResetBoard(ActionEvent event) throws IOException {
Image backgroundColor = new Image("https://i.ibb.co/1Gr7PhP/Mandalorian.png");
BackgroundSize backgroundSize = new BackgroundSize(1280, 853, true, true, true, true);
BackgroundImage backgroundImage = new BackgroundImage(backgroundColor, BackgroundRepeat.NO_REPEAT,
BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER, backgroundSize);
NewGameCONTROLLER.game = new Mines(NewGameCONTROLLER.rows, NewGameCONTROLLER.columns, NewGameCONTROLLER.mines);
FXMLLoader loader = new FXMLLoader(); // Create loading object
loader.setLocation(getClass().getResource("BoardFXML.fxml")); // fxml location
AnchorPane root = loader.load(); // Load layout
// root.setStyle("-fx-background-image: url(\"file:///C:/EclipseProjects/MineSweeper/src/MS/Mandalorian.png\")");
Scene scene = new Scene(root); // Create scene with chosen layout
Stage gameStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
// gameStage.setFullScreen(true);
root.setBackground(new Background(backgroundImage));
gameStage.setTitle("BOOM Beach"); // Set stage's title
// gameStage.setMinWidth(1000); // Won't be allowed to make width/height smaller
// gameStage.setMinHeight(500);
// gameStage.setMaxWidth(2000);
// gameStage.setMaxHeight(1000);
gameStage.setScene(scene); // Set scene to stage
BoardCONTROLLER bCont = loader.getController(); // Prepare board in BoardCONTROLLER
bCont.winLose.setVisible(false);
for (int i = 0; i < NewGameCONTROLLER.columns; i++) {
for (int j = 0; j < NewGameCONTROLLER.rows; j++) {
b = new Button(" ");
b.setMinSize(30, 30);
b.setMaxSize(30, 30);
b.setStyle("-fx-font-size:11");
bCont.TheBoard.add(b, i, j);
GridPane.setMargin(b, new Insets(5, 5, 5, 5));
GridPane.setHalignment(b, HPos.CENTER);
GridPane.setValignment(b, VPos.CENTER);
}
}
for (int i = 0; i < bCont.TheBoard.getChildren().size(); i++) {
((ButtonBase) bCont.TheBoard.getChildren().get(i)).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int x, y;
y = (int) ((Button) event.getSource()).getProperties().get("gridpane-column");
x = (int) ((Button) event.getSource()).getProperties().get("gridpane-row");
if (event.getButton().equals(MouseButton.PRIMARY))
NewGameCONTROLLER.game.open(x, y);
else if (event.getButton().equals(MouseButton.SECONDARY))
NewGameCONTROLLER.game.toggleFlag(x, y);
else if (event.getButton().equals(MouseButton.MIDDLE))
NewGameCONTROLLER.game.toggleQM(x, y);
for (Node child : bCont.TheBoard.getChildren()) {
int j = (int) ((Button) child).getProperties().get("gridpane-column");
int i = (int) ((Button) child).getProperties().get("gridpane-row");
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T') {
((Button) child).setText(NewGameCONTROLLER.game.board[i][j].substring(2, 3));
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'F' && NewGameCONTROLLER.game.board[i][j].charAt(0) == 'D')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GtnDxdQ/flag.png)");
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'F' && NewGameCONTROLLER.game.board[i][j].charAt(0) == '?')
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/fnw3Hr4/QM.jpg)");
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == 'E') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/txvBhCS/empty.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == 'M') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/6Jq9S3g/mine.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == 'B') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wgRt8v9/boom.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '1') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/5Gd2H0Y/1.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '2') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/GHGj0KM/2.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '3') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/sww9b1n/3.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '4') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/nrTjpqp/4.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '5') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/gZ6Rjrz/5.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '6') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/wh9XnfT/6.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '7') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/k1905rT/7.jpg)");
((Button) child).setText(" ");
}
if (NewGameCONTROLLER.game.board[i][j].charAt(1) == 'T' && NewGameCONTROLLER.game.board[i][j].charAt(2) == '8') {
((Button) child).setStyle(
"-fx-background-image: url(https://i.ibb.co/RCs2s2H/8.jpg)");
((Button) child).setText(" ");
}
if (Mines.winLose==1) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU WIN!");
}
if (Mines.winLose==0) {
bCont.winLose.setVisible(true);
bCont.winLose.setText("YOU LOSE! Try again...");
}
Mines.winLose=-1;
}
}
});
}
gameStage.show(); // Show stage }
}
}
board fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.paint.LinearGradient?>
<?import javafx.scene.paint.Stop?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MS.BoardCONTROLLER">
<children>
<GridPane fx:id="TheBoard" AnchorPane.bottomAnchor="90.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
</GridPane>
<HBox alignment="BOTTOM_LEFT" fillHeight="false" layoutX="14.0" layoutY="560.0" AnchorPane.bottomAnchor="20.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="700.0" AnchorPane.topAnchor="560.0">
<children>
<Button fx:id="BackMenu" mnemonicParsing="false" onAction="#BackMenu" text="Back to menu" />
<Button fx:id="ResetBoard" mnemonicParsing="false" onAction="#ResetBoard" text="Reset" textFill="RED">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
<Button fx:id="Music" mnemonicParsing="false" onAction="#Music" text="Music">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<Label fx:id="winLose" layoutX="796.0" layoutY="535.0" text="Label" AnchorPane.bottomAnchor="100.0" AnchorPane.rightAnchor="200.0">
<font>
<Font name="System Bold" size="35.0" />
</font>
<textFill>
<LinearGradient endX="1.0" endY="1.0">
<stops>
<Stop color="RED" />
<Stop color="#00e1ff" offset="1.0" />
</stops>
</LinearGradient>
</textFill>
</Label>
</children>
</AnchorPane>