-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeadHunt.java
More file actions
253 lines (203 loc) · 7.94 KB
/
DeadHunt.java
File metadata and controls
253 lines (203 loc) · 7.94 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
import java.util.Arrays;
import java.util.Random;
public class DeadHunt {
// ---------------------------
// Inner class: Coordinate
// ---------------------------
public class Coordinate {
private int x, y, z;
private final boolean readonly;
private Coordinate(int x, int y, int z) {
this(x, y, z, false);
}
private Coordinate(int x, int y, int z, boolean readonly) {
this.x = x;
this.y = y;
this.z = z;
this.readonly = readonly;
}
public int x() { return x; }
public int y() { return y; }
public int z() { return z; }
private void moveBy(int dx, int dy, int dz) {
if (readonly)
throw new UnsupportedOperationException("Cannot modify read-only coordinate");
this.x += dx;
this.y += dy;
this.z += dz;
}
private void set(int x, int y, int z) {
if (readonly)
throw new UnsupportedOperationException("Cannot modify read-only coordinate");
this.x = x;
this.y = y;
this.z = z;
}
public int manhattanTo(Coordinate other) {
return Math.abs(this.x - other.x) + Math.abs(this.y - other.y) + Math.abs(this.z - other.z);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Coordinate)) return false;
Coordinate o = (Coordinate) obj;
return x == o.x && y == o.y && z == o.z;
}
@Override
public String toString() {
return "(" + x + "," + y + "," + z + ")";
}
}
// ---------------------------
// Inner Class: TrialResult
// ---------------------------
public class TrialResult {
public final boolean foundGoal;
public final int steps;
private TrialResult(boolean foundGoal, int steps) {
this.foundGoal = foundGoal;
this.steps = steps;
}
}
// ---------------------------
// Inner Class: Collectible
// ---------------------------
public static class Collectible {
private final boolean isHint;
private final Object message;
private Collectible() {
this.isHint = false;
this.message = (String) "goal";
}
private Collectible(int hintDist, int[] goalDistRange) {
this.isHint = true;
this.message = new Object[]{hintDist, goalDistRange};
}
private static Collectible goal() {
return new Collectible();
}
private static Collectible hint(int distToPrev, int[] goalRange) {
return new Collectible(distToPrev, goalRange);
}
public Object[] getMessage() {
if (!isHint) return null;
return (Object[]) this.message;
}
public boolean isHint() {
return this.isHint;
}
public String toString() {
return "Collectible(" + ((Object[]) this.message)[0] + ", " + Arrays.toString((int[]) ((Object[]) this.message)[1]) + ")";
}
}
// ---------------------------
// Fields
// ---------------------------
public final int edgeLength = 20;
private final Collectible[][][] matrix;
public Coordinate[] hintCoords;
private final Coordinate goalCoord;
private final Coordinate currentCoord;
public static final Random rand = new Random();
private int steps = 0;
private final int nHints;
// ---------------------------
// Constructor
// ---------------------------
public DeadHunt() {
this(9);
}
public DeadHunt(int nHints) {
this.matrix = new Collectible[edgeLength][edgeLength][edgeLength];
this.nHints = nHints;
this.hintCoords = new Coordinate[this.nHints];
this.currentCoord = new Coordinate(0, 0, 0);
this.goalCoord = createRandomCoord(true);
setHintCoords();
populateMatrix();
}
public Coordinate getGoalCoord() {
return this.goalCoord;
}
// ---------------------------
// Public API (for students)
// ---------------------------
public Coordinate position() {
return currentCoord;
}
public void jumpTo(int x, int y, int z) throws IllegalArgumentException {
if (x < 0 || x >= edgeLength)
throw new IllegalArgumentException("Error: Cannot set x to " + x + ". Out of bounds");
if (y < 0 || y >= edgeLength)
throw new IllegalArgumentException("Error: Cannot set y to " + y + ". Out of bounds");
if (z < 0 || z >= edgeLength)
throw new IllegalArgumentException("Error: Cannot set z to " + z + ". Out of bounds");
currentCoord.set(x, y, z);
steps++;
}
public Collectible search() {
return matrix[currentCoord.x()][currentCoord.y()][currentCoord.z()];
}
public TrialResult submit() {
boolean found = currentCoord.equals(goalCoord);
return new TrialResult(found, steps);
}
public void moveLeft() { if (canMoveLeft()) { currentCoord.moveBy(-1, 0, 0); steps++; } }
public void moveRight() { if (canMoveRight()) { currentCoord.moveBy( 1, 0, 0); steps++; } }
public void moveUp() { if (canMoveUp()) { currentCoord.moveBy( 0, 1, 0); steps++; } }
public void moveDown() { if (canMoveDown()) { currentCoord.moveBy( 0,-1, 0); steps++; } }
public void moveFront() { if (canMoveFront()) { currentCoord.moveBy( 0, 0, 1); steps++; } }
public void moveBack() { if (canMoveBack()) { currentCoord.moveBy( 0, 0,-1); steps++; } }
public boolean canMoveLeft() { return currentCoord.x() > 0; }
public boolean canMoveRight() { return currentCoord.x() < edgeLength - 1; }
public boolean canMoveUp() { return currentCoord.y() < edgeLength - 1; }
public boolean canMoveDown() { return currentCoord.y() > 0; }
public boolean canMoveFront() { return currentCoord.z() < edgeLength - 1; }
public boolean canMoveBack() { return currentCoord.z() > 0; }
// ---------------------------
// Internal logic
// ---------------------------
private int rand_in_range() {
return rand.nextInt(this.edgeLength);
}
private Coordinate createRandomCoord(boolean readonly) {
return new Coordinate(rand_in_range(), rand_in_range(), rand_in_range(), readonly);
}
private void setHintCoords() {
int count = 0;
while (count < this.nHints) {
Coordinate newCoord = createRandomCoord(true);
boolean duplicate = false;
for (int i = 0; i < count; i++) {
if (hintCoords[i].equals(newCoord)) {
duplicate = true;
break;
}
}
if (!duplicate && !newCoord.equals(goalCoord)) {
hintCoords[count++] = newCoord;
}
}
}
private void populateMatrix() {
populateMatrixGoal();
populateMatrixHints();
}
private void populateMatrixGoal() {
this.matrix[this.goalCoord.x()][this.goalCoord.y()][this.goalCoord.z()] = Collectible.goal();
}
private void populateMatrixHints() {
final int RANGE_SIZE = 5;
for (int i = 0; i < hintCoords.length; i++) {
Coordinate current = hintCoords[i];
Coordinate prev = hintCoords[(i - 1 + hintCoords.length) % hintCoords.length];
int distToPrev = current.manhattanTo(prev);
int distToGoal = current.manhattanTo(goalCoord);
int halfSpan = rand.nextInt(RANGE_SIZE + 1);
int low = Math.max(0, distToGoal - halfSpan);
int high = Math.min(edgeLength * 3, distToGoal + (RANGE_SIZE - halfSpan));
int[] rangeToGoal = new int[] { low, high };
matrix[current.x()][current.y()][current.z()] = Collectible.hint(distToPrev, rangeToGoal);
}
}
}