En Passant logic
This commit is contained in:
parent
58dac05f95
commit
27b23a3bca
|
|
@ -12,12 +12,13 @@ public class Board {
|
|||
private int[] selected = null; //for user touch
|
||||
private ArrayList<int[]> highlighted = new ArrayList<>();
|
||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||
private int[] enPassantTarget = null;
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
this.width = colNum;
|
||||
this.height = lineNum;
|
||||
this.pieces = new ArrayList<>();
|
||||
//TODO
|
||||
this.enPassantTarget = null;
|
||||
}
|
||||
|
||||
public Board(Board other) {
|
||||
|
|
@ -26,6 +27,10 @@ public class Board {
|
|||
this.turnNumber = other.turnNumber;
|
||||
this.turnWhite = other.turnWhite;
|
||||
this.selected = other.selected != null ? new int[]{other.selected[0], other.selected[1]} : null;
|
||||
this.enPassantTarget = other.enPassantTarget != null
|
||||
? new int[]{ other.enPassantTarget[0], other.enPassantTarget[1] }
|
||||
: null;
|
||||
|
||||
|
||||
this.pieces = new ArrayList<>();
|
||||
for (Piece p : other.pieces) {
|
||||
|
|
@ -44,29 +49,24 @@ public class Board {
|
|||
|
||||
|
||||
public int getWidth() {
|
||||
//TODO
|
||||
return width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
//TODO
|
||||
return height;
|
||||
}
|
||||
|
||||
public int getTurnNumber() {
|
||||
//TODO
|
||||
return turnNumber;
|
||||
}
|
||||
|
||||
public boolean isTurnWhite() {
|
||||
//TODO
|
||||
return turnWhite;
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
Piece piece = new Piece(isWhite, type, x, y);
|
||||
pieces.add(piece);
|
||||
//TODO
|
||||
}
|
||||
|
||||
public void populateBoard() {
|
||||
|
|
@ -97,12 +97,11 @@ public class Board {
|
|||
setPiece(false, PieceType.Bishop, 5, 7);
|
||||
setPiece(false, PieceType.Knight, 6, 7);
|
||||
setPiece(false, PieceType.Rook, 7, 7);
|
||||
//TODO
|
||||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
pieces.clear();
|
||||
//TODO
|
||||
enPassantTarget = null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
|
@ -267,6 +266,12 @@ public class Board {
|
|||
Piece diagRight = getPieceAt(x + 1, forwardY);
|
||||
if (diagRight != null && diagRight.isWhite() != piece.isWhite())
|
||||
moves.add(new int[]{x + 1, forwardY});
|
||||
if (enPassantTarget != null) {
|
||||
int tx = enPassantTarget[0], ty = enPassantTarget[1];
|
||||
if (ty == forwardY && Math.abs(tx - x) == 1) {
|
||||
moves.add(new int[]{ tx, ty });
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case Rook:
|
||||
|
|
@ -492,51 +497,78 @@ public void undoLastMove() {
|
|||
|
||||
public void playMove(Move move) {
|
||||
Piece piece = move.getPieceMoved();
|
||||
if (piece.getType() == PieceType.King && Math.abs(move.getToX() - move.getFromX()) == 2) {
|
||||
int row = piece.isWhite() ? 0 : 7;
|
||||
// Kingside castling
|
||||
if (move.getToX() == 6) {
|
||||
Piece rook = getPieceAt(7, row);
|
||||
if (rook != null) {
|
||||
rook.setPosition(5, row); // rook moves to f1/f8
|
||||
}
|
||||
}
|
||||
// Queenside castling
|
||||
else if (move.getToX() == 2) {
|
||||
Piece rook = getPieceAt(0, row);
|
||||
if (rook != null) {
|
||||
rook.setPosition(3, row); // rook moves to d1/d8
|
||||
}
|
||||
}
|
||||
}
|
||||
int fromX = move.getFromX(), fromY = move.getFromY();
|
||||
int toX = move.getToX(), toY = move.getToY();
|
||||
|
||||
// If a piece is captured, remove it from the board
|
||||
if (move.getPieceCaptured() != null) {
|
||||
// ─── castling ─────────────────────────────────────────────────────────
|
||||
if (piece.getType() == PieceType.King
|
||||
&& Math.abs(toX - fromX) == 2) {
|
||||
int row = piece.isWhite() ? 0 : 7;
|
||||
// kingside
|
||||
if (toX == 6) {
|
||||
Piece rook = getPieceAt(7, row);
|
||||
if (rook != null) rook.setPosition(5, row);
|
||||
}
|
||||
// queenside
|
||||
else {
|
||||
Piece rook = getPieceAt(0, row);
|
||||
if (rook != null) rook.setPosition(3, row);
|
||||
}
|
||||
}
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
// ─── en passant capture ─────────────────────────────────────────────
|
||||
if (piece.getType() == PieceType.Pawn
|
||||
&& enPassantTarget != null
|
||||
&& toX == enPassantTarget[0]
|
||||
&& toY == enPassantTarget[1]) {
|
||||
// the pawn being captured sits on (toX, fromY)
|
||||
Piece captured = getPieceAt(toX, fromY);
|
||||
if (captured != null && captured.getType() == PieceType.Pawn) {
|
||||
pieces.remove(captured);
|
||||
move.setPieceCaptured(captured); // so undo can restore it
|
||||
}
|
||||
}
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
// ─── normal capture ─────────────────────────────────────────────────
|
||||
else if (move.getPieceCaptured() != null) {
|
||||
pieces.remove(move.getPieceCaptured());
|
||||
}
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
// Update the moved piece's position
|
||||
piece.setPosition(move.getToX(), move.getToY());
|
||||
// move the piece
|
||||
piece.setPosition(toX, toY);
|
||||
|
||||
// ─── pawn promotion ──────────────────────────────────────────────────
|
||||
if (piece.getType() == PieceType.Pawn) {
|
||||
// white promotes on y == height‐1, black on y == 0
|
||||
int promotionRank = piece.isWhite() ? height - 1 : 0;
|
||||
if (piece.getY() == promotionRank) {
|
||||
if (toY == promotionRank) {
|
||||
piece.setType(PieceType.Queen);
|
||||
}
|
||||
}
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
// Save move in history for undo
|
||||
// ─── update enPassantTarget ────────────────────────────────────────
|
||||
if (piece.getType() == PieceType.Pawn
|
||||
&& Math.abs(toY - fromY) == 2) {
|
||||
// record the square it jumped over
|
||||
enPassantTarget = new int[]{ fromX, (fromY + toY) / 2 };
|
||||
} else {
|
||||
enPassantTarget = null;
|
||||
}
|
||||
// ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
// record history & advance turn
|
||||
moveHistory.add(move);
|
||||
|
||||
// Switch turn
|
||||
turnWhite = !turnWhite;
|
||||
turnNumber++;
|
||||
|
||||
// Clear selection/highlight
|
||||
// clear selection/highlight
|
||||
selected = null;
|
||||
highlighted.clear();
|
||||
}
|
||||
|
||||
public int evaluateBoard() {
|
||||
int score = 0;
|
||||
for (Piece piece : pieces) {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ public class Move {
|
|||
private final Piece pieceMoved;
|
||||
private final int fromX, fromY;
|
||||
private final int toX, toY;
|
||||
private final Piece pieceCaptured;
|
||||
private Piece pieceCaptured; // ← removed 'final' here
|
||||
|
||||
public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) {
|
||||
this.pieceMoved = pieceMoved;
|
||||
|
|
@ -21,4 +21,9 @@ public class Move {
|
|||
public int getToX() { return toX; }
|
||||
public int getToY() { return toY; }
|
||||
public Piece getPieceCaptured() { return pieceCaptured; }
|
||||
|
||||
|
||||
public void setPieceCaptured(Piece captured) {
|
||||
this.pieceCaptured = captured;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue