Repository navigation
Expand file tree
/
Copy pathMazeReadException.java
More file actions
240 lines (218 loc) · 7.01 KB
/
Copy pathMazeReadException.java
File metadata and controls
240 lines (218 loc) · 7.01 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
/**
* Class that contains all the logic to model a Maze with Treasures, Monsters, and an Explorer.
*
* @author Arkadiy Kraminsky
* @version 5/09/2013
*/
import java.util.*;
import java.io.*;
public class Maze{
// named constants
public static final int ACTIVE = 0;
public static final int EXPLORER_WIN = 1;
public static final int MONSTER_WIN = 2;
// instance variables
private Square[][] maze;
private ArrayList<RandomOccupant> randOccupants;
private Explorer explorer;
private int rows;
private int cols;
/**
*Default constructor creates the array list.
*/
public Maze(){
randOccupants = new ArrayList<RandomOccupant>();
}
/**
* Constructor for objects of class Maze
*/
public Maze(Square[][] maze, int rows, int cols){
randOccupants = new ArrayList<RandomOccupant>();
this.maze = maze;
this.rows = rows;
this.cols = cols;
}
// QUERIES
public Square getSquare(int row, int col){
return maze[row][col];
}
public int rows(){
return rows;
}
public int cols(){
return cols;
}
public String explorerName(){
return explorer.name();
}
public Explorer getExplorer(){
return explorer;
}
// CHANGE - Implement the following two methods. I have them stubbed to return dummy values just so it will compile.
// Your getRandomOccupant should return the occupant from the ArrayList at the specified index.
public RandomOccupant getRandomOccupant(int index){
return randOccupants.get(index);
}
public int getNumRandOccupants(){
return randOccupants.size();
}
public void addRandomOccupant(RandomOccupant ro){
randOccupants.add(ro);
}
public void setExplorer(Explorer e){
explorer = e;
}
public void explorerMove(int key){
explorer.move(key);
}
public void randMove(){
for(RandomOccupant ro : randOccupants){
ro.move();
}
}
/**
* Returns the status of the game.
*
* If all treasures have been found, return EXPLORER_WIN.
* If not, check each maze occupant, if it is a Monster and
* it is in the same location as the Explorer, return
* MONSTER_WIN. Note that you can use == to check locations, do you know why?
* Otherwise, return ACTIVE.
*/
public int gameStatus(){
int status = ACTIVE;
if(foundAllTreasures()) return EXPLORER_WIN;
for(RandomOccupant ro : randOccupants){
if(ro instanceof Monster){
if(explorer.location() == ro.location()) return MONSTER_WIN;
}
}
return status;
}
private boolean foundAllTreasures(){
boolean foundAll = true;
for(RandomOccupant ro : randOccupants){
if(ro instanceof Treasure){
Treasure t = (Treasure) ro;
if(!t.found()) return false;
}
}
return foundAll;
}
public void lookAround(Square s){
int row = s.row();
int col = s.col();
// Clear what was previously in view
resetInView();
// Set the current square so that we are viewing it (obviously)
s.setInView(true);
if(!s.wall(Square.UP)){
getSquare(row - 1, col).setInView(true);
if(!getSquare(row - 1, col).wall(Square.LEFT)) getSquare(row - 1, col - 1).setInView(true);
if(!getSquare(row - 1, col).wall(Square.RIGHT)) getSquare(row - 1, col + 1).setInView(true);
}
if(!s.wall(Square.RIGHT)){
getSquare(row, col + 1).setInView(true);
if(!getSquare(row, col + 1).wall(Square.UP)) getSquare(row - 1, col + 1).setInView(true);
if(!getSquare(row, col + 1).wall(Square.DOWN)) getSquare(row + 1, col + 1).setInView(true);
}
if(!s.wall(Square.LEFT)){
getSquare(row, col - 1).setInView(true);
if(!getSquare(row, col - 1).wall(Square.UP)) getSquare(row - 1, col - 1).setInView(true);
if(!getSquare(row, col - 1).wall(Square.DOWN)) getSquare(row + 1, col - 1).setInView(true);
}
if(!s.wall(Square.DOWN)){
getSquare(row + 1, col).setInView(true);
if(!getSquare(row + 1, col).wall(Square.LEFT)) getSquare(row + 1, col - 1).setInView(true);
if(!getSquare(row + 1, col).wall(Square.RIGHT)) getSquare(row + 1, col + 1).setInView(true);
}
}
private void resetInView(){
for (int i = 0; i<rows; i++){
for (int j = 0; j<cols; j++){
maze[i][j].setInView(false);
}
}
}
public void writeMazeToFile(String filename) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter(filename));
bw.write(rows()+","+cols());
bw.newLine();
for(int i = 0; i < rows(); i++){
for(int j = 0; j < cols(); j++){
bw.write(getSquare(i, j).toText(','));
bw.newLine();
}
}
bw.write(explorer.toText(','));
bw.newLine();
for(int i = 0; i < getNumRandOccupants(); i++){
bw.write(getRandomOccupant(i).toText(','));
bw.newLine();
}
bw.close();
}
public void readMazeFromFile(String filename) throws IOException, FileNotFoundException, MazeReadException{
Scanner fs = new Scanner(new File(filename));
String line = fs.nextLine();
Scanner ls = new Scanner(line);
ls.useDelimiter(",");
int count = 1;
if(!ls.hasNextInt()) throw new MazeReadException("Rows and columns not specified.", line, count);
int r = ls.nextInt();
int c = ls.nextInt();
this.maze = new Square[r][c];
this.rows = r;
this.cols = c;
while(fs.hasNextLine()){
line = fs.nextLine();
ls = new Scanner(line);
ls.useDelimiter(",");
String type = null;
try{
count++;
type = ls.next();
}catch(NoSuchElementException e){
throw new MazeReadException("Line format or other error.", line, count);
}
if(type.equals("Square")){
r = ls.nextInt();
c = ls.nextInt();
if(getSquare(r,c) != null) throw new MazeReadException("Duplicate square.", line, count);
this.maze[r][c] = new Square(r,c);
try{
getSquare(r,c).toObject(ls);
}catch(Exception e){
throw new MazeReadException("Line format or other error.", line, count);
}
}
else if(type.equals("Explorer")){
this.explorer = new Explorer(this);
try{
explorer.toObject(ls);
}catch(Exception e){
throw new MazeReadException("Line format or other error.", line, count);
}
}
else if(type.equals("Treasure")){
Treasure t = new Treasure(this);
try{
t.toObject(ls);
}catch(Exception e){
throw new MazeReadException("Line format or other error.", line, count);
}
addRandomOccupant(t);
}
else if(type.equals("Monster")){
Monster m = new Monster(this);
try{
m.toObject(ls);
}catch(Exception e){
throw new MazeReadException("Line format or other error.", line, count);
}
addRandomOccupant(m);
}
else throw new MazeReadException("Unknown type.", line, count);
}
}
}