able to move only within the highlighted cells

This commit is contained in:
Tikea TE 2025-05-06 15:42:27 +02:00
parent 3ff691cdac
commit e296422c81
1 changed files with 10 additions and 15 deletions

View File

@ -176,34 +176,29 @@ public class Board {
return;
}
// 4) Otherwise, move the previously selected piece to (x,y):
// a) remove any piece already on the destination (simple capture)
Pieces.removeIf(p -> p.getX() == x && p.getY() == y);
// remove p if p.getX() == x && p.getY() == y
// 4) Otherwise its a move: but only if (x,y) was highlighted
if (!isHighlighted(x, y)) {
// if clicked somewhere illegal, ignore it (keep your piece in hand)
return;
}
// b) locate the selected piece object
// 5) Now perform the capture+relocate exactly as before
Pieces.removeIf(p -> p.getX() == x && p.getY() == y);
Piece toMove = null;
for (int i = 0; i < Pieces.size(); i++) {
Piece p = Pieces.get(i);
for (Piece p : Pieces) {
if (p.getX() == selectedX && p.getY() == selectedY) {
toMove = p;
break;
}
}
// c) re-place it at the new coordinates
if (toMove != null) {
Pieces.remove(toMove);
// we need to remove it from its old place
Pieces.add(new Piece(x, y, toMove.isWhite(), toMove.getType()));
} // change the x y but still the same color and type
}
// 5) Advance half-move counter and switch player
// 6) Advance turn and clear selection/highlights
turnNumber++;
turnWhite = !turnWhite;
// 6) Clear the selection and clear highlight
hasSelection = false;
clearHighlights();
}