Repository navigation
Expand file tree
/
Copy pathBoard.java
More file actions
363 lines (268 loc) · 12.9 KB
/
Copy pathBoard.java
File metadata and controls
363 lines (268 loc) · 12.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package engine.board;
import java.util.ArrayList;
import engine.GameManager;
import exception.CannotFieldException;
import exception.IllegalDestroyException;
import exception.IllegalMovementException;
import exception.IllegalSwapException;
import exception.InvalidMarbleException;
import model.Colour;
import model.player.Marble;
@SuppressWarnings("unused")
public class Board implements BoardManager {
private final ArrayList<Cell> track;
private final ArrayList<SafeZone> safeZones;
private final GameManager gameManager;
private int splitDistance;
public Board(ArrayList<Colour> colourOrder, GameManager gameManager) {
this.track = new ArrayList<>();
this.safeZones = new ArrayList<>();
this.gameManager = gameManager;
for (int i = 0; i < 100; i++) {
this.track.add(new Cell(CellType.NORMAL));
if (i % 25 == 0)
this.track.get(i).setCellType(CellType.BASE);
else if ((i+2) % 25 == 0)
this.track.get(i).setCellType(CellType.ENTRY);
}
for(int i = 0; i < 8; i++)
this.assignTrapCell();
for (int i = 0; i < 4; i++)
this.safeZones.add(new SafeZone(colourOrder.get(i)));
splitDistance = 3;
}
public ArrayList<Cell> getTrack() {
return this.track;
}
public ArrayList<SafeZone> getSafeZones() {
return this.safeZones;
}
public void setSplitDistance(int splitDistance) {
this.splitDistance = splitDistance;
}
private void assignTrapCell() {
int randIndex = -1;
do
randIndex = (int)(Math.random() * 100);
while(this.track.get(randIndex).getCellType() != CellType.NORMAL || this.track.get(randIndex).isTrap());
this.track.get(randIndex).setTrap(true);
}
private ArrayList<Cell> getSafeZone(Colour colour) {
for (int i = 0; i < 4; i++)
if (this.safeZones.get(i).getColour() == colour)
return this.safeZones.get(i).getCells();
return null;
}
private int getPositionInPath(ArrayList<Cell> path, Marble marble) {
for(int i = 0; i < path.size(); i++) {
if(path.get(i).getMarble() == marble)
return i;
}
return -1;
}
private int getBasePosition(Colour colour) {
for(int i = 0; i < safeZones.size(); i++) {
if(safeZones.get(i).getColour() == colour)
return i * 25;
}
return -1;
}
private int getEntryPosition(Colour colour) {
int idx = getBasePosition(colour);
if(idx == -1)
return -1;
else
return (idx - 2 + 100) % 100;
}
private ArrayList<Cell> validateSteps(Marble marble, int steps) throws IllegalMovementException {
Colour ownerColour = gameManager.getActivePlayerColour();
ArrayList<Cell> safeZone = getSafeZone(marble.getColour());
int entryPosition = getEntryPosition(ownerColour);
int positionOnTrack = getPositionInPath(track, marble);
int positionInSafeZone = getPositionInPath(safeZone, marble);
ArrayList<Cell> fullPath = new ArrayList<>();
if (positionOnTrack == -1 && positionInSafeZone == -1)
throw new IllegalMovementException("Cannot move a marble that is not on track nor Safe Zone");
if (positionOnTrack != -1) {
int distanceToEntry = entryPosition - positionOnTrack;
if (marble.getColour() == ownerColour && steps > 0 && distanceToEntry >= 0 && steps > distanceToEntry + 4)
throw new IllegalMovementException("Rank is too high!");
if (marble.getColour() == ownerColour && steps > 0 && distanceToEntry >= 0 && steps > distanceToEntry) {
fullPath.addAll(track.subList(positionOnTrack, entryPosition + 1));
fullPath.addAll(safeZone.subList(0, steps - distanceToEntry));
}
else {
int target = (positionOnTrack + steps + 100) % 100;
if (steps > 0) {
int current = positionOnTrack;
while (true) {
fullPath.add(track.get(current));
if (current == target) {
break;
}
current = (current + 1 + 100) % 100;
}
}
else {
int current = positionOnTrack;
while (true) {
fullPath.add(track.get(current));
if (current == target) {
break;
}
current = (current - 1 + 100) % 100;
}
}
}
}
if (positionInSafeZone != -1) {
if (steps < 0)
throw new IllegalMovementException("Cannot move a marble backwards in Safe Zone");
int distanceLeft = 3 - positionInSafeZone;
if (steps > distanceLeft)
throw new IllegalMovementException("Rank is too high!");
fullPath.addAll(safeZone.subList(positionInSafeZone, positionInSafeZone + steps + 1));
}
return fullPath;
}
private void validatePath(Marble marble, ArrayList<Cell> fullPath, boolean destroy) throws IllegalMovementException{
Colour ownerColour = gameManager.getActivePlayerColour();
int marbleCount = 0;
for(int i = 1; i < fullPath.size(); i++) {
Cell cell = fullPath.get(i);
if (cell.getMarble() != null) {
if (i != fullPath.size() - 1) //counting marbles in my path excluding target
marbleCount++;
if (cell.getCellType() == CellType.SAFE)
throw new IllegalMovementException("Cannot bypass my Safe Zone marbles!");
if (cell.getCellType() == CellType.BASE && track.indexOf(cell) == getBasePosition(cell.getMarble().getColour()))
throw new IllegalMovementException("Cannot bypass or land on marbles in their Base Cell!");
if (!destroy) {
//not marble colour as even with moving opponent marble I cannot kill my own marbles not his
if (ownerColour == cell.getMarble().getColour())
throw new IllegalMovementException("Cannot bypass or land on my own marble!");
//going into safe zone with a marble in the entry
if (cell.getCellType() == CellType.ENTRY && (i+1) < fullPath.size() && fullPath.get(i+1).getCellType() == CellType.SAFE)
throw new IllegalMovementException("Cannot bypass a marble blocking my Safe Zone!");
if (marbleCount > 1)
throw new IllegalMovementException("Cannot bypass more than 1 marble in my path!");
}
}
}
}
private void move(Marble marble, ArrayList<Cell> fullPath, boolean destroy) throws IllegalDestroyException {
Cell currentCell = fullPath.get(0);
Cell targetCell = fullPath.get(fullPath.size()-1);
currentCell.setMarble(null);
if (destroy) {
for(Cell cell : fullPath) {
if (cell.getMarble() != null)
destroyMarble(cell.getMarble());
}
}
else if (targetCell.getMarble() != null)
destroyMarble(targetCell.getMarble());
targetCell.setMarble(marble);
if(targetCell.isTrap()) {
destroyMarble(marble);
targetCell.setTrap(false);
assignTrapCell();
}
}
private void validateSwap(Marble marble_1, Marble marble_2) throws IllegalSwapException {
Colour ownerColour = gameManager.getActivePlayerColour();
int trackPosition_1 = getPositionInPath(track, marble_1);
int trackPosition_2 = getPositionInPath(track, marble_2);
if (trackPosition_1 == -1 || trackPosition_2 == -1)
throw new IllegalSwapException("Cannot swap marbles that are not on track.");
Cell cell_1 = track.get(trackPosition_1);
Cell cell_2 = track.get(trackPosition_2);
if(ownerColour != marble_1.getColour() && cell_1.getCellType() == CellType.BASE && trackPosition_1 == getBasePosition(cell_1.getMarble().getColour())
|| ownerColour != marble_2.getColour() && cell_2.getCellType() == CellType.BASE && trackPosition_2 == getBasePosition(cell_2.getMarble().getColour()))
throw new IllegalSwapException("Marbles that are in their Base Cell can not be selected as a swap target.");
}
private void validateDestroy(int positionInPath) throws IllegalDestroyException {
if (positionInPath == -1)
throw new IllegalDestroyException("Cannot burn marbles that aren't on track.");
Cell current = track.get(positionInPath);
if(current.getCellType() == CellType.BASE && current.getMarble() != null && positionInPath == getBasePosition(current.getMarble().getColour()))
throw new IllegalDestroyException("Cannot burn marbles that are safe in their Base Cell.");
}
private void validateFielding(Cell occupiedBaseCell) throws CannotFieldException {
if (occupiedBaseCell.getMarble().getColour() == gameManager.getActivePlayerColour())
throw new CannotFieldException("One of your marbles is already on your Base Cell");
}
private void validateSaving(int positionInSafeZone, int positionOnTrack) throws InvalidMarbleException {
if(positionInSafeZone != -1)
throw new InvalidMarbleException("Cannot save marbles that are already in the Safe Zone.");
if(positionOnTrack == -1)
throw new InvalidMarbleException("Cannot save marbles that aren't on track.");
}
@Override
public int getSplitDistance() {
return this.splitDistance;
}
@Override
public void moveBy(Marble marble, int steps, boolean destroy) throws IllegalMovementException, IllegalDestroyException{
ArrayList<Cell> fullPath = validateSteps(marble, steps);
validatePath(marble, fullPath, destroy);
move(marble, fullPath, destroy);
}
@Override
public void swap(Marble marble_1, Marble marble_2) throws IllegalSwapException {
validateSwap(marble_1, marble_2);
int trackPosition_1 = getPositionInPath(track, marble_1);
int trackPosition_2 = getPositionInPath(track, marble_2);
track.get(trackPosition_1).setMarble(marble_2);
track.get(trackPosition_2).setMarble(marble_1);
}
@Override
public void destroyMarble(Marble marble) throws IllegalDestroyException {
Colour ownerColour = gameManager.getActivePlayerColour();
int positionOnTrack = getPositionInPath(track, marble);
if (marble.getColour() != ownerColour)
validateDestroy(positionOnTrack);
this.track.get(positionOnTrack).setMarble(null);
this.gameManager.sendHome(marble);
}
@Override
public void sendToBase(Marble marble) throws CannotFieldException, IllegalDestroyException {
int basePosition = getBasePosition(marble.getColour());
Cell baseCell = this.track.get(basePosition);
if(baseCell.getMarble() != null) {
validateFielding(baseCell);
destroyMarble(baseCell.getMarble());
}
baseCell.setMarble(marble);
}
@Override
public void sendToSafe(Marble marble) throws InvalidMarbleException {
int positionOnTrack = getPositionInPath(track, marble);
int positionInSafeZone = getPositionInPath(getSafeZone(marble.getColour()), marble);
validateSaving(positionInSafeZone, positionOnTrack);
ArrayList<Cell> safeZone = getSafeZone(marble.getColour());
ArrayList<Cell> freeSpaces = new ArrayList<>();
for(Cell cell : safeZone){
if(cell.getMarble() == null)
freeSpaces.add(cell);
}
int randIndex = (int)(Math.random() * freeSpaces.size());
freeSpaces.get(randIndex).setMarble(marble);
this.track.get(positionOnTrack).setMarble(null);
}
@Override
public ArrayList<Marble> getActionableMarbles() {
ArrayList<Marble> marbles = new ArrayList<>();
Colour colour = gameManager.getActivePlayerColour();
ArrayList<Cell> safeZone = getSafeZone(colour);
for(Cell cell : safeZone) {
if(cell.getMarble() != null)
marbles.add(cell.getMarble());
}
for(Cell cell : track) {
if(cell.getMarble() != null)
marbles.add(cell.getMarble());
}
return marbles;
}
}