diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index e47e848..50c0e3f 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -18,22 +18,26 @@ public class AutoPlayer { int toY = pos[1]; Piece captured = board.getPieceAt(toX, toY); Move move = new Move(p, p.getX(), p.getY(), toX, toY, captured); + if (p.getType() == PieceType.King && Math.abs(toX - p.getX()) == 2) { + move.setCastling(true); + } + possibleMoves.add(move); } } } - // Si aucune possibilit => null + // Si aucune possibilit� => null if (possibleMoves.isEmpty()) return null; - // Cherche un coup qui capture une pice + // Cherche un coup qui capture une pi�ce for (Move m : possibleMoves) { if (m.getCapturedPiece() != null) { return m; } } - // Sinon, retourne un coup alatoire + // Sinon, retourne un coup al�atoire return possibleMoves.get(rand.nextInt(possibleMoves.size())); } } diff --git a/src/backend/Board.java b/src/backend/Board.java index e54069d..8c4d4ba 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -136,6 +136,9 @@ public class Board { if (valid) { Piece destPiece = getPieceAt(x, y); Move move = new Move(selectedPiece, selectedX, selectedY, x, y, destPiece); + if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2) { + move.setCastling(true); + } // 🔍 En Passant Detection if (selectedPiece.getType() == PieceType.Pawn && destPiece == null) { @@ -294,7 +297,7 @@ public class Board { public void playMove(Move move) { if (move == null) return; - + if (move.isEnPassant()) { int direction = move.getMovedPiece().isWhite() ? 1 : -1; Piece capturedPawn = getPieceAt(move.getToX(), move.getToY() + direction); @@ -306,22 +309,40 @@ public class Board { Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY()); if (pieceToMove == null) return; - // Supprimer la pi�ce d'origine + // Supprimer la pièce d'origine pieces.remove(pieceToMove); - // Supprimer la pi�ce captur�e (si elle existe) + // ======= Gestion du castling ======= + if (move.isCastling()) { + int direction = (move.getToX() > move.getFromX()) ? 1 : -1; // +1 petit roque, -1 grand roque + int rookStartX = (direction == 1) ? 7 : 0; + int rookEndX = move.getToX() - direction; + int y = move.getFromY(); + + // Trouver et retirer la tour + Piece rook = getPieceAt(rookStartX, y); + if (rook != null && rook.getType() == PieceType.Rook) { + pieces.remove(rook); + Piece newRook = new Piece(PieceType.Rook, rook.isWhite(), rookEndX, y); + newRook.setHasMoved(true); + pieces.add(newRook); + } + } + + // Supprimer la pièce capturée (si elle existe) Piece captured = getPieceAt(move.getToX(), move.getToY()); if (captured != null) { pieces.remove(captured); } - // Ajouter la nouvelle position + // Ajouter la nouvelle position du roi (ou autre pièce) Piece newPiece = new Piece( pieceToMove.getType(), pieceToMove.isWhite(), move.getToX(), move.getToY() ); + newPiece.setHasMoved(true); // ← AJOUT IMPORTANT pieces.add(newPiece); // Enregistrer dans l'historique pour undo @@ -330,15 +351,14 @@ public class Board { // Fin de tour turnNumber++; - // D�s�lection et surlignage off + // Désélection et surlignage off selectedX = -1; selectedY = -1; highlightedSquares.clear(); - //Mise � jour du statut d��chec apr�s le coup + // Mise à jour du statut d’échec après le coup isWhiteInCheck = isInCheck(isTurnWhite()); isCheck = isWhiteInCheck; - } diff --git a/src/backend/Move.java b/src/backend/Move.java index e7134b3..7b2515b 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -8,6 +8,16 @@ public class Move { private int toX, toY; private Piece capturedPiece; private boolean enPassant; + private boolean isCastling = false; + + public boolean isCastling() { + return isCastling; + } + + public void setCastling(boolean castling) { + isCastling = castling; + } + public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece) { this.movedPiece = movedPiece; diff --git a/src/backend/Piece.java b/src/backend/Piece.java index abf4213..ef94f50 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -39,6 +39,8 @@ public class Piece { public boolean isWhite() { return isWhite; } + + public boolean hasMoved() { return hasMoved; } diff --git a/src/windowInterface/JPanelChessBoard.java b/src/windowInterface/JPanelChessBoard.java index 63c4581..6832bf1 100644 --- a/src/windowInterface/JPanelChessBoard.java +++ b/src/windowInterface/JPanelChessBoard.java @@ -111,13 +111,13 @@ public class JPanelChessBoard extends JPanel { for (int x = 0; x < myGame.getWidth(); x++) { for (int y = 0; y < myGame.getHeight(); y++) { - Piece p = getPieceAt(x, y); //dfinir p AVANT toute condition + Piece p = getPieceAt(x, y); //d�finir p AVANT toute condition - // Damier invers + // Damier invers� if ((x + y) % 2 == 0) { - g.setColor(new Color(1, 1, 1)); // clair + g.setColor(new Color(255, 255, 255)); // clair } else { - g.setColor(new Color(240, 240, 240)); // fonc + g.setColor(new Color(50, 25, 0)); // fonc� } // Surlignage rouge en cas de mat @@ -125,7 +125,7 @@ public class JPanelChessBoard extends JPanel { g.setColor(Color.RED); } - // Slection + // S�lection if (myGame.isSelected(x, y)) { g.setColor(Color.ORANGE); }