-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.java
185 lines (171 loc) · 6.25 KB
/
Controller.java
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package projMAZE;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML private Pane pane, mazeGUI;
@FXML private Label status, clock, explore;
@FXML private Button genMaze, solveMaze, restart;
@FXML private CheckBox fails;
@FXML private TextField width, height, box;
private Maze maze = new Maze();
private static volatile boolean solving = false;
@Override public void initialize(URL location, ResourceBundle resources) {
maze.generateMaze();
update();
explore.setText("EXPLORED 0.00%");
width.setText(Maze.WIDTH+"");
height.setText(Maze.HEIGHT+"");
box.setText(Maze.BOXSIZE+"");
genMaze.setOnAction(event -> {
if (!solving) {
int newWidth = Maze.WIDTH;
int newHeight = Maze.HEIGHT;
int newBoxsize = Maze.BOXSIZE;
try {
newWidth = Integer.parseInt(width.getText());
if (!inRange(newWidth, 5, 300))
return;
} catch (NumberFormatException e) {
System.out.println("width wrong");
}
try {
newHeight = Integer.parseInt(height.getText());
if (!inRange(newHeight, 5, 300))
return;
} catch (NumberFormatException e) {
System.out.println("height wrong");
}
try {
newBoxsize = Integer.parseInt(box.getText());
if (!inRange(newBoxsize, 3, 100))
return;
} catch (NumberFormatException e) {
System.out.println("box size");
}
if (Maze.WIDTH != newWidth || Maze.HEIGHT != newHeight || Maze.BOXSIZE != newBoxsize) {
Maze.WIDTH = newWidth;
Maze.HEIGHT = newHeight;
Maze.BOXSIZE = newBoxsize;
maze = new Maze();
}
runGenerator();
}
});
solveMaze.setOnAction(event -> {
if(!solving)
runSolver();
});
restart.setOnAction(event -> {
if (!solving) {
status.setText("STATUS: NOT STARTED");
clock.setText("SOLVE TIME ---");
explore.setText("EXPLORED 0.00%");
maze.clearVisits();
update();
}
});
private void runGenerator() {
status.setText("STATUS: GENERATING");
clock.setText("SOLVE TIME ---");
solving = true;
Thread t = new Thread(() -> {
Runnable run = () -> {
maze.generateMaze();
update();
explore.setText("EXPLORED 0.00%");
status.setText("STATUS: NOT STARTED");
solving = false;
};
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(run);
});
t.setDaemon(true);
t.start();
}
private void runSolver() {
status.setText("STATUS: IN PROGRESS");
clock.setText("SOLVE TIME ---");
solving = true;
long startTime = System.currentTimeMillis() + 500;
Thread t = new Thread(() -> {
Runnable run = () -> {
maze.solveMaze();
update();
explore.setText(String.format("EXPLORED %.02f%%",maze.percentExplored()));
status.setText("STATUS: SOLVED");
long elapsedTime = System.currentTimeMillis() - startTime;
clock.setText("SOLVE TIME "+ elapsedTime/1000. + "s");
solving = false;
};
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Platform.runLater(run);
});
t.setDaemon(true);
t.start();
}
boolean inRange(int num, int min, int max) {
return num >= min && num <= max;
}
void drawLine(int x1, int y1, int x2, int y2) {
Line line = new Line(x1*Maze.BOXSIZE,y1*Maze.BOXSIZE,x2*Maze.BOXSIZE,y2*Maze.BOXSIZE);
line.setStrokeWidth(Maze.BOXSIZE/9.);
mazeGUI.getChildren().add(line);
}
Label box(int x, int y, String color) {
Label label = new Label();
label.setMinSize(Maze.BOXSIZE, Maze.BOXSIZE);
label.setPrefSize(Maze.BOXSIZE, Maze.BOXSIZE);
label.setMaxSize(Maze.BOXSIZE, Maze.BOXSIZE);
label.setLayoutX(x*Maze.BOXSIZE);
label.setLayoutY(y*Maze.BOXSIZE);
label.setStyle("-fx-background-color: "+color);
return label;
}
Label path(int x, int y) {
return box(x,y,"#32ff7e");
}
Label tried(int x, int y) {
return box(x,y,"#ff4d4d");
}
void update() {
mazeGUI.setTranslateX(7);
mazeGUI.setTranslateY(7);
mazeGUI.setPrefSize(Maze.WIDTH*Maze.BOXSIZE + 10,Maze.HEIGHT*Maze.BOXSIZE + 10);
mazeGUI.getChildren().clear();
Box[][] array = maze.getMaze();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
Box b = array[i][j];
if (b.isOnPath())
mazeGUI.getChildren().add(path(i,j));
else if (fails.isSelected() && b.isVisited())
mazeGUI.getChildren().add(tried(i,j));
if (b.isUp())
drawLine(i,j,i+1,j);
if (b.isLeft())
drawLine(i,j,i,j+1);
if (i == array.length - 1 && b.isRight())
drawLine(i+1,j,i+1,j+1);
if (j == array[0].length - 1 && b.isDown())
drawLine(i,j+1,i+1,j+1);
}
}
}
}