Finishing user touch added also an element so that a part cannot be

moved onto its own color.
This commit is contained in:
yohanmontagne 2025-05-07 16:23:21 +02:00
parent 932741e82c
commit 2b8c9c995c
1 changed files with 22 additions and 9 deletions

View File

@ -136,8 +136,7 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
public void userTouch(int x, int y) {
if (selectedX == null && selectedY == null) {
Piece pieceAtPos = getPieceAt(x, y);
if (pieceAtPos != null) {
@ -156,16 +155,30 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
Piece pieceToMove = getPieceAt(selectedX, selectedY);
if (pieceToMove != null) {
pieces.remove(pieceToMove);
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
turnNumber++;
turnIsWhite = !turnIsWhite;
Piece pieceAtDestination = getPieceAt(x, y);
if (pieceAtDestination == null ||
pieceAtDestination.isWhite() != pieceToMove.isWhite()) {
if (pieceAtDestination != null) {
pieces.remove(pieceAtDestination);
}
pieces.remove(pieceToMove);
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
turnNumber++;
turnIsWhite = !turnIsWhite;
}
}
selectedX = null;
selectedY = null;
}