-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
219 lines (208 loc) · 4.83 KB
/
Map.java
File metadata and controls
219 lines (208 loc) · 4.83 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
import java.util.Random;
public class Map
{
private Room[][] _rooms;
public Map(int maxWidth, int maxHeight)
{
_rooms = new Room[maxWidth][maxHeight];
generateExits(0, 0, "");
checkConnections();
}
/*
* Returns a room at the given coordinates. It will return
* null if a room does not exist at those coordinates.
* It will throw an out of bounds exception if the coordinates
* are outside of the map.
*/
public Room getRoom(int x, int y)
{
if (x < 0 || y < 0 || x >= _rooms.length || y >= _rooms[x].length)
{
throw new IndexOutOfBoundsException();
}
return _rooms[x][y];
}
/*
* Recursively generates a map in a tree-fashion.
* A room will always have an exit in the direction determined by 'entranceDirection'.
* It then goes through the direction left, right, up, and down and randomly decides
* if it should create an exit in that direction. If a room already exists in that direction
* it simply sets the exits and moves on. If a room doesn't exist it recursively runs this method
* with the entranceDirection being the direction it is coming from relative to the new room.
*/
private void generateExits(int x, int y, String entranceDirection)
{
Random randomGenerator = new Random();
boolean generateUp = false;
boolean generateRight = false;
boolean generateDown = false;
boolean generateLeft = false;
if (entranceDirection.equals("up"))
{
generateUp = true;
}
else if (entranceDirection.equals("right"))
{
generateRight = true;
}
else if (entranceDirection.equals("down"))
{
generateDown = true;
}
else if (entranceDirection.equals("left"))
{
generateLeft = true;
}
_rooms[x][y] = new Room(generateUp, generateRight, generateDown, generateLeft);
if (x > 0 && !generateLeft)
{
_rooms[x][y].setLeftExit(randomGenerator.nextBoolean());
if (_rooms[x][y].getLeftExit())
{
if (this.getRoom(x-1, y) == null)
{
generateExits(x-1, y, "right");
}
else
{
this.getRoom(x-1, y).setRightExit(true);
}
}
}
if (x < _rooms.length-1 && !generateRight)
{
_rooms[x][y].setRightExit(randomGenerator.nextBoolean());
if (_rooms[x][y].getRightExit())
{
if (this.getRoom(x+1, y) == null)
{
generateExits(x+1, y, "left");
}
else
{
this.getRoom(x+1, y).setLeftExit(true);
}
}
}
if (y > 0 && !generateUp)
{
_rooms[x][y].setUpExit(randomGenerator.nextBoolean());
if (_rooms[x][y].getUpExit())
{
if (_rooms[x][y-1] == null)
{
generateExits(x, y-1, "down");
}
else
{
_rooms[x][y-1].setDownExit(true);
}
}
}
if (y < _rooms[x].length-1 && !generateDown)
{
_rooms[x][y].setDownExit(randomGenerator.nextBoolean());
if (_rooms[x][y].getDownExit())
{
if (_rooms[x][y+1] == null)
{
generateExits(x, y+1, "up");
}
else
{
_rooms[x][y+1].setUpExit(true);
}
}
}
}
/*
* Goes through every room in the map and makes sure the up/down
* exits are correct. It is currently a work-around for a bug
* in the map generation code.
*/
private void checkConnections()
{
for (int x = 0; x < _rooms.length; x++)
{
for (int y = 0; y < _rooms[0].length; y++)
{
if (_rooms[x][y] != null)
{
if (_rooms[x][y].getUpExit())
{
_rooms[x][y-1].setDownExit(true);
}
if (_rooms[x][y].getDownExit())
{
_rooms[x][y+1].setUpExit(true);
}
}
}
}
}
/*
* This method prints a graphical representation of the map in the console.
* It is purely for testing purposes.
* Asterisks represent rooms. Lines represent hallways.
*/
public void displayMap()
{
String topLine = "";
String midLine = "";
String bottomLine = "";
for (int y = 0; y < _rooms[0].length; y++)
{
for (int x = 0; x < _rooms.length; x++)
{
if (this.getRoom(x, y) != null)
{
if (this.getRoom(x, y).getUpExit())
{
topLine += " | ";
}
else
{
topLine += " ";
}
if (this.getRoom(x, y).getLeftExit())
{
midLine += "-";
}
else
{
midLine += " ";
}
midLine += "*";
if (this.getRoom(x, y).getRightExit())
{
midLine += "-";
}
else
{
midLine += " ";
}
if (this.getRoom(x, y).getDownExit())
{
bottomLine += " | ";
}
else
{
bottomLine += " ";
}
}
else
{
topLine += " ";
midLine += " ";
bottomLine += " ";
}
}
System.out.println(topLine);
topLine = "";
System.out.println(midLine);
midLine = "";
System.out.println(bottomLine);
bottomLine = "";
}
}
}