-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.java
More file actions
181 lines (156 loc) · 6.34 KB
/
Generator.java
File metadata and controls
181 lines (156 loc) · 6.34 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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.TreeSet;
class Generator {
public Generator (MazePanel _mp) {
mp = _mp; a = mp.a;
n = mp.n; m = mp.m;
rnd = new Random();
rnd.setSeed(rnd.nextLong());
}
public void generatorStart(String s) {
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
a[i][j] = MAZE.WALL;
switch (s) {
case "Recursive Backtracker" -> recursiveBacktracker(new Coordinate(-1, -1));
case "75:25" -> growingTree(75);
case "50:50" -> growingTree(50);
case "25:75" -> growingTree(25);
case "Prim's algorithm" -> prims();
case "Kruskal's algorithm" -> kruskal();
}
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(a[i][j] != MAZE.WALL)
a[i][j] = MAZE.PASSAGE;
repaintDelay();
}
private void growingTree(int fr) {
ArrayList<Coordinate> cells = new ArrayList<>();
cells.add(getRandomPoint());
while (cells.size() != 0) {
int idx;
if (rnd.nextInt(100) < fr)
idx = cells.size() - 1;
else
idx = rnd.nextInt(cells.size());
Coordinate p1 = cells.get(idx);
ArrayList<Coordinate> pp = getAdjWalls(p1);
if (pp.size() != 0) {
Coordinate p2 = pp.get(0);
a[p1.i][p1.j] = a[(p1.i + p2.i) / 2][(p1.j + p2.j) / 2] = a[p2.i][p2.j] = MAZE.PASSAGE;
cells.add(p2);
}
else {
cells.remove(idx);
}
// needed to paint frontier in red
cells.removeIf(np -> (getAdjWalls(np).size() == 0));
cells.forEach(np -> a[np.i][np .j] = MAZE.FRONTIER);
repaintDelay();
cells.forEach(np -> a[np.i][np .j] = MAZE.PASSAGE);
}
}
private void prims() {
// create frontier, put a random point into it
ArrayList<Coordinate> frontier = new ArrayList<>();
frontier.add(getRandomPoint());
while(frontier.size() != 0) {
// pick a point
int idx = rnd.nextInt(frontier.size());
Coordinate p = frontier.get(idx);
frontier.remove(idx);
// mark it as visited and add edge
a[p.i][p.j] = MAZE.PASSAGE;
ArrayList<Coordinate> pos = new ArrayList<>();
int[][] dirs = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};
for(int[] dir : dirs) {
int ni = p.i + dir[0], nj = p.j + dir[1];
if (ni >= 0 && ni < n && nj >= 0 && nj < m && a[ni][nj] != MAZE.WALL)
pos.add(new Coordinate(ni, nj));
}
if (pos.size() != 0) {
Coordinate parent = pos.get(rnd.nextInt(pos.size()));
a[(parent.i + p.i) / 2][(parent.j + p.j) / 2] = MAZE.PASSAGE;
}
// add other points
frontier.addAll(getAdjWalls(p));
frontier.removeIf(np -> (a[np.i][np .j] != MAZE.WALL));
frontier.forEach(np -> a[np.i][np .j] = MAZE.FRONTIER);
repaintDelay();
frontier.forEach(np -> a[np.i][np .j] = MAZE.WALL);
}
}
private void recursiveBacktracker(Coordinate p) {
if (p.i == -1 || p.j == -1)
p = getRandomPoint();
repaintDelay();
ArrayList<Coordinate> walls = getAdjWalls(p);
if (walls.size() == 0) return;
Coordinate np = walls.get(rnd.nextInt(walls.size()));
a[p.i][p.j] = a[np.i][np.j] = a[(p.i + np.i) / 2][(p.j + np.j) / 2] = MAZE.PASSAGE;
recursiveBacktracker(np);
a[np.i][np.j] = a[(p.i + np.i) / 2][(p.j + np.j) / 2] = MAZE.GENERATED;
if (walls.size() > 1) recursiveBacktracker(p);
}
private void kruskal() {
int oldSleep = mp.sleepMS;
mp.sleepMS /= 2;
ArrayList<Pair<Coordinate, Coordinate>> edges = new ArrayList<>();
ArrayList<TreeSet<Coordinate>> trees = new ArrayList<>();
for(int i = 1; i < n; i += 2) {
for(int j = 1; j < m; j += 2) {
if (i + 2 < n) edges.add(new Pair<>(new Coordinate(i, j), new Coordinate(i + 2, j)));
if (j + 2 < m) edges.add(new Pair<>(new Coordinate(i, j), new Coordinate(i, j + 2)));
TreeSet<Coordinate> ts = new TreeSet<>();
ts.add(new Coordinate(i, j));
trees.add(ts);
}
}
Collections.shuffle(edges);
for(Pair<Coordinate, Coordinate> edge : edges) {
int idxTree = 0, idxTree1 = -1, idxTree2 = -1;
Coordinate p1 = edge.first, p2 = edge.second;
for(TreeSet<Coordinate> v : trees) {
if (v.contains(p1)) idxTree1 = idxTree;
if (v.contains(p2)) idxTree2 = idxTree;
if (idxTree1 != -1 && idxTree2 != -1)
break;
idxTree++;
}
if (idxTree1 != idxTree2) {
trees.get(idxTree1).addAll(trees.get(idxTree2));
trees.remove(idxTree2);
a[p1.i][p1.j] = a[p2.i][p2.j] =
a[(p1.i + p2.i) / 2][(p1.j + p2.j) / 2] = MAZE.PASSAGE;
repaintDelay();
}
}
mp.sleepMS = oldSleep;
}
private void repaintDelay() {
mp.repaintDelay();
}
private ArrayList<Coordinate> getAdjWalls(Coordinate p) {
ArrayList<Coordinate> v = new ArrayList<>();
int[][] dirs = {{0, 2}, {2, 0}, {0, -2}, {-2, 0}};
for(int[] dir : dirs) {
int ni = p.i + dir[0], nj = p.j + dir[1];
if (ni >= 0 && ni < n && nj >= 0 && nj < m && a[ni][nj] == MAZE.WALL)
v.add(new Coordinate(ni, nj));
}
Collections.shuffle(v);
return v;
}
private Coordinate getRandomPoint() {
int i = rnd.nextInt(n / 2 - 1) * 2 + 3;
int j = rnd.nextInt(m / 2 - 1) * 2 + 3;
return new Coordinate(i, j);
}
private final MazePanel mp;
private final Random rnd;
private final int n, m;
private final int[][] a;
}