From 315c8477b096de26d58c4a4140dadfec11e18adc Mon Sep 17 00:00:00 2001 From: leahb Date: Tue, 13 May 2025 16:27:06 +0200 Subject: [PATCH] attempt en passant --- src/backend/Board.java | 36 +++++++++++++++++++++++++++++++++++ src/backend/Piece.java | 11 ++++++++++- src/backend/SpecialMoves.java | 33 +++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index e9ac46f..b3ff08a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -102,6 +102,42 @@ 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(); } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index da4dab6..3a8caf4 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -6,7 +6,16 @@ public class Piece { private boolean isWhite; private PieceType type; - + /*private boolean enPassantEligible = false; + + public boolean isEnPassantEligible() { + return enPassantEligible; + } + + public void setEnPassantEligible(boolean eligible) { + this.enPassantEligible = eligible; + } +*/ public Piece(int x, int y, boolean isWhite, PieceType type) { this.x=x; this.y=y; diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java index 3ce85ee..5bf6b40 100644 --- a/src/backend/SpecialMoves.java +++ b/src/backend/SpecialMoves.java @@ -57,4 +57,35 @@ public class SpecialMoves { } } -}*/ +} +package backend; + +public class SpecialMoves { + + private Piece piece; + private Board board; + + public SpecialMoves(Piece piece, Board board) { + this.piece = piece; + this.board = board; + } + + // Call this when trying to move a pawn to check for en passant + public boolean isEnpassant(int fromX, int fromY, int toX, int toY) { + if (piece.getType() != PieceType.Pawn) return false; + + boolean isWhite = piece.isWhite(); + + // En passant capture happens diagonally + int dx = toX - fromX; + int dy = toY - fromY; + + if (Math.abs(dx) != 1 || (isWhite && dy != -1) || (!isWhite && dy != 1)) { + return false; + } + + // Check the adjacent piece + Piece sidePawn = board.getPieceAt(toX, fromY); + if (sidePawn == null || sidePawn.getType() != PieceType.Pawn || sidePawn.isWhite() == isWhite) { + +*/ \ No newline at end of file