I have created a maze game in JavaFX where a user can create their own maze and play it. The maze is built using buttons with CSS IDs depending on the 2-dimensional array the level is temporarily stored in.
The problem arises with the next part of the project. I have created an algorithm which generates a random maze. In order for the level to be possible, I need to check if the maze is solvable (i.e. you can get from the start (0, 3) to the finish (6, 3)).
I have created a separate project with the same display algorithm, the classes are below:
Main.java
import javafx.application.Application;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
public class Main extends Application{
int[][] level = {{1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1}};
public static boolean[][] status = new boolean[7][7];
public static Button[][] blankButtons = new Button[7][7];
public static Runner[][] runners = new Runner[7][7];
boolean solvable = false;
GridPane buttonGrid = new GridPane();
public static void main(String[] args){launch(args);}
public void start(Stage primaryStage) throws Exception {
Stage window = primaryStage;
for (int i = 0; i < 7; i++){
for (int j = 0; j < 7; j++){
if (level[i][j] == 1){
status[i][j] = false;
} else if (level[i][j] == 0){
status[i][j] = true;
}
}
}
GridPane mazeGrid = new GridPane();
Scene maze = new Scene(mazeGrid, 700, 700);
maze.getStylesheets().add("Main.css");
for(int i = 0; i < 7; i++){
for(int j = 0; j < 7; j++){
makeBlankButton(i, j);
}
}
//MY PREVIOUS ATTEMPT AT A FLOOD SOLVER//
// for(int i = 0; i < 7; i++){
// for(int j = 0; j < 7; j++){
// runners[i][j] = new Runner(i, j);
// runners[i][j].alive = false;
// }
// }
// Runner finish = new Runner(3, 6);
// finish.alive = false;
//
//
// runners[3][0].alive = true;
// runners[3][0].run();
// for(int i = 0; i < 7; i++){
// for(int j = 0; j < 7; j++){
// if(runners[i][j].alive){
// runners[i][j].run();
// }
// if(runners[3][1].alive){
// solvable = true;
// System.out.println(solvable);
//
// }
// System.out.println(solvable);
// }
// }
mazeGrid.getChildren().add(buttonGrid);
window.setScene(maze);
window.show();
}
public void makeBlankButton(int row, int column){
blankButtons[row][column] = new Button();
GridPane.setConstraints(blankButtons[row][column], column, row);
if (level[row][column] == 1){
blankButtons[row][column].setId("button-array-clicked");
} else if (level[row][column] == 0) {
blankButtons[row][column].setId("button-array-blank");
}
buttonGrid.getChildren().add(blankButtons[row][column]);
GridPane.setConstraints(buttonGrid, 0, 1);
if (row == 3){
if (column == 0){
blankButtons[row][column].setId("button-start");
} else if(column == 6){
blankButtons[row][column].setId("button-end");
}
}
}
}
Runner.java
public class Runner {
int x, y;
boolean alive;
Runner(int x, int y){
this.x = x;
this.y = y;
this.alive = false;
if(this.alive) {
Main.blankButtons[x][y].setId("button-water");
}
}
public void run(){
if (this.alive) {
if (Main.status[x + 1][y]) {
// Main.runners[x + 1][y] = new Runner(x + 1, y);
Main.runners[y + 1][x].alive = true;
Main.status[x][y] = false;
this.alive = false;
} else if (Main.status[y][x + 1]) {
// Main.runners[x][y + 1] = new Runner(x, y + 1);
Main.runners[x][y + 1].alive = true;
Main.status[x][y] = false;
this.alive = false;
} else if (Main.status[y][x-1]) {
// Main.runners[x - 1][y] = new Runner(x - 1, y);
Main.runners[x - 1][y].alive = true;
Main.status[x][y] = false;
this.alive = false;
} else if (Main.status[y][x-1]) {
// Main.runners[x][y - 1] = new Runner(x, y - 1);
Main.runners[x][y - 1].alive = true;
Main.status[x][y] = false;
this.alive = false;
}
}
}
}
Main.css
#button-array-clicked{
-fx-pref-width: 100px;
-fx-pref-height: 100px;
-fx-border-width: 1px;
-fx-border-color: #00aa00;
-fx-background-color: #000000;
-fx-font-size: 10px;
}
#button-array-blank{
-fx-pref-width: 100px;
-fx-pref-height: 100px;
-fx-border-width: 1px;
-fx-border-color: #00aa00;
-fx-background-color: #ffffff;
-fx-font-size: 10px;
}
#button-start{
-fx-pref-width: 100px;
-fx-pref-height: 100px;
-fx-border-width: 1px;
-fx-border-color: #00aa00;
-fx-background-color: #00bbff;
-fx-font-size: 10px;
}
#button-end{
-fx-pref-width: 100px;
-fx-pref-height: 100px;
-fx-border-width: 1px;
-fx-border-color: #00aa00;
-fx-background-color: #cc00aa;
-fx-font-size: 10px;
}
#button-water{
-fx-pref-width: 100px;
-fx-pref-height: 100px;
-fx-border-width: 1px;
-fx-border-color: #00aa00;
-fx-background-color: #0000ff;
-fx-font-size: 10px;
}
How could I go around solving the maze, while showing visibly? If the maze is solvable, I would like it to display the maze once it is complete, but I would like the program to generate a new maze to check of it is unsolvable.
Thank you
See Question&Answers more detail:
os