handling the new class special moves with the new parameters
This commit is contained in:
parent
07a9040077
commit
661d5980c7
|
|
@ -447,7 +447,7 @@ public class Board {
|
|||
return x >= 0 && x < width && y >= 0 && y < height;
|
||||
}
|
||||
|
||||
private boolean isEmpty(int x, int y) {
|
||||
public boolean isEmpty(int x, int y) {
|
||||
return isValidPosition(x, y) && board[x][y] == null;
|
||||
}
|
||||
|
||||
|
|
@ -459,6 +459,32 @@ public class Board {
|
|||
return isValidPosition(x, y) && board[x][y] != null && board[x][y].isWhite() == isWhite;
|
||||
}
|
||||
|
||||
public Piece getPieceAt(int x, int y) {
|
||||
if (!isValidPosition(x, y)) return null;
|
||||
return board[x][y];
|
||||
}
|
||||
|
||||
public void removePieceAt(int x, int y) {
|
||||
if (isValidPosition(x, y)) board[x][y] = null;
|
||||
}
|
||||
|
||||
public void setEnPassantCol(int col) {
|
||||
this.enPassantCol = col;
|
||||
}
|
||||
|
||||
public void setEnPassantRow(int row) {
|
||||
this.enPassantRow = row;
|
||||
}
|
||||
|
||||
public int getEnPassantCol() {
|
||||
return this.enPassantCol;
|
||||
}
|
||||
|
||||
public int getEnPassantRow() {
|
||||
return this.enPassantRow;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void undoLastMove() {
|
||||
if (!undoStack.isEmpty()) {
|
||||
|
|
@ -497,46 +523,34 @@ public class Board {
|
|||
// Save current state before move for undo
|
||||
undoStack.push(toFileRep());
|
||||
|
||||
// existing move logic
|
||||
Piece piece = board[move.getFromX()][move.getFromY()];
|
||||
|
||||
// Handle en passant capture
|
||||
if (SpecialMoves.isEnPassant(this, move)) {
|
||||
Piece capturedPawn = board[move.getToX()][move.getFromY()];
|
||||
move.setCapturedPiece(capturedPawn);
|
||||
SpecialMoves.handleEnPassant(this, move);
|
||||
}
|
||||
|
||||
// Move the piece
|
||||
board[move.getToX()][move.getToY()] = piece;
|
||||
board[move.getFromX()][move.getFromY()] = null;
|
||||
piece.x = move.getToX();
|
||||
piece.y = move.getToY();
|
||||
|
||||
// Reset en passant tracking at the start of each move
|
||||
enPassantCol = -1;
|
||||
enPassantRow = -1;
|
||||
|
||||
// Check if this is a pawn making a double move
|
||||
if (piece.getType() == PieceType.Pawn &&
|
||||
Math.abs(move.getFromY() - move.getToY()) == 2) {
|
||||
enPassantCol = move.getToX();
|
||||
enPassantRow = (move.getFromY() + move.getToY()) / 2; // Middle square
|
||||
}
|
||||
// Update en passant tracking
|
||||
SpecialMoves.updateEnPassantTracking(this, move, piece);
|
||||
|
||||
// Handle pawn promotion
|
||||
if (piece.getType() == PieceType.Pawn &&
|
||||
(piece.getY() == 0 || piece.getY() == height - 1)) {
|
||||
board[piece.getX()][piece.getY()] = promotePawn(piece.isWhite(), piece.getX(), piece.getY());
|
||||
}
|
||||
|
||||
// Handle en passant capture
|
||||
if (piece.getType() == PieceType.Pawn &&
|
||||
move.getToX() != move.getFromX() &&
|
||||
board[move.getToX()][move.getToY()] == null) {
|
||||
// This is a diagonal pawn move to an empty square - must be en passant
|
||||
|
||||
// Capture the pawn
|
||||
Piece capturedPawn = board[move.getToX()][move.getFromY()];
|
||||
move.setCapturedPiece(capturedPawn);
|
||||
|
||||
// Remove the captured pawn
|
||||
board[move.getToX()][move.getFromY()] = null; // not working
|
||||
board[piece.getX()][piece.getY()] = SpecialMoves.promotePawn(piece.isWhite(), piece.getX(), piece.getY());
|
||||
}
|
||||
|
||||
turn++;
|
||||
}
|
||||
|
||||
|
||||
//Finds the position of the king of a given color.
|
||||
public Piece findKing(boolean isWhite) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue