-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreasureHunt.java
More file actions
225 lines (179 loc) · 6.54 KB
/
TreasureHunt.java
File metadata and controls
225 lines (179 loc) · 6.54 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
import java.util.Random;
public class TreasureHunt {
// ---------------------------
// 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;
}
}
// ---------------------------
// Fields
// ---------------------------
public final int edgeLength = 20;
private final Collectible[][][] matrix;
private final Coordinate[] hintCoords;
private final Coordinate goalCoord;
private final Coordinate currentCoord;
private static final Random rand = new Random();
private int steps = 0;
// ---------------------------
// Constructor
// ---------------------------
public TreasureHunt() {
this.matrix = new Collectible[edgeLength][edgeLength][edgeLength];
this.hintCoords = new Coordinate[9];
this.currentCoord = new Coordinate(0, 0, 0);
this.goalCoord = createRandomCoord(true);
setHintCoords();
populateMatrix();
}
// ---------------------------
// 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);
}
// ---------------------------
// 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 < 9) {
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);
}
}
}