Compare commits

...

1 Commits

Author SHA1 Message Date
gaspa 912fd07326 "j'ai essayé un autre castling" 2025-05-21 21:28:28 +02:00
5 changed files with 51 additions and 15 deletions

View File

@ -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<EFBFBD> => null
if (possibleMoves.isEmpty()) return null;
// Cherche un coup qui capture une pièce
// Cherche un coup qui capture une pi<EFBFBD>ce
for (Move m : possibleMoves) {
if (m.getCapturedPiece() != null) {
return m;
}
}
// Sinon, retourne un coup aléatoire
// Sinon, retourne un coup al<EFBFBD>atoire
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
}
}

View File

@ -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<EFBFBD>ce d'origine
// Supprimer la pièce d'origine
pieces.remove(pieceToMove);
// Supprimer la pi<EFBFBD>ce captur<EFBFBD>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<EFBFBD>s<EFBFBD>lection et surlignage off
// Désélection et surlignage off
selectedX = -1;
selectedY = -1;
highlightedSquares.clear();
//Mise <EFBFBD> jour du statut d<EFBFBD><EFBFBD>chec apr<EFBFBD>s le coup
// Mise à jour du statut déchec après le coup
isWhiteInCheck = isInCheck(isTurnWhite());
isCheck = isWhiteInCheck;
}

View File

@ -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;

View File

@ -39,6 +39,8 @@ public class Piece {
public boolean isWhite() {
return isWhite;
}
public boolean hasMoved() {
return hasMoved;
}

View File

@ -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); //définir p AVANT toute condition
Piece p = getPieceAt(x, y); //d<EFBFBD>finir p AVANT toute condition
// Damier inversé
// Damier invers<EFBFBD>
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<EFBFBD>
}
// Surlignage rouge en cas de mat
@ -125,7 +125,7 @@ public class JPanelChessBoard extends JPanel {
g.setColor(Color.RED);
}
// Sélection
// S<EFBFBD>lection
if (myGame.isSelected(x, y)) {
g.setColor(Color.ORANGE);
}