From 22eea2b7e9e16ac30828cef8341b4ab517a30e67 Mon Sep 17 00:00:00 2001 From: leahb Date: Mon, 19 May 2025 21:03:14 +0200 Subject: [PATCH] en oassant working --- src/backend/Board.java | 35 ++++++++++++++++++++++----------- src/backend/MoveConditions.java | 5 ++++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index dd07f37..b493aee 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -15,6 +15,7 @@ public class Board { private boolean pawnDoubleStep; private int xCoordinatePawn; private int yCoordinatePawn; + private boolean enPassant; private ArrayList previousStates; @@ -24,6 +25,7 @@ public class Board { this.turnNumber = 0; this.isWhiteTurn = true; // White starts first this.previousStates = new ArrayList<>(); // Initialize the ArrayList + } public int getWidth() { @@ -54,6 +56,14 @@ public class Board { return this.isWhiteTurn; } + public boolean isEnPassant() { + return this.enPassant; + } + + public void setEnPassant(boolean enPassant) { + this.enPassant = enPassant; + } + public void resetTurn() { this.turnNumber = 0; this.isWhiteTurn = true; @@ -196,11 +206,23 @@ public class Board { // detects castling (piece is castling only if piecetype is king and distance between king and its move is of 2) boolean isCastling = selectedPiece.getType() == PieceType.King && Math.abs(x - originalX) == 2; - - if (clickedPiece != null) { System.out.println("Capturing piece at: " + x + ", " + y); pieces.remove(clickedPiece); + } else { + if (enPassant == true && y == Math.abs(yCoordinatePawn-1) && x == xCoordinatePawn) { + pieces.remove(getPieceAt(xCoordinatePawn, yCoordinatePawn)); + enPassant = false; + } + } + + 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; } System.out.println("Moving piece to: " + x + ", " + y); @@ -223,15 +245,6 @@ public class Board { } } } - - 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 f0155e6..7da9fc5 100644 --- a/src/backend/MoveConditions.java +++ b/src/backend/MoveConditions.java @@ -5,8 +5,10 @@ import java.util.ArrayList; public class MoveConditions { private Piece piece; private Board board; + //private boolean enPassant; - public MoveConditions(Piece piece, Board board) { + + public MoveConditions(Piece piece, Board board) { this.piece = piece; this.board = board; } @@ -58,6 +60,7 @@ public class MoveConditions { sidePawn.isWhite() != isWhite) { moves.add(new int[]{sideX, y + dir}); // capture en passant square + board.setEnPassant(true); } } }