Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
912fd07326 |
|
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,8 +62,8 @@ public class Board {
|
|||
setPiece(false, PieceType.Rook, 0, 0);
|
||||
setPiece(false, PieceType.Knight, 1, 0);
|
||||
setPiece(false, PieceType.Bishop, 2, 0);
|
||||
setPiece(false, PieceType.Queen, 3, 0);
|
||||
setPiece(false, PieceType.King, 4, 0);
|
||||
setPiece(false, PieceType.Queen, 4, 0);
|
||||
setPiece(false, PieceType.King, 3, 0);
|
||||
setPiece(false, PieceType.Bishop, 5, 0);
|
||||
setPiece(false, PieceType.Knight, 6, 0);
|
||||
setPiece(false, PieceType.Rook, 7, 0);
|
||||
|
|
@ -72,8 +72,8 @@ public class Board {
|
|||
setPiece(true, PieceType.Rook, 0, 7);
|
||||
setPiece(true, PieceType.Knight, 1, 7);
|
||||
setPiece(true, PieceType.Bishop, 2, 7);
|
||||
setPiece(true, PieceType.Queen, 3, 7);
|
||||
setPiece(true, PieceType.King, 4, 7);
|
||||
setPiece(true, PieceType.Queen, 4, 7);
|
||||
setPiece(true, PieceType.King, 3, 7);
|
||||
setPiece(true, PieceType.Bishop, 5, 7);
|
||||
setPiece(true, PieceType.Knight, 6, 7);
|
||||
setPiece(true, PieceType.Rook, 7, 7);
|
||||
|
|
@ -87,7 +87,7 @@ public class Board {
|
|||
StringBuilder sb = new StringBuilder();
|
||||
for (Piece p : pieces) {
|
||||
sb.append(p.getType()).append(" ")
|
||||
.append(p.isWhite() ? "black" : "white")
|
||||
.append(p.isWhite() ? "white" : "black")
|
||||
.append(" at (").append(p.getX()).append(", ").append(p.getY()).append(")\n");
|
||||
}
|
||||
return sb.toString();
|
||||
|
|
@ -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) {
|
||||
|
|
@ -148,10 +151,7 @@ public class Board {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2 && selectedY == y) {
|
||||
move.setCastling(true);
|
||||
}
|
||||
|
||||
|
||||
// Record move
|
||||
moveHistory.add(move);
|
||||
|
||||
|
|
@ -162,7 +162,9 @@ public class Board {
|
|||
Piece capturedPawn = getPieceAt(x, y + dir);
|
||||
if (capturedPawn != null) {
|
||||
pieces.remove(capturedPawn);
|
||||
}
|
||||
}
|
||||
} else if (destPiece != null) {
|
||||
pieces.remove(destPiece);
|
||||
}
|
||||
pieces.add(new Piece(selectedPiece.getType(), selectedPiece.isWhite(), x, y));
|
||||
|
||||
|
|
@ -296,53 +298,69 @@ public class Board {
|
|||
public void playMove(Move move) {
|
||||
if (move == null) return;
|
||||
|
||||
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
|
||||
if (pieceToMove == null) return;
|
||||
|
||||
// Handle capture or en passant
|
||||
if (move.isEnPassant()) {
|
||||
int dir = pieceToMove.isWhite() ? 1 : -1;
|
||||
Piece captured = getPieceAt(move.getToX(), move.getToY() + dir);
|
||||
if (captured != null) pieces.remove(captured);
|
||||
} else if (move.getCapturedPiece() != null) {
|
||||
pieces.remove(move.getCapturedPiece());
|
||||
}
|
||||
|
||||
// Move the piece instead of removing it
|
||||
pieceToMove.setX(move.getToX());
|
||||
pieceToMove.setY(move.getToY());
|
||||
pieceToMove.setHasMoved(true);
|
||||
|
||||
// ♔ Handle castling
|
||||
if (move.isCastling()) {
|
||||
int y = move.getToY();
|
||||
boolean kingside = move.getToX() > move.getFromX();
|
||||
int rookFromX = kingside ? 7 : 0;
|
||||
int rookToX = kingside ? move.getToX() - 1 : move.getToX() + 1;
|
||||
|
||||
Piece rook = getPieceAt(rookFromX, y);
|
||||
if (rook != null && rook.getType() == PieceType.Rook) {
|
||||
rook.setX(rookToX);
|
||||
rook.setY(y);
|
||||
rook.setHasMoved(true);
|
||||
int direction = move.getMovedPiece().isWhite() ? 1 : -1;
|
||||
Piece capturedPawn = getPieceAt(move.getToX(), move.getToY() + direction);
|
||||
if (capturedPawn != null) {
|
||||
pieces.remove(capturedPawn);
|
||||
}
|
||||
}
|
||||
|
||||
// Save move to history
|
||||
Piece pieceToMove = getPieceAt(move.getFromX(), move.getFromY());
|
||||
if (pieceToMove == null) return;
|
||||
|
||||
// Supprimer la pièce d'origine
|
||||
pieces.remove(pieceToMove);
|
||||
|
||||
// ======= 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 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
|
||||
moveHistory.add(move);
|
||||
|
||||
// Fin de tour
|
||||
turnNumber++;
|
||||
|
||||
// Deselect anything
|
||||
// Désélection et surlignage off
|
||||
selectedX = -1;
|
||||
selectedY = -1;
|
||||
highlightedSquares.clear();
|
||||
|
||||
// Update check status
|
||||
// Mise à jour du statut d’échec après le coup
|
||||
isWhiteInCheck = isInCheck(isTurnWhite());
|
||||
isCheck = isWhiteInCheck;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Piece getPieceAt(int x, int y) {
|
||||
for (Piece p : pieces) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,16 @@ public class Move {
|
|||
private int toX, toY;
|
||||
private Piece capturedPiece;
|
||||
private boolean enPassant;
|
||||
private boolean castling;
|
||||
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;
|
||||
|
|
@ -23,18 +32,9 @@ public class Move {
|
|||
public boolean isEnPassant() {
|
||||
return enPassant;
|
||||
}
|
||||
|
||||
public void setEnPassant(boolean enPassant) {
|
||||
this.enPassant = enPassant;
|
||||
}
|
||||
|
||||
public boolean isCastling() {
|
||||
return castling;
|
||||
}
|
||||
|
||||
public void setCastling(boolean castling) {
|
||||
this.castling = castling;
|
||||
}
|
||||
|
||||
public Piece getMovedPiece() { return movedPiece; }
|
||||
public int getFromX() { return fromX; }
|
||||
|
|
|
|||
|
|
@ -32,14 +32,6 @@ public class Piece {
|
|||
return y;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public PieceType getType() {
|
||||
return type;
|
||||
}
|
||||
|
|
@ -47,6 +39,8 @@ public class Piece {
|
|||
public boolean isWhite() {
|
||||
return isWhite;
|
||||
}
|
||||
|
||||
|
||||
public boolean hasMoved() {
|
||||
return hasMoved;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,9 +115,9 @@ public class JPanelChessBoard extends JPanel {
|
|||
|
||||
// Damier invers<EFBFBD>
|
||||
if ((x + y) % 2 == 0) {
|
||||
g.setColor(new Color(255, 250, 255)); // clair
|
||||
g.setColor(new Color(255, 255, 255)); // clair
|
||||
} else {
|
||||
g.setColor(new Color(50, 25, 1)); // fonc<EFBFBD>
|
||||
g.setColor(new Color(50, 25, 0)); // fonc<EFBFBD>
|
||||
}
|
||||
|
||||
// Surlignage rouge en cas de mat
|
||||
|
|
|
|||
Loading…
Reference in New Issue