Repository navigation
Expand file tree
/
Copy pathEntryPointHandler.java
More file actions
421 lines (356 loc) · 14.4 KB
/
Copy pathEntryPointHandler.java
File metadata and controls
421 lines (356 loc) · 14.4 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package application;
import java.util.ArrayList;
import engine.Game;
import engine.board.Cell;
import engine.board.CellType;
import exception.GameException;
import exception.CannotDiscardException;
import exception.IllegalDestroyException;
import exception.IllegalMovementException;
import model.Colour;
import model.card.Card;
import model.player.Marble;
import model.player.Player;
import javafx.animation.PauseTransition;
import javafx.util.Duration;
/**
* Helper class to handle entry point behavior in the UI layer.
* This works around the issue where marbles automatically enter
* the safe zone without stopping at entry points.
*/
public class EntryPointHandler {
private final Game game;
public EntryPointHandler(Game game) {
this.game = game;
}
/**
* Checks if a marble would pass its entry point and enter safe zone
* with the selected card.
* @param player The current player
* @param marble The marble being moved
* @param card The card being played
* @return true if the marble would skip entry point, false otherwise
*/
public boolean wouldSkipEntryPoint(Player player, Marble marble, Card card) {
// Only handle number cards (standard cards with ranks)
if (!card.getName().matches("\\d+|Ten|Jack|Queen|King|Ace")) {
return false;
}
// Get the entry cell for this marble's color
int entryPosition = getEntryPosition(marble.getColour());
int marblePosition = getMarblePosition(marble);
// If marble is already at entry point or in safe zone, no issue
if (marblePosition == entryPosition || marblePosition == -1) {
return false;
}
// Check if the move would take the marble past its entry point
int rank = getRank(card);
if (rank <= 0) {
return false; // Not a forward move
}
// Calculate if the move would pass the entry point
int distance = calculateDistance(marblePosition, entryPosition);
return distance > 0 && distance <= rank;
}
/**
* Checks if a marble at an entry cell is trying to move into the safe zone
* with a card rank that's too high
*/
private boolean isSafeZoneEntryRankTooHigh(Marble marble, Card card) {
// Check if marble is at its entry position
int marblePosition = getMarblePosition(marble);
int entryPosition = getEntryPosition(marble.getColour());
if (marblePosition != entryPosition) {
return false; // Not at entry, not applicable
}
// Get the rank of the card
int rank = getRank(card);
// Safe zone has 4 cells total, so rank > 4 would be too high
return rank > 4;
}
/**
* Checks if there's another marble of the same color in the path into the safe zone
*/
private boolean isMarbleBlockingSafeZoneEntry(Marble marble, Card card) {
// Check if marble is at its entry position
int marblePosition = getMarblePosition(marble);
int entryPosition = getEntryPosition(marble.getColour());
if (marblePosition != entryPosition) {
return false; // Not at entry, not applicable
}
// Get the rank of the card
int rank = getRank(card);
if (rank <= 0) {
return false; // Not a forward move
}
// Check if there are any marbles in the safe zone that would block the path
ArrayList<Cell> safeZone = null;
for (var sz : game.getBoard().getSafeZones()) {
if (sz.getColour() == marble.getColour()) {
safeZone = sz.getCells();
break;
}
}
if (safeZone == null) {
return false;
}
// Check if any cells in the desired path contain a marble
for (int i = 0; i < Math.min(rank, safeZone.size()); i++) {
if (safeZone.get(i).getMarble() != null) {
return true; // Found a blocking marble
}
}
return false;
}
/**
* Checks if the card is a King card and needs special handling.
*/
private boolean isKingCard(Card card) {
return card != null && card.getName().equals("King");
}
/**
* Plays the card for the player, handling the entry point logic.
* @throws GameException If the play is invalid
*/
public void handleCardPlay(Player player) throws GameException {
Card card = player.getSelectedCard();
// If no card selected, use normal behavior
if (card == null) {
player.play();
return;
}
// Special handling for Queen card with no marbles
if (card.getName().equals("Queen") && player.getSelectedMarbles().isEmpty()) {
try {
player.play();
} catch (exception.CannotDiscardException e) {
// If there are no valid targets to discard from, still consider the card played
player.deselectAll();
ErrorDialog.show("No other players have cards to discard. Card has been played.");
}
return;
}
// If no marbles selected for other cards, use normal behavior
if (player.getSelectedMarbles().isEmpty()) {
player.play();
return;
}
Marble marble = player.getSelectedMarbles().get(0);
// Only apply entry point logic for cards with numeric values
boolean isNumericCard = card.getName().matches("\\d+|Ten|Jack|Queen|King|Ace");
// Special handling for marble at entry cell trying to move into safe zone
if (isNumericCard && isAtEntryCellToSafeZone(marble)) {
// Check if rank is too high (> 4)
if (isSafeZoneEntryRankTooHigh(marble, card)) {
player.deselectAll();
ErrorDialog.show("Rank is too high! Maximum safe zone entry is 4 spaces.");
return;
}
// Check if there's a marble blocking the path
if (isMarbleBlockingSafeZoneEntry(marble, card)) {
player.deselectAll();
ErrorDialog.show("Cannot bypass my Safe Zone marbles!");
return;
}
// Move the marble into the safe zone
handleSafeZoneEntry(player, marble, card);
return;
}
// Special handling for King card to fix destruction bug
if (isKingCard(card) && player.getSelectedMarbles().size() == 1) {
handleKingCard(player, marble);
return;
}
// Special handling for cases where a marble would skip its entry
if (isNumericCard && wouldSkipEntryPoint(player, marble, card)) {
ArrayList<Marble> selectedMarbles = player.getSelectedMarbles();
player.deselectAll();
// Notify the user
ErrorDialog.show("Moving marble to its entry point.");
// Move marble exactly to entry position
moveToEntry(marble);
return;
}
// Default behavior for all other cases
player.play();
}
/**
* Check if a marble is at its entry cell ready to enter safe zone
*/
private boolean isAtEntryCellToSafeZone(Marble marble) {
int marblePosition = getMarblePosition(marble);
int entryPosition = getEntryPosition(marble.getColour());
// Check that:
// 1. The marble is at the entry position
// 2. The marble is the active player's marble
// 3. The cell at this position is an actual ENTRY cell type
return marblePosition == entryPosition &&
marble.getColour() == game.getActivePlayerColour() &&
game.getBoard().getTrack().get(marblePosition).getCellType() == CellType.ENTRY;
}
/**
* Handle moving a marble from the entry cell into the safe zone
*/
private void handleSafeZoneEntry(Player player, Marble marble, Card card) throws GameException {
// Get rank of card
int rank = getRank(card);
if (rank <= 0) {
// Non-numeric card, use default behavior
player.play();
return;
}
// Get marble position
int currentPos = getMarblePosition(marble);
// Get safe zone for this color
ArrayList<Cell> safeZone = null;
for (var sz : game.getBoard().getSafeZones()) {
if (sz.getColour() == marble.getColour()) {
safeZone = sz.getCells();
break;
}
}
if (safeZone == null || currentPos == -1) {
player.play();
return;
}
// Remove marble from track
game.getBoard().getTrack().get(currentPos).setMarble(null);
// Place in safe zone at the appropriate distance
int safePosition = Math.min(rank - 1, safeZone.size() - 1);
safeZone.get(safePosition).setMarble(marble);
// Deselect card and marble
player.deselectAll();
}
/**
* Special handling for King card to fix the destruction bug
*/
private void handleKingCard(Player player, Marble marble) throws GameException {
// Get current position
int currentPos = getMarblePosition(marble);
if (currentPos == -1) {
// If marble is not on track, use regular play (field a new marble)
player.play();
return;
}
// Calculate target position (13 spaces forward)
int targetPos = (currentPos + 13) % 100;
// Find marbles in the path to destroy them
ArrayList<Marble> marblesToDestroy = new ArrayList<>();
int pos = currentPos;
while (pos != targetPos) {
pos = (pos + 1) % 100;
Cell cell = game.getBoard().getTrack().get(pos);
Marble m = cell.getMarble();
if (m != null && !m.equals(marble) && m.getColour() != player.getColour()) {
marblesToDestroy.add(m);
}
}
// Remove the source marble
game.getBoard().getTrack().get(currentPos).setMarble(null);
// Destroy any marbles in the way that are not the player's own marbles
for (Marble m : marblesToDestroy) {
int mPos = getMarblePosition(m);
if (mPos != -1) {
game.getBoard().getTrack().get(mPos).setMarble(null);
for (Player p : game.getPlayers()) {
if (p.getColour() == m.getColour()) {
p.regainMarble(m);
break;
}
}
}
}
// Check if target is a trap
Cell targetCell = game.getBoard().getTrack().get(targetPos);
if (targetCell.isTrap()) {
// Handle trap
targetCell.setTrap(false);
player.regainMarble(marble);
// Reassign trap
boolean assigned = false;
while (!assigned) {
int randIndex = (int)(Math.random() * 100);
Cell cell = game.getBoard().getTrack().get(randIndex);
if (cell.getCellType() == CellType.NORMAL && !cell.isTrap() && cell.getMarble() == null) {
cell.setTrap(true);
assigned = true;
}
}
} else {
// Place marble at target
targetCell.setMarble(marble);
}
// Deselect card and marbles
player.deselectAll();
}
/**
* Move a marble to its entry position
*/
private void moveToEntry(Marble marble) {
// Get current position and entry position
int currentPos = getMarblePosition(marble);
int entryPos = getEntryPosition(marble.getColour());
if (currentPos == -1 || entryPos == -1) {
return;
}
// Remove from current position
game.getBoard().getTrack().get(currentPos).setMarble(null);
// Place at entry position
game.getBoard().getTrack().get(entryPos).setMarble(marble);
}
/**
* Gets the rank of a card
*/
private int getRank(Card card) {
String name = card.getName().toLowerCase();
if (name.equals("ace")) return 1;
if (name.equals("jack")) return 11;
if (name.equals("queen")) return 12;
if (name.equals("king")) return 13;
try {
return Integer.parseInt(name);
} catch (NumberFormatException e) {
return 0;
}
}
/**
* Calculate the distance from position to entry point
*/
private int calculateDistance(int fromPosition, int toPosition) {
int clockwise = (toPosition - fromPosition + 100) % 100;
return clockwise;
}
/**
* Get a marble's position on the track
*/
private int getMarblePosition(Marble marble) {
ArrayList<Cell> track = game.getBoard().getTrack();
for (int i = 0; i < track.size(); i++) {
if (track.get(i).getMarble() == marble) {
return i;
}
}
return -1;
}
/**
* Get the entry position for a color
*/
private int getEntryPosition(Colour colour) {
// Use the same logic as in the Board class
int basePosition = getBasePosition(colour);
if(basePosition == -1)
return -1;
else
return (basePosition - 2 + 100) % 100;
}
/**
* Get the base position for a color
*/
private int getBasePosition(Colour colour) {
for(int i = 0; i < game.getBoard().getSafeZones().size(); i++) {
if(game.getBoard().getSafeZones().get(i).getColour() == colour)
return i * 25;
}
return -1;
}
}