-
Notifications
You must be signed in to change notification settings - Fork 173
/
AlgoVisualizer.java
78 lines (57 loc) · 2.03 KB
/
AlgoVisualizer.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
import javafx.geometry.Pos;
import java.util.LinkedList;
import java.util.Stack;
import java.awt.*;
public class AlgoVisualizer {
private static int DELAY = 20;
private static int blockSide = 8;
private MazeData data;
private AlgoFrame frame;
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
public AlgoVisualizer(int N, int M){
// 初始化数据
data = new MazeData(N, M);
int sceneHeight = data.N() * blockSide;
int sceneWidth = data.M() * blockSide;
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Random Maze Generation Visualization", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
private void run(){
setData(-1, -1);
RandomQueue<Position> queue = new RandomQueue<Position>();
Position first = new Position(data.getEntranceX(), data.getEntranceY()+1);
queue.add(first);
data.visited[first.getX()][first.getY()] = true;
while(queue.size() != 0){
Position curPos = queue.remove();
for(int i = 0 ; i < 4 ; i ++){
int newX = curPos.getX() + d[i][0]*2;
int newY = curPos.getY() + d[i][1]*2;
if(data.inArea(newX, newY)
&& !data.visited[newX][newY]
&& data.maze[newX][newY] == MazeData.ROAD){
queue.add(new Position(newX, newY));
data.visited[newX][newY] = true;
setData(curPos.getX() + d[i][0], curPos.getY() + d[i][1]);
}
}
}
setData(-1, -1);
}
private void setData(int x, int y){
if(data.inArea(x, y))
data.maze[x][y] = MazeData.ROAD;
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
public static void main(String[] args) {
int N = 101;
int M = 101;
AlgoVisualizer vis = new AlgoVisualizer(N, M);
}
}