en-passant method for white and black pawns

This commit is contained in:
martinbst 2025-05-01 12:23:58 +02:00
parent 22dfad92d7
commit b6d2dc4045
2 changed files with 24 additions and 0 deletions

View File

@ -15,6 +15,7 @@ public class Board {
private int ym;
private int turnNumber;
private boolean turnColor;
private Move lastMove; // new field
private ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
private LinkedList<BoardHistory> boardHistory = new LinkedList<>();
@ -129,6 +130,13 @@ public class Board {
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
// En passant capture
board.get(ym).set(x, null);
}
//check for castling mode:
if (isCastlingMove) {
@ -156,6 +164,7 @@ public class Board {
board.get(this.ym).set(this.xm,null);
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
Piece movedPiece = this.getPiece(x, y);
lastMove = new Move(xm, ym, x, y);
if (movedPiece instanceof Pawn) {
int promotionRow = movedPiece.isWhite() ? 0 : 7;
if (y == promotionRow) {
@ -215,6 +224,9 @@ public class Board {
// System.out.println("a"); // Debug
select = false;
}
possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck
}
public boolean isSelected(int x, int y) {
@ -285,6 +297,13 @@ public class Board {
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
Piece pieceToMove = this.board.get(ym).get(xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
lastMove = new Move(xm, ym, x, y);
if (pieceToMove instanceof Pawn && x != xm && board.get(y).get(x) == null) {
// En passant capture
board.get(ym).set(x, null);
}
//check for castling mode:
if (isCastlingMove) {

View File

@ -31,4 +31,9 @@ public class Move {
public String toString() {
return "(" + fromX + "," + fromY + ") → (" + toX + "," + toY + ")";
}
public Pawn getMovedPiece() {
// TODO Auto-generated method stub
return null;
}
}