Compare commits

...

1 Commits

Author SHA1 Message Date
apoll d143f7464a roque 2025-04-19 12:21:20 +02:00
4 changed files with 96 additions and 30 deletions

View File

@ -209,7 +209,7 @@ public class Board {
}
else {
if (isHighlighted(x, y)) {
Piece capturedPiece = clickedPiece; // Peut être null pour en passant
Piece capturedPiece = clickedPiece; // Peut être null pour en passant
Move move = new Move(selectedX, selectedY, x, y, selectedPiece, capturedPiece);
playMove(move);
@ -242,7 +242,7 @@ public class Board {
if (pieceToMove == null || pieceToMove.getType() != move.getPieceMoved().getType() || pieceToMove.isWhite() != move.getPieceMoved().isWhite()) {
System.err.println("playMove Error: Mismatch between move data and piece found at source square (" + move.getFromX() + "," + move.getFromY() + "). Aborting move.");
return;
return;
}
boolean isEnPassantCapture = false;
@ -270,7 +270,7 @@ public class Board {
}
}
if (!removed) {
System.err.println("playMove Warning: Could not remove captured piece " + capturedPiece.getType() + " at (" + move.getToX() + "," + move.getToY() + ")");
System.err.println("playMove Warning: Could not remove captured piece " + capturedPiece.getType() + " at (" + move.getToX() + "," + move.getToY() + ")");
}
}
@ -282,6 +282,11 @@ public class Board {
System.out.println("En Passant target set at (" + this.enPassantTargetX + "," + this.enPassantTargetY + ")");
}
// Gérer le roque
if (pieceToMove.getType() == PieceType.King && Math.abs(move.getToX() - move.getFromX()) == 2) {
handleCastling(move);
}
moveHistory.addMove(move);
this.turnNumber++;
@ -291,6 +296,27 @@ public class Board {
System.out.println("Turn " + this.turnNumber + ". " + (this.isTurnWhite ? "White" : "Black") + " to move.");
}
private void handleCastling(Move move) {
Piece king = move.getPieceMoved();
int fromX = move.getFromX();
int toX = move.getToX();
int y = move.getFromY();
if (toX == fromX + 2) {
// Roque côté roi
Piece rook = getPieceAt(fromX + 3, y);
if (rook != null && rook.getType() == PieceType.Rook) {
rook.setPosition(fromX + 1, y);
}
} else if (toX == fromX - 2) {
// Roque côté dame
Piece rook = getPieceAt(fromX - 4, y);
if (rook != null && rook.getType() == PieceType.Rook) {
rook.setPosition(fromX - 1, y);
}
}
}
public boolean undoLastMove() {
return moveHistory.undoLastMove();
}
@ -332,4 +358,4 @@ public class Board {
// TODO: Add saving of enPassantTargetX/Y
return null;
}
}
}

View File

@ -129,17 +129,16 @@ public class Move {
}
}
// *** D<>but Modification En Passant ***
// V<>rifier la capture en passant
// Vérifier la capture en passant
int enPassantTargetX = board.getEnPassantTargetX();
int enPassantTargetY = board.getEnPassantTargetY();
if (enPassantTargetX != -1 && enPassantTargetY != -1) { // Si une cible EP existe
// V<>rifier si la cible est sur la case diagonale avant
if (enPassantTargetY == oneStepY) { // La cible doit <20>tre sur la rang<6E>e devant le pion
if (Math.abs(x - enPassantTargetX) == 1) { // La cible doit <20>tre sur une colonne adjacente
// V<>rifier qu'il y a bien un pion adverse <20> capturer <20> c<>t<EFBFBD> de nous (sur la case EP virtuelle)
// Le pion <20> capturer est sur (enPassantTargetX, y)
// Vérifier si la cible est sur la case diagonale avant
if (enPassantTargetY == oneStepY) { // La cible doit être sur la rangée devant le pion
if (Math.abs(x - enPassantTargetX) == 1) { // La cible doit être sur une colonne adjacente
// Vérifier qu'il y a bien un pion adverse à capturer à côté de nous (sur la case EP virtuelle)
// Le pion à capturer est sur (enPassantTargetX, y)
Piece adjacentPawn = getPieceAt(board, enPassantTargetX, y);
if (adjacentPawn != null && adjacentPawn.getType() == PieceType.Pawn && adjacentPawn.isWhite() != piece.isWhite()) {
addMove(enPassantTargetX, enPassantTargetY, validMoves);
@ -148,10 +147,8 @@ public class Move {
}
}
}
// *** Fin Modification En Passant ***
}
private static void calculateKnightMoves(Board board, Piece piece, int x, int y, Set<String> validMoves) {
int[][] knightMoves = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
@ -180,20 +177,57 @@ public class Move {
if (isValidPosition(board, nextX, nextY)) {
Piece target = getPieceAt(board, nextX, nextY);
if (target == null || target.isWhite() != piece.isWhite()) {
addMove(nextX, nextY, validMoves);
addMove(nextX, nextY, validMoves);
}
}
}
}
// TODO: Ajouter calcul du roque ici
// Ajouter calcul du roque ici
calculateCastlingMoves(board, piece, x, y, validMoves);
}
@Override
public String toString() {
String captureStr = (pieceCaptured != null) ? "x" + pieceCaptured.getType().getSummary() : "";
return pieceMoved.getType().getSummary() +
"(" + fromX + "," + fromY + ")" +
"-" + captureStr +
"(" + toX + "," + toY + ")";
}
}
private static void calculateCastlingMoves(Board board, Piece piece, int x, int y, Set<String> validMoves) {
if (piece.getType() != PieceType.King || piece.hasMoved()) {
return;
}
// Roque côté roi
if (canCastleKingside(board, piece, x, y)) {
addMove(x + 2, y, validMoves);
}
// Roque côté dame
if (canCastleQueenside(board, piece, x, y)) {
addMove(x - 2, y, validMoves);
}
}
private static boolean canCastleKingside(Board board, Piece king, int x, int y) {
Piece rook = board.getPieceAt(x + 3, y);
return rook != null && rook.getType() == PieceType.Rook && !rook.hasMoved() &&
board.getPieceAt(x + 1, y) == null && board.getPieceAt(x + 2, y) == null &&
!isKingInCheck(board, king.isWhite());
}
private static boolean canCastleQueenside(Board board, Piece king, int x, int y) {
Piece rook = board.getPieceAt(x - 4, y);
return rook != null && rook.getType() == PieceType.Rook && !rook.hasMoved() &&
board.getPieceAt(x - 1, y) == null && board.getPieceAt(x - 2, y) == null && board.getPieceAt(x - 3, y) == null &&
!isKingInCheck(board, king.isWhite());
}
private static boolean isKingInCheck(Board board, boolean isWhite) {
// Implémentez la logique pour vérifier si le roi est en échec
// Cela nécessite de vérifier tous les mouvements possibles des pièces adverses
return false;
}
@Override
public String toString() {
String captureStr = (pieceCaptured != null) ? "x" + pieceCaptured.getType().getSummary() : "";
return pieceMoved.getType().getSummary() +
"(" + fromX + "," + fromY + ")" +
"-" + captureStr +
"(" + toX + "," + toY + ")";
}
}

View File

@ -45,7 +45,7 @@ public class MoveHistory {
board.getEnPassantTargetY()
);
moveRecords.add(record);
System.out.println("MoveHistory: Added move from (" + move.getFromX() + "," + move.getFromY() +
System.out.println("MoveHistory: Added move from (" + move.getFromX() + "," + move.getFromY() +
") to (" + move.getToX() + "," + move.getToY() + ")");
}
@ -64,7 +64,7 @@ public class MoveHistory {
MoveRecord lastRecord = moveRecords.remove(moveRecords.size() - 1);
Move lastMove = lastRecord.getMove();
System.out.println("MoveHistory: Undoing move from (" + lastMove.getFromX() + "," + lastMove.getFromY() +
System.out.println("MoveHistory: Undoing move from (" + lastMove.getFromX() + "," + lastMove.getFromY() +
") to (" + lastMove.getToX() + "," + lastMove.getToY() + ")");
// Restore the piece to its original position
@ -80,7 +80,7 @@ public class MoveHistory {
boolean wasEnPassantCapture = false;
if (pieceCaptured != null) {
// Check if this was an en passant capture
if (lastMove.getToX() == lastRecord.getEnPassantTargetX() &&
if (lastMove.getToX() == lastRecord.getEnPassantTargetX() &&
lastMove.getToY() == lastRecord.getEnPassantTargetY() &&
lastMove.getPieceCaptured() == null) {
// For en passant, the captured pawn is at (toX, fromY)
@ -120,4 +120,4 @@ public class MoveHistory {
}
return true;
}
}
}

View File

@ -8,6 +8,7 @@ public class Piece {
private int y;
private final PieceType type;
private final boolean isWhite;
private boolean hasMoved;
public Piece(int x, int y, PieceType type, boolean isWhite) {
if (type == null) {
@ -17,14 +18,19 @@ public class Piece {
this.y = y;
this.type = type;
this.isWhite = isWhite;
this.hasMoved = false;
}
public int getX() { return this.x; }
public int getY() { return this.y; }
public PieceType getType() { return this.type; }
public boolean isWhite() { return this.isWhite; }
public boolean hasMoved() { return hasMoved; }
void setPosition(int newX, int newY) {
if (this.x != newX || this.y != newY) {
this.hasMoved = true;
}
this.x = newX;
this.y = newY;
}
@ -82,7 +88,7 @@ public class Piece {
return false;
}
private boolean isPathClear(int fromX, int fromY, int toX, int toY, Board board) {
private boolean isPathClear(int fromX, int fromY, int toX, int toY, Board board) {
int dx = toX - fromX;
int dy = toY - fromY;
int steps = Math.max(Math.abs(dx), Math.abs(dy));
@ -118,4 +124,4 @@ public class Piece {
public int hashCode() {
return Objects.hash(x, y, type, isWhite);
}
}
}