-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabyrinthGenerator.java
More file actions
104 lines (90 loc) · 4.18 KB
/
Copy pathLabyrinthGenerator.java
File metadata and controls
104 lines (90 loc) · 4.18 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
import java.awt.image.BufferedImage;
public class LabyrinthGenerator {
private Shape[][] labyrinth;
private BufferedImage buffer;
// This class is in charge of the unique labyrinth's generation depending on the shape in use
public LabyrinthGenerator(int dimensionX, int dimensionY, int shapeDimension, Type shapeType, BufferedImage buffer) {
this.labyrinth = new Shape[dimensionY][dimensionX];
this.buffer = buffer;
switch (shapeType) {
case SQUARE -> generateSquareLabyrinth(shapeDimension);
case HEXAGON -> generateHexagonalLabyrinth(shapeDimension);
case TRIANGLE -> generateTriangularLabyrinth(shapeDimension);
}
}
// ====================== Shape based Methods ====================== //
public void generateSquareLabyrinth(int shapeDimension) {
for (int i = 0; i < labyrinth.length; i++) {
for (int j = 0; j < labyrinth[i].length; j++) {
labyrinth[i][j] = new Square(shapeDimension, i, j);
Shape currentShape = labyrinth[i][j];
// Connect to left neighbor
if (j > 0) {
Shape leftNeighbor = labyrinth[i][j - 1];
currentShape.addNeighbor(leftNeighbor, 0);
leftNeighbor.addNeighbor(currentShape, 2);
}
// Connect to top neighbor
if (i > 0) {
Shape topNeighbor = labyrinth[i - 1][j];
currentShape.addNeighbor(topNeighbor, 1);
topNeighbor.addNeighbor(currentShape, 3);
}
currentShape.draw(buffer.getGraphics());
}
}
}
public void generateHexagonalLabyrinth(int shapeDimension) {
for (int i = 0; i < labyrinth.length; i++) {
for (int j = 0; j < labyrinth[i].length; j++) {
labyrinth[i][j] = new Hexagon(shapeDimension, i, j);
Shape currentShape = labyrinth[i][j];
// Connect to left neighbor
if (j > 0) {
Shape leftNeighbor = labyrinth[i][j - 1];
currentShape.addNeighbor(leftNeighbor, 0);
leftNeighbor.addNeighbor(currentShape, 3);
}
// Connect to top-centre neighbor
int side = (i % 2 == 0) ? -1 : 1;
if (i > 0) {
Shape topCenterNeighbor = labyrinth[i - 1][j];
currentShape.addNeighbor(topCenterNeighbor, 2 - i % 2);
topCenterNeighbor.addNeighbor(currentShape, 5 - i % 2);
}
// Connect to top-side neighbor
if (i > 0 && j + side >= 0 && j + side < labyrinth[i].length) {
Shape otherTopNeighbor = labyrinth[i - 1][j + side];
currentShape.addNeighbor(otherTopNeighbor, 1 + i % 2);
otherTopNeighbor.addNeighbor(currentShape, 4 + i % 2);
}
currentShape.draw(buffer.getGraphics());
}
}
}
public void generateTriangularLabyrinth(int shapeDimension) {
for (int i = 0; i < labyrinth.length; i++) {
for (int j = 0; j < labyrinth[i].length; j++) {
labyrinth[i][j] = new Triangle(shapeDimension, i, j);
Shape currentShape = labyrinth[i][j];
// Connect to left neighbor
if (j > 0) {
Shape leftNeighbor = labyrinth[i][j - 1];
currentShape.addNeighbor(leftNeighbor, 0);
leftNeighbor.addNeighbor(currentShape, 1 + (i + j) % 2);
}
// Connect to top neighbor
if (i > 0 && (i + j) % 2 == 0) {
Shape topNeighbor = labyrinth[i - 1][j];
currentShape.addNeighbor(topNeighbor, 1);
topNeighbor.addNeighbor(currentShape, 2);
}
currentShape.draw(buffer.getGraphics());
}
}
}
// generic method to retrieve the labyrinth
public Shape[][] getLabyrinth() {
return this.labyrinth;
}
}