-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapCell.java
More file actions
294 lines (246 loc) · 7.67 KB
/
MapCell.java
File metadata and controls
294 lines (246 loc) · 7.67 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
import java.awt.Color;
public class MapCell extends CellComponent {
private CellType type; // Stores the current type of this cell. This value changes as the cells in the map are marked
private CellType originalType; // Type initially assigned to this cell
private boolean isStart; // Is this the starting cell?
private boolean isTarget; // Is this a jewel cell?
private MapCell[] neighbors; // Stores the cells neighboring THIS one
private int numNeighbours = 4;
private int timeDelay; // Time that the program waits before moving to an adjacent cell
private int identifier;
public static enum CellType {
BLOCK, INITIAL, TARGET, CROSS_PATH, INITIAL_PROCESSED, TARGET_PROCESSED, INITIAL_POPPED, IN_STACK, OUT_STACK, HORIZ_PATH, VERT_PATH
};
public MapCell (CellType cType, int delay, int identifier) {
type = cType;
originalType = cType;
timeDelay = delay;
this.isStart = (cType == CellType.INITIAL);
this.isTarget = (cType == CellType.TARGET);
// set the initial color based on the initial type
this.setColor(this.type);
// allocate space for the neighbor array
this.neighbors = new MapCell[numNeighbours];
this.identifier = identifier;
}
/**
* Set the neighbor for this cell using the neighbor index.
*
* The index for the neighbor indicates which side of the square this new
* neighbor is on: 0-3 inclusive.
*
* @param neighbor
* The new cell neighbor
* @param i
* The index specifying which side this neighbor is on (0-3 inclusive)
* @throws InvalidNeighbourIndexException
* When an index is specified that is not 0-3.
*/
public void setNeighbour(MapCell neighbor, int i) throws InvalidNeighbourIndexException {
if (0 <= i && i < numNeighbours)
this.neighbors[i] = neighbor;
else
throw new InvalidNeighbourIndexException(i);
}
/**
* Returns the neighbor for this cell using the neighbor index.
*
* The index for the neighbor indicates in which side of the cell the
* neighbor is: 0-3.
*
* @param i
* The index of the neighbor
* @return The cell that is on the i-th side of the current cell, or null if
* no neighbor
* @throws InvalidNeighbourIndexException
* When an index is specified that is not 0-3.
*/
public MapCell getNeighbour(int i) throws InvalidNeighbourIndexException {
if (0 <= i && i < numNeighbours)
return this.neighbors[i];
else
throw new InvalidNeighbourIndexException(i);
}
/**
* Set the colour/image of the cell in the visual JFrame based on its type and stack status.
*
*
* @param type
* The cell's type from the enum made at the top of this class.
*
*/
private void setColor(CellType t) {
switch (t) {
case BLOCK:
this.setBackground(CellColours.BLOCK);
break;
case INITIAL:
this.setBackground(CellColours.INITIAL);
break;
case TARGET:
this.setBackground(CellColours.TARGET);
break;
case CROSS_PATH:
this.setBackground(CellColours.OMNI_SWITCH);
break;
case TARGET_PROCESSED:
this.setBackground(CellColours.TARGET_PROCESSED);
break;
case INITIAL_PROCESSED:
this.setBackground(CellColours.INITIAL_PROCESSED);
break;
case INITIAL_POPPED:
this.setBackground(CellColours.INITIAL_POPPED);
break;
case IN_STACK:
if (originalType == CellType.VERT_PATH)
setBackground(CellColours.VERT_PATH_PUSHED);
else if (originalType == CellType.HORIZ_PATH)
setBackground(CellColours.HORIZ_PATH_PUSHED);
else if (originalType == CellType.CROSS_PATH)
setBackground(CellColours.CROSS_PATH_PUSHED);
else if (originalType == CellType.INITIAL)
this.setBackground(CellColours.INITIAL_PROCESSED);
else if (originalType == CellType.TARGET)
this.setBackground(CellColours.TARGET_PROCESSED);
else
setBackground(CellColours.IN_STACK);
break;
case OUT_STACK:
if (originalType == CellType.VERT_PATH)
setBackground(CellColours.VERT_PATH_POPPED);
else if (originalType == CellType.HORIZ_PATH)
setBackground(CellColours.HORIZ_PATH_POPPED);
else if (originalType == CellType.CROSS_PATH)
setBackground(CellColours.CROSS_PATH_POPPED);
else if (originalType == CellType.INITIAL)
setBackground(CellColours.INITIAL_POPPED);
else if (originalType == CellType.TARGET)
setBackground(CellColours.TARGET_POPPED);
else
setBackground(CellColours.OUT_STACK);
break;
case VERT_PATH:
this.setBackground(CellColours.VERT_PATH);
break;
case HORIZ_PATH:
this.setBackground(CellColours.HORIZ_PATH);
break;
default:
this.setBackground(CellColours.BLOCK);
break;
}
this.setForeground(Color.BLACK);
}
/**
* This method checks if the current cell is a block/wall.
*
* @return true if this is a cell representing a block/wall, false otherwise.
*/
public boolean isBlackHole() {
return type == CellType.BLOCK;
}
/**
* This method checks if the current cell is an cross-path.
*
* @return true if this cell is a cross-path, false otherwise.
*/
public boolean isCrossPath() {
return originalType == CellType.CROSS_PATH;
}
/**
* This method checks if the current cell has been marked as in stack or out
* of stack.
*
* @return true if this cell has been marked as in stack or out of stack,
* false otherwise.
*/
public boolean isMarked() {
return (type == CellType.IN_STACK) || (type == CellType.OUT_STACK);
}
public boolean isMarkedInStack() {
return type == CellType.IN_STACK;
}
public boolean isMarkedOutStack() {
return type == CellType.OUT_STACK;
}
/**
* This method checks if the current cell is a vertical path.
*
* @return true if this is a vertical path, false otherwise.
*/
public boolean isVerticalPath() {
return originalType == CellType.VERT_PATH;
}
/**
* This method checks if the current cell is a horizontal path.
*
* @return true if this is a horizontal path, false otherwise.
*/
public boolean isHorizontalPath() {
return originalType == CellType.HORIZ_PATH;
}
/**
* This method checks if the current cell is the initial cell.
*
* @return true if this is the initial cell, false otherwise.
*/
public boolean isStart() {
return this.isStart;
}
/**
* This method checks if the current cell is the destination.
*
* @return true if this is the destination cell, false otherwise.
*/
public boolean isTarget() {
return this.isTarget;
}
/**
* This method re-draws the current hexagonal cell
*/
private void reDraw() {
try {
Thread.sleep(timeDelay);
} catch (Exception e) {
System.err.println("Error while issuing time delay\n" + e.getMessage());
}
super.repaint();
}
/**
* This method marks the cell as in-stack and updates the cell's colour
*/
public void markInStack() {
type = CellType.IN_STACK;
setColor(type);
reDraw();
}
/**
* This method marks the cell as popped and updates the cell's colour
*/
public void markOutStack() {
type = CellType.OUT_STACK;
setColor(this.type);
reDraw();
}
/**
* This method marks a jewel cell and updates the cell's colour
*/
public void markTarget() {
this.type = CellType.TARGET_PROCESSED;
this.setColor(this.type);
reDraw();
}
/**
* This method marks the starting cell as the initial cell and updates the cell's
* colour
*/
public void markInitial() {
this.type = CellType.INITIAL_PROCESSED;
this.setColor(this.type);
reDraw();
}
public int getIdentifier() {
return this.identifier;
}
}