en passant method

This commit is contained in:
User 2025-05-13 15:39:16 +02:00
parent 9331dc8b12
commit db431b8d0b
1 changed files with 38 additions and 74 deletions

View File

@ -13,6 +13,7 @@ public class Board {
private int turnNumber = 0; private int turnNumber = 0;
private boolean turnWhite = true; private boolean turnWhite = true;
private ArrayList<int[]> highlightedSquares = new ArrayList<>(); private ArrayList<int[]> highlightedSquares = new ArrayList<>();
private int[] enPassantTarget = null; // [x,y] coordinates of en passant target square
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
this.width = colNum; this.width = colNum;
@ -138,94 +139,37 @@ public class Board {
int dir = piece.isWhite() ? 1 : -1; int dir = piece.isWhite() ? 1 : -1;
int startRow = piece.isWhite() ? 1 : 6; int startRow = piece.isWhite() ? 1 : 6;
// Normal forward moves
int oneStep = y + dir; int oneStep = y + dir;
if (inBounds(x, oneStep) && board[x][oneStep] == null) { if (inBounds(x, oneStep) && board[x][oneStep] == null) {
moves.add(new int[]{x, oneStep}); moves.add(new int[]{x, oneStep});
// If on start row, try two steps forward // Two-step move from starting position
int twoStep = y + 2 * dir; int twoStep = y + 2 * dir;
if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null) { if (y == startRow && inBounds(x, twoStep) && board[x][twoStep] == null && board[x][oneStep] == null) {
moves.add(new int[]{x, twoStep}); moves.add(new int[]{x, twoStep});
} }
} }
// Capture diagonals // Capture diagonals (including en passant)
for (int dx : new int[]{-1, 1}) { for (int dx : new int[]{-1, 1}) {
int nx = x + dx; int nx = x + dx;
int ny = y + dir; int ny = y + dir;
if (inBounds(nx, ny) && board[nx][ny] != null && if (inBounds(nx, ny)) {
board[nx][ny].isWhite() != piece.isWhite()) { // Normal capture
moves.add(new int[]{nx, ny}); if (board[nx][ny] != null && board[nx][ny].isWhite() != piece.isWhite()) {
}
}
}
else if (type == PieceType.Rook) {
int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
while (inBounds(nx, ny)) {
if (board[nx][ny] == null) {
moves.add(new int[]{nx, ny}); moves.add(new int[]{nx, ny});
} else { }
if (board[nx][ny].isWhite() != piece.isWhite()) { // En passant capture
else if (board[nx][ny] == null && enPassantTarget != null &&
nx == enPassantTarget[0] && ny == enPassantTarget[1]) {
// Verify there's an opponent pawn beside us
int pawnY = y; // The pawn to capture is beside us, not in front
if (inBounds(nx, pawnY) && board[nx][pawnY] != null &&
board[nx][pawnY].getType() == PieceType.Pawn &&
board[nx][pawnY].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny}); moves.add(new int[]{nx, ny});
} }
break;
}
nx += d[0]; ny += d[1];
}
}
}
else if (type == PieceType.Bishop) {
int[][] dirs = {{1,1}, {-1,1}, {-1,-1}, {1,-1}};
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
while (inBounds(nx, ny)) {
if (board[nx][ny] == null) {
moves.add(new int[]{nx, ny});
} else {
if (board[nx][ny].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny});
}
break;
}
nx += d[0]; ny += d[1];
}
}
}
else if (type == PieceType.Queen) {
// Queen = Rook + Bishop
moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Rook)));
moves.addAll(getValidMoves(new Piece(x, y, piece.isWhite(), PieceType.Bishop)));
}
else if (type == PieceType.Knight) {
int[][] jumps = {
{2,1},{1,2},{-1,2},{-2,1},
{-2,-1},{-1,-2},{1,-2},{2,-1}
};
for (int[] j : jumps) {
int nx = x + j[0], ny = y + j[1];
if (inBounds(nx, ny)) {
if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny});
}
}
}
}
else if (type == PieceType.King) {
int[][] dirs = {
{1,0}, {-1,0}, {0,1}, {0,-1},
{1,1}, {-1,1}, {-1,-1}, {1,-1}
};
for (int[] d : dirs) {
int nx = x + d[0], ny = y + d[1];
if (inBounds(nx, ny)) {
if (board[nx][ny] == null || board[nx][ny].isWhite() != piece.isWhite()) {
moves.add(new int[]{nx, ny});
} }
} }
} }
@ -248,7 +192,7 @@ public class Board {
selectedX = x; selectedX = x;
selectedY = y; selectedY = y;
hasSelectedPiece = true; hasSelectedPiece = true;
highlightedSquares = getValidMoves(clicked); // Highlight legal moves highlightedSquares = getValidMoves(clicked);
} }
} else { } else {
// Check if clicked again on the same square to unselect // Check if clicked again on the same square to unselect
@ -260,12 +204,32 @@ public class Board {
else if (isHighlighted(x, y)) { else if (isHighlighted(x, y)) {
Piece selectedPiece = board[selectedX][selectedY]; Piece selectedPiece = board[selectedX][selectedY];
// Check if this is an en passant capture
boolean isEnPassant = selectedPiece.getType() == PieceType.Pawn &&
enPassantTarget != null &&
x == enPassantTarget[0] &&
y == enPassantTarget[1] &&
board[x][y] == null;
// Move piece // Move piece
board[x][y] = selectedPiece; board[x][y] = selectedPiece;
board[selectedX][selectedY] = null; board[selectedX][selectedY] = null;
selectedPiece.setX(x); selectedPiece.setX(x);
selectedPiece.setY(y); selectedPiece.setY(y);
// If en passant, remove the captured pawn
if (isEnPassant) {
int capturedPawnY = selectedY; // The pawn is beside, not in front
board[x][capturedPawnY] = null;
}
// Set en passant target if pawn moved two squares
enPassantTarget = null;
if (selectedPiece.getType() == PieceType.Pawn &&
Math.abs(y - selectedY) == 2) {
enPassantTarget = new int[]{x, (selectedY + y) / 2};
}
// Update turn // Update turn
turnWhite = !turnWhite; turnWhite = !turnWhite;
turnNumber++; turnNumber++;