-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.java
More file actions
318 lines (300 loc) · 11.2 KB
/
Maze.java
File metadata and controls
318 lines (300 loc) · 11.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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class Maze implements Theme {
private List<List<Cell>> cellList = new ArrayList<List<Cell>>();
private HashMap<Cell, String> lineInfoMap = new HashMap<Cell, String>();
private int mazeSize;
private int rowLength;
private int columnLength;
public Maze() {
}
public void loadMaze(File file) {
int countRow = 0;
int countColumn = 0;
int lineNum = 0;
final String FIELD_SEP = ",";
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
in.readLine();
String line = in.readLine();
while (line != null) {
lineNum++;
line = line.trim();
String[] column = line.split(FIELD_SEP);
int x = isValidXY(column[0], lineNum, line);
int y = isValidXY(column[1], lineNum, line);
Wall northwall = Wall.fromString(column[2], lineNum, line);
Wall southwall = Wall.fromString(column[3], lineNum, line);
Wall eastwall = Wall.fromString(column[4], lineNum, line);
Wall westwall = Wall.fromString(column[5], lineNum, line);
Content content = Content.fromString(column[6], lineNum, line);
for (int i = cellList.size(); i <= x; i++) {
cellList.add(new ArrayList<Cell>());
if (cellList.size() > countRow) {
countRow++;
} // else continue loop
}
List<Cell> cellRow = cellList.get(x);
for (int j = cellRow.size(); j <= y; j++) {
cellRow.add(null);
if (cellRow.size() > countColumn) {
countColumn++;
} // else continue loop
}
Cell cell = new Cell(northwall, southwall, eastwall, westwall, content);
cellRow.set(y, cell);
String lineInfo = "line=" + lineNum + "(" + line + ")";
lineInfoMap.put(cell, lineInfo);
line = in.readLine();
}
rowLength = countColumn;
columnLength = countRow;
mazeSize = rowLength * columnLength;
in.close();
} catch (IOException e) {
System.out.println(e);
}
/* reverses order of rows (x) as intended by maze input file */
Collections.reverse(cellList);
}
/*
* XYcoordinates cannot be negative integers. Mazes larger than 40 x 40 are
* not suitable for Console view
*/
private int isValidXY(String input, int lineNum, String line) {
int i = 0;
try {
i = Integer.parseInt(input);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException(ex.getMessage()
+ ". Invalid maze input file. XYcoordinate must be an integer 0 to 40. Error detected at line="
+ lineNum + "(" + line + ")");
}
if (i >= 0 && i <= 40)
return i;
else
throw new IllegalArgumentException(
"Invalid maze input file. XYcoordinate must be an integer 0 to 40. Error detected at line="
+ lineNum + "(" + line + ")");
}
public void validateMaze() {
checkForNull();
isRegularRectangle();
validateWall();
validateStartEnd(Content.START);
validateStartEnd(Content.END);
}
/* Maze cannot have null cells */
private void checkForNull() {
for (int x = 0; x < cellList.size(); x++) {
List<Cell> cellRow = cellList.get(x);
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
if (cell == null) {
throw new IllegalArgumentException(
"Invalid maze input file, missing lines/cells. Error detected at "
+ (cellList.size() - x - 1) + "," + y);
}
}
}
}
/* Maze cannot have null cells */
private void isRegularRectangle() {
int y = 0;
for (int x = 0; x < cellList.size(); x++) {
int count = 0;
for (y = 0; y < cellList.get(x).size(); y++) {
count++;
}
if (count != rowLength)
throw new IllegalArgumentException(
"Invalid maze input file. Must be a regular rectangle. Error detected for "
+ (cellList.size() - x - 1) + "," + y);
}
}
/* Cell wall type must match neighboring cell wall */
private void validateWall() {
for (int x = 0; x < cellList.size(); x++) {
for (int y = 0; y < cellList.get(x).size(); y++) {
Cell cell = getCell(x, y);
if (cell != null) {
Cell nb = getCell(x - 1, y);
if (nb != null && cell.getNorthwall() != nb.getSouthwall()) {
throw new IllegalArgumentException("Invalid mazefile" + getLineInfo(cell)
+ " Northwall must match Southwall of " + getLineInfo(nb));
}
nb = getCell(x + 1, y);
if (nb != null && cell.getSouthwall() != nb.getNorthwall()) {
throw new IllegalArgumentException("Invalid mazefile " + getLineInfo(cell)
+ " Southwall must match Northwall of " + getLineInfo(nb));
}
nb = getCell(x, y + 1);
if (nb != null && cell.getEastwall() != nb.getWestwall()) {
throw new IllegalArgumentException("Invalid mazefile " + getLineInfo(cell)
+ " Eastwall must match Westwall of " + getLineInfo(nb));
}
nb = getCell(x, y - 1);
if (nb != null && cell.getWestwall() != nb.getEastwall()) {
throw new IllegalArgumentException("Invalid mazefile " + getLineInfo(cell)
+ " Westwall must match Eastwall of " + getLineInfo(nb));
}
}
}
}
}
/* Maze must have exactly one start and one end */
private void validateStartEnd(Content SE) {
List<Cell> listSE = new ArrayList<Cell>();
Cell cellSE = null;
for (int x = 0; x < cellList.size(); x++) {
List<Cell> cellRow = cellList.get(x);
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
if (cell.getContent().equals(SE)) {
listSE.add(cell);
}
}
}
if (listSE.size() == 0)
throw new IllegalArgumentException(getLineInfo(cellSE) + "Maze input file has no " + SE.toString());
else if (listSE.size() > 1)
throw new IllegalArgumentException("Maze input file must have exactly one " + SE.toString()
+ ". Error detected at " + getLineInfo(listSE.get(0)) + "; " + getLineInfo(listSE.get(1)) + "...");
}
public List<List<Cell>> getCellList() {
return cellList;
}
public Cell getCell(int x, int y) {
try {
Cell cell = cellList.get(x).get(y);
return cell;
} catch (IndexOutOfBoundsException exception) {
return null;
}
}
public IntPair getStartPosition() {
IntPair p = new IntPair();
for (int x = 0; x < cellList.size(); x++) {
List<Cell> cellRow = cellList.get(x);
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
if (cell.getContent().equals(Content.START)) {
p.xpos = x;
p.ypos = y;
break;
}
}
}
return p;
}
public int getMazeSize() {
return mazeSize;
}
public void printMaze() {
for (int x = 0; x < cellList.size(); x++) {
List<Cell> cellRow = cellList.get(x);
if (x == 0) {
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
printNSwall(cell.getNorthwall());
}
System.out.println("+");
}
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
if (y == 0)
printEWwall(cell.getWestwall());
printContent(cell.getContent());
printEWwall(cell.getEastwall());
}
System.out.println();
for (int y = 0; y < cellRow.size(); y++) {
Cell cell = cellRow.get(y);
printNSwall(cell.getSouthwall());
}
System.out.println("+");
}
}
private void printNSwall(Wall wall) {
switch (wall) {
case WALL:
System.out.print("+---");
break;
case NO_WALL:
System.out.print("+ ");
break;
case DOOR:
System.out.print("+-d-");
break;
case BREAKABLE:
System.out.print("+-b-");
break;
case FAKE:
System.out.print("+---");
break;
default:
throw new RuntimeException("Unrecognized Wall type " + wall);
}
}
private void printEWwall(Wall wall) {
switch (wall) {
case WALL:
System.out.print("|");
break;
case NO_WALL:
System.out.print(" ");
break;
case DOOR:
System.out.print("d");
break;
case BREAKABLE:
System.out.print("b");
break;
case FAKE:
System.out.print("|");
break;
default:
throw new RuntimeException("Unrecognized Wall type " + wall);
}
}
private void printContent(Content content) {
switch (content) {
case START:
System.out.print(Start + " ");
break;
case END:
System.out.print(" " + End);
break;
case EMPTY:
System.out.print(" ");
break;
case KEY:
System.out.print(" ");
break;
case HAMMER:
System.out.print(" ");
break;
case TROPHY:
System.out.print(" ");
break;
case PLAYER:
System.out.print(Player + " ");
break;
case VISIT:
System.out.print(Visit + " ");
break;
default:
throw new RuntimeException("Unrecognized Content type " + content);
}
}
/* Line information of original input file in case of error detected */
public String getLineInfo(Cell cell) {
String lineInfo = lineInfoMap.get(cell);
return lineInfo;
}
}