-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
379 lines (343 loc) · 15.2 KB
/
Map.java
File metadata and controls
379 lines (343 loc) · 15.2 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package APCSA.APCSA_Code_Your_Own;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
public class Map implements Serializable {
private Room[][] mapRooms;
private static final String fileName = "MapState.ser";
public Map(int mapSize) {
mapRooms = generateNewMap(mapSize);
mapRooms = addNpcs(mapRooms);
}
public Room[][] generateNewMap(int mapSize) {
Room[][] rooms = new Room[mapSize][mapSize];
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
HashMap<String, Door> tempDoors = new HashMap<String, Door>();
if ((Utils.randInt(0, 3) != 0)) {
tempDoors.put("north", new Door("north"));
}
if ((Utils.randInt(0, 3) != 0)) {
tempDoors.put("east", new Door("east"));
}
if ((Utils.randInt(0, 3) != 0)) {
tempDoors.put("south", new Door("south"));
}
if ((Utils.randInt(0, 3) != 0)) {
tempDoors.put("west", new Door("west"));
}
rooms[i][j] = new Room(i, j, tempDoors);
}
}
if (!checkContinuity(rooms)) {
rooms = generateNewMap(mapSize);
}
return rooms;
}
public boolean checkContinuity(Room[][] map) {
map = repairMapLinks(map);
ArrayList<Room> roomList = new ArrayList<Room>();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
roomList.add(map[i][j]);
}
}
ArrayList<Room> visited = new ArrayList<Room>();
ArrayList<Room> queued = new ArrayList<Room>();
queued.add(map[0][0]);
while (!queued.isEmpty()) {
Room currentRoom = queued.remove(0);
visited.add(currentRoom);
int currentRow = currentRoom.getRow();
int currentColumn = currentRoom.getColumn();
if (currentRoom.hasNorthDoor() && !(visited.contains(map[currentRow - 1][currentColumn])
|| queued.contains(map[currentRow - 1][currentColumn]))) {
queued.add(0, map[currentRow - 1][currentColumn]);
}
if (currentRoom.hasEastDoor() && !(visited.contains(map[currentRow][currentColumn + 1])
|| queued.contains(map[currentRow][currentColumn + 1]))) {
queued.add(0, map[currentRow][currentColumn + 1]);
}
if (currentRoom.hasSouthDoor() && !(visited.contains(map[currentRow + 1][currentColumn])
|| queued.contains(map[currentRow + 1][currentColumn]))) {
queued.add(0, map[currentRow + 1][currentColumn]);
}
if (currentRoom.hasWestDoor() && !(visited.contains(map[currentRow][currentColumn - 1])
|| queued.contains(map[currentRow][currentColumn - 1]))) {
queued.add(0, map[currentRow][currentColumn - 1]);
}
}
return visited.size() == roomList.size();
}
public Room[][] repairMapLinks(Room[][] rooms) {
for (int row = 0; row < rooms.length; row++) {// Check Links
for (int column = 0; column < rooms[row].length; column++) {
boolean connectedNorth = false;
boolean connectedEast = false;
boolean connectedSouth = false;
boolean connectedWest = false;
if (row == 0) {// Top row
if (column == 0) {// left-most column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly beneath
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
} else if (column == rooms[row].length - 1) {// right-most column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly beneath
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
} else {// intermediate column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly below
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
}
} else if (row == rooms.length - 1) {// Bottom row
if (column == 0) {// left-most column
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
} else if (column == rooms[row].length - 1) {// right-most column
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
} else {// intermediate column
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
}
} else {// Intermediate row
if (column == 0) {// left-most column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly below
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
} else if (column == rooms[row].length - 1) {// right-most column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly below
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
} else {// intermediate column
connectedSouth = checkSingleLink(row, column, row + 1, column, rooms);// Directly below
connectedEast = checkSingleLink(row, column, row, column + 1, rooms);// Directly to the right
connectedWest = checkSingleLink(row, column, row, column - 1, rooms);// Directly to the left
connectedNorth = checkSingleLink(row, column, row - 1, column, rooms);// Directly above
}
}
if (!connectedNorth) {
rooms[row][column].removeDoor("north");
}
if (!connectedEast) {
rooms[row][column].removeDoor("east");
}
if (!connectedSouth) {
rooms[row][column].removeDoor("south");
}
if (!connectedWest) {
rooms[row][column].removeDoor("west");
}
}
}
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms.length; j++) {
if (rooms[i][j].getDoors().size() == 0) {
rooms[i][j].setConnection(false);
} else {
rooms[i][j].setConnection(true);
}
}
}
return rooms;
}
public boolean checkSingleLink(int row1, int column1, int row2, int column2, Room[][] roomList) {
if (!Utils.twoDimensionalArrayInBounds(row1, column1, row2, column2, roomList.length, roomList[0].length)) {
return false;
}
Room room1 = roomList[row1][column1];
Room room2 = roomList[row2][column2];
boolean room1n = false;
boolean room1e = false;
boolean room1s = false;
boolean room1w = false;
boolean room2n = false;
boolean room2e = false;
boolean room2s = false;
boolean room2w = false;
if (room1.hasNorthDoor()) {
room1n = true;
}
if (room1.hasEastDoor()) {
room1e = true;
}
if (room1.hasSouthDoor()) {
room1s = true;
}
if (room1.hasWestDoor()) {
room1w = true;
}
if (room2.hasNorthDoor()) {
room2n = true;
}
if (room2.hasEastDoor()) {
room2e = true;
}
if (room2.hasSouthDoor()) {
room2s = true;
}
if (room2.hasWestDoor()) {
room2w = true;
}
if ((Math.abs(row1 - row2) == 1 || Math.abs(column1 - column2) == 1)) {
if ((row2 > row1) && (column2 == column1)) {// room2 is directly below room1
return (room2n && room1s);
} else if ((row2 < row1) && (column2 == column1)) {// room1 is directly below room2
return (room2s && room1n);
} else if ((row2 == row1) && (column2 < column1)) {// room2 is directly to the left of room1
return (room2e && room1w);
} else if ((row2 == row1) && (column2 > column1)) {// room2 is directly to the right of room1
return (room2w && room1e);
}
return false;
} else {
return false;
}
}
public Room[][] addNpcs(Room[][] rooms) {
for (int row = 0; row < rooms.length; row++) {
for (int column = 0; column < rooms[row].length; column++) {
if (!(row == 0 && column == 0)) {
if (Utils.randInt(0, 4) == 0) {
rooms[row][column]
.addNpc(new NonPlayerCharacter(Utils.randInt(1, 5) * 2, Utils.randInt(1, 4), "goblin"));
}
if (Utils.randInt(0, 9) == 0) {
rooms[row][column]
.addNpc(new NonPlayerCharacter(Utils.randInt(2, 10) * 2, Utils.randInt(5, 10), "orc"));
}
}
}
}
rooms[Utils.randInt(3, rooms.length - 1)][Utils.randInt(3, rooms[0].length - 1)]
.addNpc(new NonPlayerCharacter(40, 25, "dragon"));
return rooms;
}
public Room[][] getRooms() {
return mapRooms;
}
public Room getRoom(int row, int column) {
return mapRooms[row][column];
}
public String toString() {
String returnString = "";
for (int i = 0; i < mapRooms.length; i++) {
for (int j = 0; j < mapRooms.length; j++) {
returnString += "Room at location " + i + "," + j + ": ";
returnString += mapRooms[i][j].toString() + "\n\n";
}
}
return returnString;
}
public String toPrettyString(Player player) {
int playerRow = player.getRow();
int playerColumn = player.getColumn();
ArrayList<Room> playerVisitedRooms = new ArrayList<Room>();
for (Room room : player.getVisited()) {
playerVisitedRooms.add(room);
}
String result = "";
// add top border start
for (int i = 0; i < mapRooms.length; i++) {
result += "--------";
}
result += "------\n| ";
// add top border end
// add top padding start
for (int i = 0; i < mapRooms[0].length; i++) {
result += " ";
}
result += " |\n";
// add top padding end
// add rooms start
for (int i = 0; i < mapRooms.length; i++) {
result += "| ";// side border
for (int j = 0; j < mapRooms[i].length; j++) {
if (playerVisitedRooms.contains(mapRooms[i][j])) {
if (mapRooms[i][j].hasNorthDoor()) {
result += " -+- ";
} else {
result += " --- ";
}
} else {
result += " ";
}
}
result += " |\n| ";// side border
for (int j = 0; j < mapRooms[i].length; j++) {
if (playerVisitedRooms.contains(mapRooms[i][j])) {
if (mapRooms[i][j].hasWestDoor()) {
result += "+";
} else {
result += "|";
}
if (i == playerRow && j == playerColumn) {
result += " x ";
} else {
result += " ";
}
if (mapRooms[i][j].hasEastDoor()) {
result += "+ ";
} else {
result += "| ";
}
} else
result += " ";
}
result += " |\n| ";// side border
for (int j = 0; j < mapRooms[i].length; j++) {
if (playerVisitedRooms.contains(mapRooms[i][j])) {
if (mapRooms[i][j].hasSouthDoor()) {
result += " -+- ";
} else {
result += " --- ";
}
} else
result += " ";
}
result += " |\n| ";// sider border
for (int j = 0; j < mapRooms[i].length; j++) {
result += " ";
}
result += " |\n";
}
// add rooms end
// add bottom border start
for (int i = 0; i < mapRooms.length; i++) {
result += "--------";
}
result += "------";
// add bottom border end
return result;
}
public boolean save() {
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(this);
objectOutputStream.close();
fileOutputStream.close();
return true;
} catch (IOException exception) {
System.err.println(exception);
return false;
}
}
public static Map restore() {
try {
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Map map = (Map) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
return map;
} catch (Exception exception) { // IOException, ClassNotFoundException
return null;
}
}
}