En passant logic - to do: make sure the taken pawn is removed from board
This commit is contained in:
parent
668bb8cb15
commit
48eab6ae73
|
|
@ -12,6 +12,9 @@ public class Board {
|
|||
private int selectedX = -1;
|
||||
private int selectedY = -1;
|
||||
ArrayList<int[]> highlightedSquares = new ArrayList<>();
|
||||
private boolean pawnDoubleStep;
|
||||
private int xCoordinatePawn;
|
||||
private int yCoordinatePawn;
|
||||
|
||||
private ArrayList<Board> previousStates;
|
||||
|
||||
|
|
@ -27,6 +30,18 @@ public class Board {
|
|||
return colNum;
|
||||
}
|
||||
|
||||
public boolean isPawnDoubleStep() {
|
||||
return pawnDoubleStep;
|
||||
}
|
||||
|
||||
public int getXCoordinatePawn() {
|
||||
return xCoordinatePawn;
|
||||
}
|
||||
|
||||
public int getYCoordinatePawn() {
|
||||
return yCoordinatePawn;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return lineNum;
|
||||
}
|
||||
|
|
@ -102,42 +117,6 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
/*public void movePiece(int fromX, int fromY, int toX, int toY) {
|
||||
Piece piece = board.getPieceAt(fromX, fromY);
|
||||
if (piece == null) return;
|
||||
|
||||
// --- En Passant ---
|
||||
SpecialMoves special = new SpecialMoves(piece, board);
|
||||
boolean isEnPassant = special.isEnpassant(fromX, fromY, toX, toY);
|
||||
|
||||
// --- Move the piece
|
||||
board.setPieceAt(toX, toY, piece);
|
||||
board.setPieceAt(fromX, fromY, null);
|
||||
piece.setX(toX);
|
||||
piece.setY(toY);
|
||||
|
||||
// --- Reset all pawns' en passant eligibility
|
||||
for (int i = 0; i < 8; i++) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
Piece p = board.getPieceAt(i, j);
|
||||
if (p != null && p.getType() == PieceType.Pawn) {
|
||||
p.setEnPassantEligible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- If current pawn moved 2 steps forward, set it eligible for en passant
|
||||
if (piece.getType() == PieceType.Pawn && Math.abs(toY - fromY) == 2) {
|
||||
piece.setEnPassantEligible(true);
|
||||
}
|
||||
|
||||
// --- En Passant message
|
||||
if (isEnPassant) {
|
||||
System.out.println("En Passant captured successfully!");
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
public void cleanBoard() {
|
||||
pieces.clear();
|
||||
}
|
||||
|
|
@ -208,15 +187,25 @@ public class Board {
|
|||
System.out.println("Invalid move — not in highlighted squares.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (clickedPiece != null) {
|
||||
System.out.println("Capturing piece at: " + x + ", " + y);
|
||||
pieces.remove(clickedPiece);
|
||||
pieces.remove(clickedPiece);
|
||||
}
|
||||
|
||||
|
||||
System.out.println("Moving piece to: " + x + ", " + y);
|
||||
selectedPiece.setX(x);
|
||||
selectedPiece.setY(y);
|
||||
|
||||
if (selectedPiece.getType() == PieceType.Pawn && Math.abs(y - selectedY) == 2) {
|
||||
pawnDoubleStep = true; //boolean to check if pawn has been moved 2 at start
|
||||
xCoordinatePawn = x; //get its coordinates
|
||||
yCoordinatePawn = y;
|
||||
System.out.println("Pawn moved two squares to (" + xCoordinatePawn + ", " + yCoordinatePawn + ")");
|
||||
} else {
|
||||
pawnDoubleStep = false;
|
||||
}
|
||||
|
||||
turnNumber++;
|
||||
isWhiteTurn = !isWhiteTurn;
|
||||
|
|
|
|||
|
|
@ -42,8 +42,29 @@ public class MoveConditions {
|
|||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
|
||||
// En Passant
|
||||
boolean pawnDoubleStep = board.isPawnDoubleStep();
|
||||
int xCoordinatePawn = board.getXCoordinatePawn();
|
||||
int yCoordinatePawn = board.getYCoordinatePawn();
|
||||
|
||||
if (pawnDoubleStep && y == (isWhite ? 3 : 4)) { // y = 3 for white, y = 4 for black
|
||||
for (int dx : new int[]{-1, 1}) {
|
||||
int sideX = x + dx;
|
||||
if (sideX == xCoordinatePawn && y == yCoordinatePawn) {
|
||||
Piece sidePawn = board.getPieceAt(sideX, y);
|
||||
if (sidePawn != null &&
|
||||
sidePawn.getType() == PieceType.Pawn &&
|
||||
sidePawn.isWhite() != isWhite) {
|
||||
|
||||
moves.add(new int[]{sideX, y + dir}); // capture en passant square
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getKnightMoves() {
|
||||
ArrayList<int[]> moves = new ArrayList<>();
|
||||
|
|
|
|||
|
|
@ -1,18 +1,74 @@
|
|||
/*package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SpecialMoves {
|
||||
<<<<<<< HEAD
|
||||
|
||||
private Piece piece;
|
||||
private Board board;
|
||||
|
||||
|
||||
private Piece piece;
|
||||
|
||||
private Piece piece;
|
||||
private Board board;
|
||||
private boolean isWhite;
|
||||
|
||||
|
||||
public SpecialMoves(Piece piece, Board board) {
|
||||
this.piece = piece;
|
||||
this.board = board;
|
||||
this.isWhite = piece.isWhite();
|
||||
}
|
||||
|
||||
private int x = piece.getX();
|
||||
private int y = piece.getY();
|
||||
private PieceType type = PieceType.getType();
|
||||
private boolean isWhite;
|
||||
|
||||
private boolean pawnDoubleStep = board.isPawnDoubleStep();
|
||||
private int xCoordinatePawn = board.getXCoordinatePawn();
|
||||
private int yCoordinatePawn = board.getYCoordinatePawn();
|
||||
|
||||
for (int n = 2; n < 4; n++) {
|
||||
int i = x + pieceMoves[n][0]
|
||||
|
||||
int j = y + pieceMoves[n][1];
|
||||
Move move = new Move(x, y, i, j);
|
||||
|
||||
// Diagonal capture
|
||||
if (inBoard(i, j) &&
|
||||
board.getPiece(j, i) != null &&
|
||||
board.getPiece(j, i).isWhite() != isWhite &&
|
||||
!checkIf(move, coordinates[0], coordinates[1], isWhite)) {
|
||||
|
||||
moves.add(new int[]{i, j});
|
||||
}
|
||||
|
||||
// En Passant
|
||||
else if (inBoard(i, j) &&
|
||||
board.getPiece(j, i) == null &&
|
||||
pawnDoubleStep &&
|
||||
i == xCoordinatePawn &&
|
||||
y == yCoordinatePawn &&
|
||||
Math.abs(x - xCoordinatePawn) == 1 &&
|
||||
board.getPiece(y, xCoordinatePawn) != null &&
|
||||
board.getPiece(y, xCoordinatePawn).getType() == PieceType.Pawn &&
|
||||
board.getPiece(y, xCoordinatePawn).isWhite() != isWhite &&
|
||||
!checkIf(move, coordinates[0], coordinates[1], isWhite)) {
|
||||
|
||||
moves.add(new int[]{i, j});
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
private boolean inBoard(int x, int y) {
|
||||
return x >= 0 && x < 8 && y >= 0 && y < 8;
|
||||
}
|
||||
|
||||
private boolean checkIf(Move move, int x, int y, boolean isWhite) {
|
||||
// Stub – replace with your actual move-check logic
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* public SpecialMoves(Piece piece, Board board) {
|
||||
this.piece = piece;
|
||||
|
|
|
|||
Loading…
Reference in New Issue