72 lines
2.2 KiB
Java
72 lines
2.2 KiB
Java
package backend;
|
|
|
|
public class Enpassant {
|
|
private Board board;
|
|
|
|
public Enpassant(Board board) {
|
|
this.board = board;
|
|
}
|
|
|
|
public boolean isEnPassantValid(int fromX, int fromY, int toX, int toY) {
|
|
Piece movingPiece = board.getPieceAt(fromX, fromY);
|
|
|
|
if (movingPiece == null || movingPiece.getType() != PieceType.Pawn) {
|
|
return false;
|
|
}
|
|
|
|
// Must be a diagonal move
|
|
if (Math.abs(toX - fromX) != 1 || Math.abs(toY - fromY) != 1) {
|
|
return false;
|
|
}
|
|
|
|
if (board.getPieceAt(toX, toY) != null) {
|
|
return false;
|
|
}
|
|
|
|
Piece targetPawn = board.getPieceAt(toX, fromY); // Same rank as moving pawn
|
|
if (targetPawn == null || targetPawn.getType() != PieceType.Pawn) {
|
|
return false;
|
|
}
|
|
|
|
// Target pawn must be opposite color
|
|
if (targetPawn.isWhite() == movingPiece.isWhite()) {
|
|
return false;
|
|
}
|
|
|
|
return board.canCaptureEnPassant(toX, fromY);
|
|
}
|
|
|
|
|
|
public void executeEnPassant(int fromX, int fromY, int toX, int toY) {
|
|
Piece movingPawn = board.getPieceAt(fromX, fromY);
|
|
|
|
board.removePiece(toX, fromY);
|
|
board.removePiece(fromX, fromY);
|
|
board.setPiece(movingPawn.isWhite(), movingPawn.getType(), toX, toY);
|
|
}
|
|
|
|
public Position getCapturedPawnPosition(int toX, int toY) {
|
|
Piece targetPawn = board.getPieceAt(toX, toY - (board.isTurnWhite() ? -1 : 1));
|
|
if (targetPawn != null && targetPawn.getType() == PieceType.Pawn) {
|
|
return new Position(toX, toY - (board.isTurnWhite() ? -1 : 1));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static class Position {
|
|
public int x;
|
|
public int y;
|
|
|
|
public Position(int x, int y) {
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
public boolean equals(Object obj) {
|
|
if (this == obj) return true;
|
|
if (obj == null || getClass() != obj.getClass()) return false;
|
|
Position position = (Position) obj;
|
|
return x == position.x && y == position.y;
|
|
}
|
|
}
|
|
} |