correct errors

This commit is contained in:
MSI-AB\antoineB 2025-05-16 09:06:32 +02:00
parent f75359ffb6
commit 2ab662f394
1 changed files with 57 additions and 15 deletions

View File

@ -13,6 +13,7 @@ public class Board {
private Integer selectedY = null;
private ArrayList<int[]> highlightedSquares = new ArrayList<>();
private ArrayList<Move> moveHistory = new ArrayList<>();
private int[] enPassantTarget = null;
/*public Board(int colNum, int lineNum) {
@ -203,8 +204,38 @@ public class Board {
selectedX = null;
selectedY = null;
highlightedSquares.clear();
// Check for en passant capture BEFORE updating enPassantTarget!
if (selectedPiece.getType() == PieceType.Pawn &&
enPassantTarget != null &&
x == enPassantTarget[0] &&
y == enPassantTarget[1]) {
int capturedY = selectedPiece.isWhite() ? y - 1 : y + 1;
Piece capturedPawn = getPieceAt(x, capturedY);
// Validate that it's an enemy pawn that just moved two steps
if (capturedPawn != null &&
capturedPawn.getType() == PieceType.Pawn &&
capturedPawn.isWhite() != selectedPiece.isWhite()) {
// Remove the pawn captured en passant
pieces.remove(capturedPawn);
}
}
// Update enPassantTarget
if (selectedPiece.getType() == PieceType.Pawn && selectedY != null && Math.abs(y - selectedY) == 2) {
int middleY = (y + selectedY) / 2;
enPassantTarget = new int[]{x, middleY};
} else {
enPassantTarget = null;
}
}
public boolean isSelected(int x, int y) {
return selectedX != null && selectedY != null && selectedX == x && selectedY == y;
}
@ -416,19 +447,18 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
PieceType type = piece.getType();
if (type == PieceType.Pawn) {
int dir = piece.isWhite() ? -1 : 1; // reverse the -1 and 1 to match the board
int dir = piece.isWhite() ? -1 : 1;
int nextY = y + dir;
boolean isPromoting =(piece.isWhite() && nextY == 7)||(!piece.isWhite() && nextY==0);
// Move forward if square is empty
if (isEmpty(x, nextY)) {
moves.add(new int[]{x, nextY});
// Check for first move double step
int startRow = piece.isWhite()? 6:1; // same here i reversed 6 and 1
// First move double step
int startRow = piece.isWhite() ? 6 : 1;
int twoStepsY = y + 2 * dir;
if (y== startRow && isEmpty(x,twoStepsY)) {
moves.add(new int[] {x, twoStepsY});
if (y == startRow && isEmpty(x, twoStepsY)) {
moves.add(new int[]{x, twoStepsY});
}
}
@ -439,15 +469,23 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
if (isEnemy(x + 1, nextY, piece.isWhite())) {
moves.add(new int[]{x + 1, nextY});
}
//promotion à revoir ca marche pas
if (isPromoting) {
piece.setType(PieceType.Queen);
piece.setY(nextY);
}
// en passant
// En passant
if (enPassantTarget != null) {
int targetX = enPassantTarget[0];
int targetY = enPassantTarget[1];
if (nextY == targetY && Math.abs(x - targetX) == 1) {
moves.add(new int[]{targetX, targetY});
}
}
// Promotion check (you shouldn't promote here only on move execution)
// Do NOT change piece type inside this method
}
if (type == PieceType.Rook) {
// Directions : haut, bas, gauche, droite
@ -538,9 +576,13 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
}
}
}
}
return moves;
}
}