added a break statement to needpieceDeletion and everything works !

This commit is contained in:
hugomanipoud2 2025-05-19 17:19:10 +02:00
parent 8f5a17fc1a
commit dbe928ce78
1 changed files with 10 additions and 14 deletions

View File

@ -120,6 +120,7 @@ public class Board {
}
public void userTouch(int x, int y) {
if(selectX == -1 && selectY == -1) { // if initial position for select x and y
if(setSelectXY(x,y) == true) { // checks for pieces at the position x and y
selectX = x; //set select x to x
@ -151,30 +152,25 @@ public class Board {
if(piece.getX() == selectX && piece.getY() == selectY) { // if the coordinates selected and the coordinate of a piece on the board matches, the loop will activate
movingPiece = piece; //storing in memory the piece
if (setSelectXY(toX, toY)){//checking if a piece is at the arrival coordinates
if (setSelectXY(toX, toY)){//checking if a piece is at the arrival coordinates
needPieceDeletion(toX, toY); //if a piece is at arrival coord, remove the piece
movingPiece.setX(toX); // change coordinates to the new coordinate
movingPiece.setY(toY);
break;
}else { //if not, just move piece to new coord
movingPiece.setX(toX); // change coordinates to the new coordinate
movingPiece.setY(toY);
break;
}
movingPiece.setX(toX); // change coordinates to the new coordinate
movingPiece.setY(toY);
break;
}
}
}
}
private void needPieceDeletion(int x, int y) { //almost a copy of setSelectXY but removes the piece instead of returning a bool
for (Piece piece : pieces) { // iterate trough all pieces
for (Piece piece : pieces) { // iterate trough all pieces searching for the one with the specific wanted coordinates
if (piece.getX() == x && piece.getY() == y) {
pieces.remove(piece); //removing piece according to if statement
pieces.remove(piece);//removing piece according to if statement
break; // stop the loop
}
}
}