diff --git a/src/backend/Board.java b/src/backend/Board.java index 9d4d335..6bba15a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -12,6 +12,9 @@ public class Board { private int selectedX = -1; private int selectedY = -1; ArrayList highlightedSquares = new ArrayList<>(); + private boolean pawnDoubleStep; + private int xCoordinatePawn; + private int yCoordinatePawn; private ArrayList 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; diff --git a/src/backend/MoveConditions.java b/src/backend/MoveConditions.java index 0021d22..f0155e6 100644 --- a/src/backend/MoveConditions.java +++ b/src/backend/MoveConditions.java @@ -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 getKnightMoves() { ArrayList moves = new ArrayList<>(); diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java index 5bf6b40..5c50a38 100644 --- a/src/backend/SpecialMoves.java +++ b/src/backend/SpecialMoves.java @@ -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;