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 int[] selected = null; //for user touch
|
||||||
private ArrayList<int[]> highlighted = new ArrayList<>();
|
private ArrayList<int[]> highlighted = new ArrayList<>();
|
||||||
private ArrayList<Move> moveHistory = new ArrayList<>();
|
private ArrayList<Move> moveHistory = new ArrayList<>();
|
||||||
|
private int[] enPassantTarget = null;
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum;
|
this.width = colNum;
|
||||||
this.height = lineNum;
|
this.height = lineNum;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
//TODO
|
this.enPassantTarget = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board(Board other) {
|
public Board(Board other) {
|
||||||
|
|
@ -26,6 +27,10 @@ public class Board {
|
||||||
this.turnNumber = other.turnNumber;
|
this.turnNumber = other.turnNumber;
|
||||||
this.turnWhite = other.turnWhite;
|
this.turnWhite = other.turnWhite;
|
||||||
this.selected = other.selected != null ? new int[]{other.selected[0], other.selected[1]} : null;
|
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<>();
|
this.pieces = new ArrayList<>();
|
||||||
for (Piece p : other.pieces) {
|
for (Piece p : other.pieces) {
|
||||||
|
|
@ -44,29 +49,24 @@ public class Board {
|
||||||
|
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
//TODO
|
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
//TODO
|
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getTurnNumber() {
|
public int getTurnNumber() {
|
||||||
//TODO
|
|
||||||
return turnNumber;
|
return turnNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTurnWhite() {
|
public boolean isTurnWhite() {
|
||||||
//TODO
|
|
||||||
return turnWhite;
|
return turnWhite;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
Piece piece = new Piece(isWhite, type, x, y);
|
Piece piece = new Piece(isWhite, type, x, y);
|
||||||
pieces.add(piece);
|
pieces.add(piece);
|
||||||
//TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
|
|
@ -97,12 +97,11 @@ public class Board {
|
||||||
setPiece(false, PieceType.Bishop, 5, 7);
|
setPiece(false, PieceType.Bishop, 5, 7);
|
||||||
setPiece(false, PieceType.Knight, 6, 7);
|
setPiece(false, PieceType.Knight, 6, 7);
|
||||||
setPiece(false, PieceType.Rook, 7, 7);
|
setPiece(false, PieceType.Rook, 7, 7);
|
||||||
//TODO
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanBoard() {
|
public void cleanBoard() {
|
||||||
pieces.clear();
|
pieces.clear();
|
||||||
//TODO
|
enPassantTarget = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
@ -267,6 +266,12 @@ public class Board {
|
||||||
Piece diagRight = getPieceAt(x + 1, forwardY);
|
Piece diagRight = getPieceAt(x + 1, forwardY);
|
||||||
if (diagRight != null && diagRight.isWhite() != piece.isWhite())
|
if (diagRight != null && diagRight.isWhite() != piece.isWhite())
|
||||||
moves.add(new int[]{x + 1, forwardY});
|
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;
|
break;
|
||||||
|
|
||||||
case Rook:
|
case Rook:
|
||||||
|
|
@ -490,53 +495,80 @@ public void undoLastMove() {
|
||||||
highlighted.clear();
|
highlighted.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
Piece piece = move.getPieceMoved();
|
Piece piece = move.getPieceMoved();
|
||||||
if (piece.getType() == PieceType.King && Math.abs(move.getToX() - move.getFromX()) == 2) {
|
int fromX = move.getFromX(), fromY = move.getFromY();
|
||||||
int row = piece.isWhite() ? 0 : 7;
|
int toX = move.getToX(), toY = move.getToY();
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If a piece is captured, remove it from the board
|
// ─── castling ─────────────────────────────────────────────────────────
|
||||||
if (move.getPieceCaptured() != null) {
|
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());
|
pieces.remove(move.getPieceCaptured());
|
||||||
}
|
}
|
||||||
|
// ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
// Update the moved piece's position
|
// move the piece
|
||||||
piece.setPosition(move.getToX(), move.getToY());
|
piece.setPosition(toX, toY);
|
||||||
|
|
||||||
|
// ─── pawn promotion ──────────────────────────────────────────────────
|
||||||
if (piece.getType() == PieceType.Pawn) {
|
if (piece.getType() == PieceType.Pawn) {
|
||||||
// white promotes on y == height‐1, black on y == 0
|
|
||||||
int promotionRank = piece.isWhite() ? height - 1 : 0;
|
int promotionRank = piece.isWhite() ? height - 1 : 0;
|
||||||
if (piece.getY() == promotionRank) {
|
if (toY == promotionRank) {
|
||||||
piece.setType(PieceType.Queen);
|
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);
|
moveHistory.add(move);
|
||||||
|
|
||||||
// Switch turn
|
|
||||||
turnWhite = !turnWhite;
|
turnWhite = !turnWhite;
|
||||||
turnNumber++;
|
turnNumber++;
|
||||||
|
|
||||||
// Clear selection/highlight
|
// clear selection/highlight
|
||||||
selected = null;
|
selected = null;
|
||||||
highlighted.clear();
|
highlighted.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int evaluateBoard() {
|
public int evaluateBoard() {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
for (Piece piece : pieces) {
|
for (Piece piece : pieces) {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ public class Move {
|
||||||
private final Piece pieceMoved;
|
private final Piece pieceMoved;
|
||||||
private final int fromX, fromY;
|
private final int fromX, fromY;
|
||||||
private final int toX, toY;
|
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) {
|
public Move(Piece pieceMoved, int fromX, int fromY, int toX, int toY, Piece pieceCaptured) {
|
||||||
this.pieceMoved = pieceMoved;
|
this.pieceMoved = pieceMoved;
|
||||||
|
|
@ -21,4 +21,9 @@ public class Move {
|
||||||
public int getToX() { return toX; }
|
public int getToX() { return toX; }
|
||||||
public int getToY() { return toY; }
|
public int getToY() { return toY; }
|
||||||
public Piece getPieceCaptured() { return pieceCaptured; }
|
public Piece getPieceCaptured() { return pieceCaptured; }
|
||||||
|
|
||||||
|
|
||||||
|
public void setPieceCaptured(Piece captured) {
|
||||||
|
this.pieceCaptured = captured;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue