-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotMaze.java
More file actions
273 lines (246 loc) · 8.95 KB
/
Copy pathRobotMaze.java
File metadata and controls
273 lines (246 loc) · 8.95 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
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.Random;
/******************************************************************************
* Compilation: javac Maze.java
* Execution: java Maze.java n
* Dependencies: StdDraw.java // Library from Princeton
*
* @author: https://algs4.cs.princeton.edu/41graph/Maze.java.html
* @author: melmccord
* @author: rowanholop
*
* This is a concrete class that creates our RobotMaze and places our Robots.
*
* Class based off of Princeton's Maze.java, keys for methods as follows:
* - Original methods (written by Melanie and Rowan) are marked original.
* - Modified methods (sourced from Princeton but modified by us) are marked modified.
* - Unmodified methods (sourced from Princeton and unchanged) are marked unmodified.
*
******************************************************************************/
public class RobotMaze {
private int n; // dimension of maze
private boolean[][] north; // is there a wall to north of cell i, j
private boolean[][] east;
private boolean[][] south;
private boolean[][] west;
private boolean[][] visited;
private boolean[][] occupied;
private Bob bob; // **NEW CODE ADDITION**
private Alice alice; // **NEW CODE ADDITION**
private Robot[][] robots; // **NEW CODE ADDITION**
private boolean done = false;
Random generator; // **NEW CODE ADDITION**
MazeRobotFactory factory; // **NEW CODE ADDITION**
public RobotMaze(int n) {
this.n = n;
StdDraw.setXscale(0, n+2);
StdDraw.setYscale(0, n+2);
generator = new Random(); // **NEW CODE ADDITION**
factory = new MazeRobotFactory(); // **NEW CODE ADDITION**
init();
generate();
}
/*
* Original method
*/
private void placeBots() {
// place bob and alice
bob = factory.deliverBob();
occupied[1][1] = true;
alice = factory.deliverAlice();
robots[n][n] = alice;
occupied[n][n] = true;
// find a random number between 0 and .05 for how many bots will be generated
double number = generator.nextDouble() * .05;
// add some random robots
for (int i = 0; i < n*n*number; i++) {
int x = 2 + generator.nextInt(n-2);
int y = 2 + generator.nextInt(n-2);
// mark walls as existing so bob cannot pass
east[x][y] = west[x][y] = true;
north[x][y] = south[x][y] = true;
robots[x][y] = factory.deliverARobot();
occupied[x][y] = true;
StdDraw.setPenColor(StdDraw.GREEN);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.375);
}
}
/*
* Original method
*/
private void robotsInteract(int x, int y) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text((n+2)/2, .5, bob.speak());
StdDraw.show();
StdDraw.pause(2000);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle((n+2)/2, 0, (n+2)/2, .95);
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text((n+2)/2, .5, robots[x][y].speak());
StdDraw.show();
StdDraw.pause(2000);
StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle((n+2)/2, 0, (n+2)/2, .95);
if (robots[x][y].equals(alice)) {
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.text((n+2)/2, .5, bob.foundAlice());
StdDraw.show();
StdDraw.pause(2000);
}
}
/*
* Modified method
*/
// generate the maze starting from lower left
private void generate() {
generate(1, 1);
placeBots(); // **NEW CODE ADDITION**
}
/*
* Modified method
*/
private void init() {
// initialize border cells as already visited
visited = new boolean[n+2][n+2];
for (int x = 0; x < n+2; x++) {
visited[x][0] = true;
visited[x][n+1] = true;
}
for (int y = 0; y < n+2; y++) {
visited[0][y] = true;
visited[n+1][y] = true;
}
// initialze all walls as present & all squares as unoccupied
north = new boolean[n+2][n+2];
east = new boolean[n+2][n+2];
south = new boolean[n+2][n+2];
west = new boolean[n+2][n+2];
occupied = new boolean[n+2][n+2]; // **NEW CODE ADDITION**
robots = new Robot[n+2][n+2]; // **NEW CODE ADDITION**
for (int x = 0; x < n+2; x++) {
for (int y = 0; y < n+2; y++) {
north[x][y] = true;
east[x][y] = true;
south[x][y] = true;
west[x][y] = true;
occupied[x][y] = false; // **NEW CODE ADDITION**
robots[x][y] = null; // **NEW CODE ADDITION**
}
}
}
/*
* Modified method
*/
// solve the maze using depth-first search
private void solve(int x, int y) {
if (x == 0 || y == 0 || x == n+1 || y == n+1) return;
if ((x == 1 && y == 1) && visited[x][y]) {
StdDraw.setPenColor(StdDraw.BLACK); // **NEW CODE ADDITION**
StdDraw.text((n+2)/2, .5, bob.failed()); // **NEW CODE ADDITION**
}
bob.move(); // **NEW CODE ADDITION**
if (done || visited[x][y]) return;
visited[x][y] = true;
if ((x!=1 && y!=1) && occupied[x][y]) { // **NEW CODE ADDITION**
robotsInteract(x, y); // **NEW CODE ADDITION**
}
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
StdDraw.show();
StdDraw.pause(30);
// reached end
if (x == n && y == n) {
done = true;
}
if (!north[x][y]) solve(x, y + 1);
if (!east[x][y]) solve(x + 1, y);
if (!south[x][y]) solve(x, y - 1);
if (!west[x][y]) solve(x - 1, y);
if (done) return;
StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
StdDraw.filledCircle(x + 0.5, y + 0.5, 0.25);
StdDraw.show();
StdDraw.pause(30);
}
/*
* Modified method
*/
// draw the maze
public void draw() {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledCircle(n + 0.5, n + 0.5, 0.375); // Dot for alice
StdDraw.setPenColor(StdDraw.BLUE);
StdDraw.filledCircle(1.5, 1.5, 0.375); // Dot for Bob
StdDraw.setPenColor(StdDraw.BLACK);
// Make sure our outer edges are filled even though they're occupied
StdDraw.line(1, 1, 2, 1); // **NEW CODE ADDITION**
StdDraw.line(1, 1, 1, 2); // **NEW CODE ADDITION**
StdDraw.line(n, n+1, n+1, n+1); // **NEW CODE ADDITION**
StdDraw.line(n+1, n, n+1, n+1); // **NEW CODE ADDITION**
/*
* For loop was slightly modified to make sure occupied squares are not drawn
*/
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= n; y++) {
if (south[x][y] && !occupied[x][y]) StdDraw.line(x, y, x+1, y);
if (north[x][y] && !occupied [x][y]) StdDraw.line(x, y+1, x+1, y+1);
if (west[x][y] && !occupied[x][y]) StdDraw.line(x, y, x, y+1);
if (east[x][y] && !occupied [x][y]) StdDraw.line(x+1, y, x+1, y+1);
}
}
StdDraw.show();
StdDraw.pause(1000);
}
/*
* Unmodified method
*/
// solve the maze starting from the start state
public void solve() {
for (int x = 1; x <= n; x++)
for (int y = 1; y <= n; y++)
visited[x][y] = false;
done = false;
solve(1, 1);
}
/*
* Unmodified method
*/
// generate the maze
private void generate(int x, int y) {
visited[x][y] = true;
// while there is an unvisited neighbor
while (!visited[x][y+1] || !visited[x+1][y] || !visited[x][y-1] || !visited[x-1][y]) {
// pick random neighbor (could use Knuth's trick instead)
while (true) {
int r = generator.nextInt(4);
if (r == 0 && !visited[x][y+1]) {
north[x][y] = false;
south[x][y+1] = false;
generate(x, y + 1);
break;
}
else if (r == 1 && !visited[x+1][y]) {
east[x][y] = false;
west[x+1][y] = false;
generate(x+1, y);
break;
}
else if (r == 2 && !visited[x][y-1]) {
south[x][y] = false;
north[x][y-1] = false;
generate(x, y-1);
break;
}
else if (r == 3 && !visited[x-1][y]) {
west[x][y] = false;
east[x-1][y] = false;
generate(x-1, y);
break;
}
}
}
}
}