-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.java
More file actions
288 lines (267 loc) · 8.05 KB
/
Maze.java
File metadata and controls
288 lines (267 loc) · 8.05 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package maze;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
/**
* <P>This class represents a randomly generated Maze. </P>
*
* <P>The Maze should be imagined as a rectangular grid
* of "Junctures" (intersections). There is a wall surrounding
* the entire grid. Adjacent junctures
* may or may not have a "wall" between them.</P>
*
* <P>There is also a weight (positive integer) between
* any two junctures. This weight could be viewed as the
* "cost" of traveling from a juncture to an adjacent
* juncture.</P>
*
*/
public class Maze {
private static Random random = new Random();
private int width, height; // wall dimensions, not junctures
private int[][] wallIndex;
private boolean[][] wallToRight;
private boolean[][] wallDown;
private int [][] horizontalWeights;
private int [][] verticalWeights;
/**
* Construct random maze.
*
* @param mazeHeight
* @param mazeWidth
* @param sparcity value from 0 to 100. If set to 100, there is exactly one path
* from any juncture to any other juncture. Lower values have fewer walls, hence
* more paths between junctures.
*/
public Maze(int mazeHeight, int mazeWidth, int sparcity) {
this(mazeHeight, mazeWidth, sparcity, random.nextLong());
}
/**
* This constructor also allows a seed for the random number generator.
* (If you pass in the same seed, you get the same maze every time.)
*
* @param mazeHeight
* @param mazeWidth
* @param sparcity
* @param randomSeed
*/
public Maze(int mazeHeight, int mazeWidth, int sparcity, long randomSeed) {
random = new Random(randomSeed);
width = mazeWidth + 1;
height = mazeHeight + 1;
int currWall = 1;
horizontalWeights = new int[height - 2][width - 1];
verticalWeights = new int[height - 1][width - 2];
for (int i = 0; i < height - 2; i++) {
for (int j = 0; j < width - 1; j++) {
horizontalWeights[i][j] = random.nextInt(9) + 1;
}
}
for (int i = 0; i < height - 1; i++) {
for (int j = 0; j < width - 2; j++) {
verticalWeights[i][j] = random.nextInt(9) + 1;
}
}
wallIndex = new int[height][width];
for (int i = 0; i < width; i++) {
wallIndex[0][i] = currWall;
wallIndex[height - 1][i] = currWall;
}
for (int i = 0; i < height; i++) {
wallIndex[i][0] = currWall;
wallIndex[i][width - 1] = currWall;
}
wallToRight = new boolean[height][width];
wallDown = new boolean[height][width];
for (int i = 0; i < width - 1; i++) {
wallToRight[0][i] = true;
wallToRight[height - 1][i] = true;
}
for (int i = 0; i < height - 1; i++) {
wallDown[i][0] = true;
wallDown[i][width - 1] = true;
}
/* Draw the interior walls */
ArrayList<Juncture> points = new ArrayList<>();
for (int x = 1; x < width; x++) {
for (int y = 1; y < height; y++) {
points.add(new Juncture(x, y));
}
}
Collections.shuffle(points);
while (points.size() > 0) {
Juncture p = points.remove(0);
int row = p.getY();
int col = p.getX();
if (wallIndex[row][col] == 0) {
currWall++;
wallIndex[row][col] = currWall;
drawFrom(row, col, currWall);
}
}
/* Remove some walls if sparcity is > 0 */
if (sparcity > 0) {
for (int i = 0; i < width * height * sparcity/50.0; i++) {
int r = random.nextInt(height - 2) + 1;
int c = random.nextInt(width - 2) + 1;
if (random.nextBoolean() == false) {
wallDown[r][c] = false;
} else {
wallToRight[r][c] = false;
}
}
}
}
private void drawFrom(int row, int col, int currWall) {
if (possibleToExtend(row, col, currWall)) {
while(true) {
int directionToTry = random.nextInt(4);
if (directionToTry == 0) { // to right
int wallFound = wallIndex[row][col + 1];
if (wallFound != currWall) {
wallIndex[row][col + 1] = currWall;
wallToRight[row][col] = true;
if (wallFound == 0) {
drawFrom(row, col + 1, currWall);
}
return;
}
} else if (directionToTry == 1) { // to left
int wallFound = wallIndex[row][col - 1];
if (wallFound != currWall) {
wallIndex[row][col - 1] = currWall;
wallToRight[row][col - 1] = true;
if (wallFound == 0) {
drawFrom(row, col - 1, currWall);
}
return;
}
} else if (directionToTry == 2) { // up
int wallFound = wallIndex[row - 1][col];
if (wallFound != currWall) {
wallIndex[row - 1][col] = currWall;
wallDown[row - 1][col] = true;
if (wallFound == 0) {
drawFrom(row - 1, col, currWall);
}
return;
}
} else { //down
int wallFound = wallIndex[row + 1][col];
if (wallFound != currWall) {
wallIndex[row + 1][col] = currWall;
wallDown[row][col] = true;
if (wallFound == 0) {
drawFrom(row + 1, col, currWall);
}
return;
}
}
}
} else { // no current extension possible
while(true) {
int r = random.nextInt(height);
int c = random.nextInt(width);
if (wallIndex[r][c] == currWall && possibleToExtend(r, c, currWall)) {
drawFrom(r, c, currWall);
return;
}
}
}
}
private boolean possibleToExtend(int row, int col, int currWall) {
if (wallIndex[row][col + 1] != currWall) {
return true;
}
if (wallIndex[row][col - 1] != currWall) {
return true;
}
if (wallIndex[row + 1][col] != currWall) {
return true;
}
if (wallIndex[row - 1][col] != currWall) {
return true;
}
return false;
}
/** Returns the width of this maze. (This is the width
* of the grid of junctures.)
* @return width of maze
*/
public int getMazeWidth() {
return width - 1;
}
/** Returns the height of this maze. (This is the height
* of the grid of junctures.)
* @return height of maze
*/
public int getMazeHeight() {
return height - 1;
}
/** Returns true if there is a wall above the given juncture,
* false otherwise.
* @param juncture
* @return true if there is a wall above this juncture
*/
public boolean isWallAbove(Juncture juncture) {
return wallToRight[juncture.getY()][juncture.getX()];
}
/** Returns true if there is a wall below the given juncture,
* false otherwise.
* @param juncture
* @return true if there is a wall below this juncture
*/
public boolean isWallBelow(Juncture juncture) {
return wallToRight[juncture.getY()+ 1][juncture.getX()];
}
/** Returns true if there is a wall to the left of the
* given juncture, false otherwise.
* @param juncture
* @return true if there is a wall to the left of this juncture
*/
public boolean isWallToLeft(Juncture juncture) {
return wallDown[juncture.getY()][juncture.getX()];
}
/** Returns true if there is a wall to the right of the
* given juncture, false otherwise.
* @param juncture
* @return true if there is a wall to the right of this juncture
*/
public boolean isWallToRight(Juncture juncture) {
return wallDown[juncture.getY()][juncture.getX() + 1];
}
/** Returns the weight between this juncture and the one above.
*
* @param juncture
* @return a positive integer
*/
public int getWeightAbove(Juncture juncture) {
return horizontalWeights[juncture.getY() - 1][juncture.getX()];
}
/** Returns the weight between this juncture and the one below.
*
* @param juncture
* @return a positive integer
*/
public int getWeightBelow(Juncture juncture) {
return horizontalWeights[juncture.getY()][juncture.getX()];
}
/** Returns the weight between this juncture and the one to its
* left.
*
* @param juncture
* @return a positive integer
*/
public int getWeightToLeft(Juncture juncture) {
return verticalWeights[juncture.getY()][juncture.getX() - 1];
}
/** Returns the weight between this juncture and the one to its
* right.
*
* @param juncture
* @return a positive integer
*/
public int getWeightToRight(Juncture juncture) {
return verticalWeights[juncture.getY()][juncture.getX()];
}
}