working on movePiece to remove pieces, might want to create another

method
This commit is contained in:
hugomanipoud2 2025-05-19 16:39:49 +02:00
parent be812038d3
commit 63518d092e
1 changed files with 22 additions and 3 deletions

View File

@ -138,13 +138,32 @@ public class Board {
}
}
}
//helper function to change the coordinates of a piece
private void movePiece(int selectX, int selectY, int toX, int toY) {
Piece movingPiece = null ; // initializing variable to work with and store it
for (Piece piece : pieces) { // iterate trough all pieces
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
piece.setX(toX); // change coordinates to the new coordinate
piece.setY(toY);
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
for(Piece pieceInTheWay : pieces) { //loop to check if a piece is in the way
if(pieceInTheWay.getX() == toX && pieceInTheWay.getY() == toY) { //checking if a piece is at the arrival coordinates
pieces.remove(pieceInTheWay); //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;
}
}
}
}
}