-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeSolver.java
More file actions
141 lines (116 loc) · 4.41 KB
/
Copy pathMazeSolver.java
File metadata and controls
141 lines (116 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.*;
public class MazeSolver extends Application {
private static final int CELL_SIZE = 60;
private static final int[][] MAZE = {
{1, 0, 1, 1, 1},
{1, 1, 1, 0, 1},
{0, 0, 1, 0, 1},
{1, 1, 1, 1, 1},
{1, 0, 0, 1, 1}
};
private static final int START_X = 0, START_Y = 0;
private static final int END_X = 4, END_Y = 4;
private Canvas canvas;
private GraphicsContext gc;
static class Cell {
int x, y;
Cell parent;
Cell(int x, int y, Cell parent) {
this.x = x;
this.y = y;
this.parent = parent;
}
}
private static final int[] rowDir = {-1, 1, 0, 0};
private static final int[] colDir = {0, 0, -1, 1};
private static boolean isValid(int[][] maze, boolean[][] visited, int x, int y) {
int n = maze.length;
int m = maze[0].length;
return (x >= 0 && y >= 0 && x < n && y < m && maze[x][y] == 1 && !visited[x][y]);
}
private List<Cell> bfsSolve() {
int n = MAZE.length;
boolean[][] visited = new boolean[n][n];
Queue<Cell> queue = new LinkedList<>();
queue.add(new Cell(START_X, START_Y, null));
visited[START_X][START_Y] = true;
Cell endCell = null;
while (!queue.isEmpty()) {
Cell current = queue.poll();
if (current.x == END_X && current.y == END_Y) {
endCell = current;
break;
}
for (int i = 0; i < 4; i++) {
int newX = current.x + rowDir[i];
int newY = current.y + colDir[i];
if (isValid(MAZE, visited, newX, newY)) {
visited[newX][newY] = true;
queue.add(new Cell(newX, newY, current));
}
}
}
List<Cell> path = new ArrayList<>();
while (endCell != null) {
path.add(endCell);
endCell = endCell.parent;
}
Collections.reverse(path);
return path;
}
private void drawMaze(List<Cell> path) {
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
for (int i = 0; i < MAZE.length; i++) {
for (int j = 0; j < MAZE[i].length; j++) {
if (MAZE[i][j] == 0)
gc.setFill(Color.DARKGRAY);
else
gc.setFill(Color.WHITE);
gc.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
gc.setStroke(Color.BLACK);
gc.strokeRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
gc.setFill(Color.LIGHTBLUE);
gc.fillRect(START_Y * CELL_SIZE, START_X * CELL_SIZE, CELL_SIZE, CELL_SIZE);
gc.setFill(Color.LIGHTGREEN);
gc.fillRect(END_Y * CELL_SIZE, END_X * CELL_SIZE, CELL_SIZE, CELL_SIZE);
gc.setFill(Color.GOLD);
for (Cell c : path) {
gc.fillRect(c.y * CELL_SIZE + 10, c.x * CELL_SIZE + 10, CELL_SIZE - 20, CELL_SIZE - 20);
}
}
@Override
public void start(Stage primaryStage) {
canvas = new Canvas(MAZE[0].length * CELL_SIZE, MAZE.length * CELL_SIZE);
gc = canvas.getGraphicsContext2D();
Button solveBtn = new Button("Solve Maze");
solveBtn.setOnAction(e -> {
List<Cell> path = bfsSolve();
drawMaze(path);
});
Button resetBtn = new Button("Reset");
resetBtn.setOnAction(e -> drawMaze(Collections.emptyList()));
HBox buttons = new HBox(10, solveBtn, resetBtn);
BorderPane root = new BorderPane();
root.setCenter(canvas);
root.setBottom(buttons);
drawMaze(Collections.emptyList());
Scene scene = new Scene(root);
primaryStage.setTitle("Maze Solver - BFS Visualization");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}