modify the setPiece method to make it remove any piece that was already

there first
This commit is contained in:
Tikea TE 2025-05-22 22:24:51 +02:00
parent 709e41c360
commit b702e97e27
1 changed files with 9 additions and 0 deletions

View File

@ -33,6 +33,15 @@ public class Board {
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
// 1. remove any piece that was already in this cell
for (int i = 0; i < pieces.size(); i++) {
Piece p = pieces.get(i);
if(p.getX() == x && p.getY() == y) {
pieces.remove(i);
break;
}
}
// 2. add the new piece
pieces.add(new Piece(x,y,type,isWhite)); pieces.add(new Piece(x,y,type,isWhite));
} }