From 2ab662f39497dbe91a6971900a87da0567ab0f3d Mon Sep 17 00:00:00 2001 From: "MSI-AB\\antoineB" Date: Fri, 16 May 2025 09:06:32 +0200 Subject: [PATCH] correct errors --- src/backend/Board.java | 72 +++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index a55b731..d3f34ed 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -13,6 +13,7 @@ public class Board { private Integer selectedY = null; private ArrayList highlightedSquares = new ArrayList<>(); private ArrayList 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 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 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 + 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}); } - // en passant - } + // 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 computeLegalMoves(Piece piece) { } } } + } - + + return moves; + + } }