"j'ai essayé un autre castling"
This commit is contained in:
parent
d818b7d313
commit
912fd07326
|
|
@ -18,22 +18,26 @@ public class AutoPlayer {
|
||||||
int toY = pos[1];
|
int toY = pos[1];
|
||||||
Piece captured = board.getPieceAt(toX, toY);
|
Piece captured = board.getPieceAt(toX, toY);
|
||||||
Move move = new Move(p, p.getX(), p.getY(), toX, toY, captured);
|
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);
|
possibleMoves.add(move);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si aucune possibilité => null
|
// Si aucune possibilit<EFBFBD> => null
|
||||||
if (possibleMoves.isEmpty()) return 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) {
|
for (Move m : possibleMoves) {
|
||||||
if (m.getCapturedPiece() != null) {
|
if (m.getCapturedPiece() != null) {
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sinon, retourne un coup aléatoire
|
// Sinon, retourne un coup al<EFBFBD>atoire
|
||||||
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
|
return possibleMoves.get(rand.nextInt(possibleMoves.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,9 @@ public class Board {
|
||||||
if (valid) {
|
if (valid) {
|
||||||
Piece destPiece = getPieceAt(x, y);
|
Piece destPiece = getPieceAt(x, y);
|
||||||
Move move = new Move(selectedPiece, selectedX, selectedY, x, y, destPiece);
|
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
|
// 🔍 En Passant Detection
|
||||||
if (selectedPiece.getType() == PieceType.Pawn && destPiece == null) {
|
if (selectedPiece.getType() == PieceType.Pawn && destPiece == null) {
|
||||||
|
|
@ -294,7 +297,7 @@ public class Board {
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
if (move == null) return;
|
if (move == null) return;
|
||||||
|
|
||||||
if (move.isEnPassant()) {
|
if (move.isEnPassant()) {
|
||||||
int direction = move.getMovedPiece().isWhite() ? 1 : -1;
|
int direction = move.getMovedPiece().isWhite() ? 1 : -1;
|
||||||
Piece capturedPawn = getPieceAt(move.getToX(), move.getToY() + direction);
|
Piece capturedPawn = getPieceAt(move.getToX(), move.getToY() + direction);
|
||||||
|
|
@ -306,22 +309,40 @@ public class Board {
|
||||||
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
|
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
|
||||||
if (pieceToMove == null) return;
|
if (pieceToMove == null) return;
|
||||||
|
|
||||||
// Supprimer la pi<EFBFBD>ce d'origine
|
// Supprimer la pièce d'origine
|
||||||
pieces.remove(pieceToMove);
|
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());
|
Piece captured = getPieceAt(move.getToX(), move.getToY());
|
||||||
if (captured != null) {
|
if (captured != null) {
|
||||||
pieces.remove(captured);
|
pieces.remove(captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajouter la nouvelle position
|
// Ajouter la nouvelle position du roi (ou autre pièce)
|
||||||
Piece newPiece = new Piece(
|
Piece newPiece = new Piece(
|
||||||
pieceToMove.getType(),
|
pieceToMove.getType(),
|
||||||
pieceToMove.isWhite(),
|
pieceToMove.isWhite(),
|
||||||
move.getToX(),
|
move.getToX(),
|
||||||
move.getToY()
|
move.getToY()
|
||||||
);
|
);
|
||||||
|
newPiece.setHasMoved(true); // ← AJOUT IMPORTANT
|
||||||
pieces.add(newPiece);
|
pieces.add(newPiece);
|
||||||
|
|
||||||
// Enregistrer dans l'historique pour undo
|
// Enregistrer dans l'historique pour undo
|
||||||
|
|
@ -330,15 +351,14 @@ public class Board {
|
||||||
// Fin de tour
|
// Fin de tour
|
||||||
turnNumber++;
|
turnNumber++;
|
||||||
|
|
||||||
// D<EFBFBD>s<EFBFBD>lection et surlignage off
|
// Désélection et surlignage off
|
||||||
selectedX = -1;
|
selectedX = -1;
|
||||||
selectedY = -1;
|
selectedY = -1;
|
||||||
highlightedSquares.clear();
|
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());
|
isWhiteInCheck = isInCheck(isTurnWhite());
|
||||||
isCheck = isWhiteInCheck;
|
isCheck = isWhiteInCheck;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,16 @@ public class Move {
|
||||||
private int toX, toY;
|
private int toX, toY;
|
||||||
private Piece capturedPiece;
|
private Piece capturedPiece;
|
||||||
private boolean enPassant;
|
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) {
|
public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece) {
|
||||||
this.movedPiece = movedPiece;
|
this.movedPiece = movedPiece;
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,8 @@ public class Piece {
|
||||||
public boolean isWhite() {
|
public boolean isWhite() {
|
||||||
return isWhite;
|
return isWhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean hasMoved() {
|
public boolean hasMoved() {
|
||||||
return hasMoved;
|
return hasMoved;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -111,13 +111,13 @@ public class JPanelChessBoard extends JPanel {
|
||||||
for (int x = 0; x < myGame.getWidth(); x++) {
|
for (int x = 0; x < myGame.getWidth(); x++) {
|
||||||
for (int y = 0; y < myGame.getHeight(); y++) {
|
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) {
|
if ((x + y) % 2 == 0) {
|
||||||
g.setColor(new Color(1, 1, 1)); // clair
|
g.setColor(new Color(255, 255, 255)); // clair
|
||||||
} else {
|
} else {
|
||||||
g.setColor(new Color(240, 240, 240)); // foncé
|
g.setColor(new Color(50, 25, 0)); // fonc<EFBFBD>
|
||||||
}
|
}
|
||||||
|
|
||||||
// Surlignage rouge en cas de mat
|
// Surlignage rouge en cas de mat
|
||||||
|
|
@ -125,7 +125,7 @@ public class JPanelChessBoard extends JPanel {
|
||||||
g.setColor(Color.RED);
|
g.setColor(Color.RED);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sélection
|
// S<EFBFBD>lection
|
||||||
if (myGame.isSelected(x, y)) {
|
if (myGame.isSelected(x, y)) {
|
||||||
g.setColor(Color.ORANGE);
|
g.setColor(Color.ORANGE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue