attempt en passant

This commit is contained in:
leahb 2025-05-13 16:27:06 +02:00
parent 2036a3d034
commit 315c8477b0
3 changed files with 78 additions and 2 deletions

View File

@ -102,6 +102,42 @@ public class Board {
} }
} }
/*public void movePiece(int fromX, int fromY, int toX, int toY) {
Piece piece = board.getPieceAt(fromX, fromY);
if (piece == null) return;
// --- En Passant ---
SpecialMoves special = new SpecialMoves(piece, board);
boolean isEnPassant = special.isEnpassant(fromX, fromY, toX, toY);
// --- Move the piece
board.setPieceAt(toX, toY, piece);
board.setPieceAt(fromX, fromY, null);
piece.setX(toX);
piece.setY(toY);
// --- Reset all pawns' en passant eligibility
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Piece p = board.getPieceAt(i, j);
if (p != null && p.getType() == PieceType.Pawn) {
p.setEnPassantEligible(false);
}
}
}
// --- If current pawn moved 2 steps forward, set it eligible for en passant
if (piece.getType() == PieceType.Pawn && Math.abs(toY - fromY) == 2) {
piece.setEnPassantEligible(true);
}
// --- En Passant message
if (isEnPassant) {
System.out.println("En Passant captured successfully!");
}
}*/
public void cleanBoard() { public void cleanBoard() {
pieces.clear(); pieces.clear();
} }

View File

@ -6,7 +6,16 @@ public class Piece {
private boolean isWhite; private boolean isWhite;
private PieceType type; private PieceType type;
/*private boolean enPassantEligible = false;
public boolean isEnPassantEligible() {
return enPassantEligible;
}
public void setEnPassantEligible(boolean eligible) {
this.enPassantEligible = eligible;
}
*/
public Piece(int x, int y, boolean isWhite, PieceType type) { public Piece(int x, int y, boolean isWhite, PieceType type) {
this.x=x; this.x=x;
this.y=y; this.y=y;

View File

@ -57,4 +57,35 @@ public class SpecialMoves {
} }
} }
}*/ }
package backend;
public class SpecialMoves {
private Piece piece;
private Board board;
public SpecialMoves(Piece piece, Board board) {
this.piece = piece;
this.board = board;
}
// Call this when trying to move a pawn to check for en passant
public boolean isEnpassant(int fromX, int fromY, int toX, int toY) {
if (piece.getType() != PieceType.Pawn) return false;
boolean isWhite = piece.isWhite();
// En passant capture happens diagonally
int dx = toX - fromX;
int dy = toY - fromY;
if (Math.abs(dx) != 1 || (isWhite && dy != -1) || (!isWhite && dy != 1)) {
return false;
}
// Check the adjacent piece
Piece sidePawn = board.getPieceAt(toX, fromY);
if (sidePawn == null || sidePawn.getType() != PieceType.Pawn || sidePawn.isWhite() == isWhite) {
*/