/
CircleMazeFactory.java
134 lines (105 loc) · 3.16 KB
/
CircleMazeFactory.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
package maize;
/** Creates an maze made of concentric cirlces */
public class CircleMazeFactory implements MazeFactory {
/** Maze width */
private int width = 0;
/** Maze height */
private int height = 0;
/** Maze data */
private boolean[][] data = null;
/** Default Constructor */
public CircleMazeFactory() { }
/** Public method for getting the maze.
*
* @param width Width of the maze.
* @param height Width of the maze.
*
* @return Maze object.
*/
public Maze getMaze(int width, int height){
/* Check for sane values, otherwise use defaults. */
if(width < 7 || height < 7){
this.width = 7;
this.height = 7;
}
if ( (width >> 1 << 1) == width ){
this.width = width+1;
}
if ( (height >> 1 << 1) == height ){
this.height = height+1;
}
this.width = (this.width == 0)? width : this.width;
this.height = (this.height == 0)? height : this.height;
this.createBlankCircles();
this.createCircleExits();
return new Maze(this.data, this.width, this.height, 1, 1, width/2, height/2);
}
/** Creates a blank maze with concentric circles, no joins.
*/
public void createBlankCircles(){
this.data = new boolean[this.width][this.height];
int noCircles = (int)Math.floor(Math.min(this.data.length, this.data[0].length)/2);
/* Create the circles */
for(int i=0;i<noCircles;i++){
for(int w=0; w<=1; w++){ /* Wall or room layer */
for(int x=0+(i*2)+w; x<this.data.length-((i*2)+w); x++){
for(int y=0+(i*2)+w; y<this.data[0].length-((i*2)+w); y++){
this.data[x][y] = (w == 0) ? true : false;
}
}
}
}
}
/** Creates exits between the circles.
*/
public void createCircleExits(){
int noCircles = (int)Math.ceil(Math.min(this.data.length, this.data[0].length)/4);
/* Create the circles */
for(int i=1;i<=noCircles;i++){
/* Create a circumference and choose from that */
int w = this.data.length - (i*4) - 1;
int h = this.data[0].length- (i*4) - 1;
int c = (w*2 + h*2);
/* Choose position based upon a random number in the circumference. */
int num = (int)(Math.random() * c);
/* The most intriciate bit of evil ever.
* Translate from circumference number to x,y
*/
int x = 0;
if(num < w || (num < (w*2 +h)&& num >= (w+h)) ){
if(num < w){
x = num;
} else {
x = num - (w + h);
}
} else {
if(num < (w+h)){
x = w;
} else {
x = 0;
}
}
int y = 0;
if(num < w || (num < (w*2 +h)&& num >= (w+h)) ){
if(num < w){
y = 0;
} else {
y = h;
}
} else {
if(num < (w+h)){
y = num - w;
} else {
y = num - (2*w + h);
}
}
/* If (x,y) is a corner fix it */
if(x == 0 && y ==0) {x++;}
if(x == 0 && y ==h) {x++;}
if(x == w && y ==0) {x--;}
if(x == w && y ==h) {x--;}
this.data[x + ((i*2))][y + ((i*2))] = false;
}
this.data[this.width/2][this.height/2] = false;
}
}