Implementation of en passant but does not seem to work.
This commit is contained in:
parent
361f8f2d3c
commit
5f340c22b2
|
|
@ -346,7 +346,29 @@ public class Board implements Cloneable {
|
|||
System.err.println("Invalid move: wrong turn");
|
||||
return;
|
||||
}
|
||||
boolean wasThisMoveAnEnPassantCapture = false;
|
||||
|
||||
if (pieceToMove.getType() == PieceType.Pawn &&
|
||||
move.getToCol() != move.getFromCol() &&
|
||||
getPieceAt(move.getToCol(), move.getToRow()) == null &&
|
||||
move.getCapturedPiece() != null &&
|
||||
move.getCapturedPiece().getType() == PieceType.Pawn &&
|
||||
move.getCapturedPiece() == this.enPassantVulnerablePawn &&
|
||||
move.getCapturedPiece().getX() == move.getToCol() &&
|
||||
move.getCapturedPiece().getY() == move.getFromRow()) {
|
||||
|
||||
wasThisMoveAnEnPassantCapture = true;
|
||||
Piece actualPawnToCaptureForEP = move.getCapturedPiece();
|
||||
if (pieces.contains(actualPawnToCaptureForEP)) {
|
||||
pieces.remove(actualPawnToCaptureForEP);
|
||||
}
|
||||
} else if (move.getCapturedPiece() != null) {
|
||||
|
||||
if (pieces.contains(move.getCapturedPiece())) {
|
||||
pieces.remove(move.getCapturedPiece());
|
||||
}
|
||||
} else {
|
||||
Piece pawnThatJustAdvancedTwoSquares = null;
|
||||
// Remove captured piece, if any
|
||||
Piece capturedPiece = getPieceAt(move.getToCol(), move.getToRow());
|
||||
if (capturedPiece != null) {
|
||||
|
|
@ -367,7 +389,7 @@ public class Board implements Cloneable {
|
|||
turnNumber++;
|
||||
|
||||
this.lastMoveSourceSquare = new int[] {move.getFromCol(), move.getFromRow()};
|
||||
|
||||
this.enPassantVulnerablePawn = null;
|
||||
// Play move sound if enabled
|
||||
playMoveSound();
|
||||
return;
|
||||
|
|
@ -388,6 +410,7 @@ public class Board implements Cloneable {
|
|||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public ArrayList<Move> getAllLegalMoves(boolean isWhite) {
|
||||
|
|
@ -550,10 +573,23 @@ public class Board implements Cloneable {
|
|||
&& kingCheckPos[0] == squareX
|
||||
&& kingCheckPos[1] == squareY);
|
||||
}
|
||||
private Piece enPassantVulnerablePawn = null;
|
||||
private int[] lastMoveSourceSquare = null;
|
||||
|
||||
public boolean isLastMoveSourceSquare(int x, int y) {
|
||||
return lastMoveSourceSquare != null && lastMoveSourceSquare[0] == x && lastMoveSourceSquare[1] == y;
|
||||
}
|
||||
public Piece getEnPassantVulnerablePawn() {
|
||||
return this.enPassantVulnerablePawn;
|
||||
}
|
||||
private void updateEnPassantStateAfterMove(Move playedMove, Piece pieceThatMoved) {
|
||||
if (pieceThatMoved.getType() == PieceType.Pawn &&
|
||||
Math.abs(playedMove.getFromRow() - playedMove.getToRow()) == 2) {
|
||||
this.enPassantVulnerablePawn = pieceThatMoved;
|
||||
System.out.println("EP Vulnerable: " + pieceThatMoved.getType() + " at " + pieceThatMoved.getX() + "," + pieceThatMoved.getY());
|
||||
} else {
|
||||
this.enPassantVulnerablePawn = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,4 +49,5 @@ public class Move {
|
|||
return movedPiece.getType() + " from (" + fromRow + "," + fromCol + ") to (" + toRow + "," + toCol + ")" +
|
||||
(capturedPiece != null ? " capturing " + capturedPiece.getType() : "");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,22 @@ public class Pawn extends Piece {
|
|||
}
|
||||
}
|
||||
}
|
||||
Piece vulnerablePawn = board.getEnPassantVulnerablePawn();
|
||||
|
||||
|
||||
if (vulnerablePawn != null &&
|
||||
vulnerablePawn.isWhite() != this.isWhite &&
|
||||
vulnerablePawn.getType() == PieceType.Pawn &&
|
||||
vulnerablePawn.getY() == row &&
|
||||
Math.abs(vulnerablePawn.getX() - col) == 1) {
|
||||
|
||||
int enPassantToRow = row + direction;
|
||||
int enPassantToCol = vulnerablePawn.getX();
|
||||
moves.add(new Move(this, row, col, enPassantToRow, enPassantToCol, vulnerablePawn));
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue