diff --git a/src/backend/Board.java b/src/backend/Board.java index 95213a2..a8f9dc7 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -10,6 +10,7 @@ public class Board { private int turnNumber=0; private boolean isTurnWhite=true; private ArrayList highlightedSquares= new ArrayList<>(); //stores highlighted board positions + private Move lastMove; // Tracks the last move; needed for en-passant rule public Board(int colNum, int lineNum) { this.width=colNum; @@ -156,6 +157,12 @@ public class Board { // Update its internal coordinates piece.setX(newX); piece.setY(newY); + lastMove= new Move(oldX, oldY, newX, newY, piece); + System.out.println(" Move saved: " + oldX + "," + oldY + " to " + newX + "," + newY); + piece.setDidMove(true);// setting that the piece has been moved + + + } } /* saving-loading feature :*/ @@ -185,6 +192,17 @@ public class Board { } } return false; } + + + public void removePiece(int x, int y) { + board[x][y]= null; + } + + + public Move getLastMove() { + return lastMove; + } + public void undoLastMove() { diff --git a/src/backend/Move.java b/src/backend/Move.java index 5f64282..02c3745 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -1,5 +1,39 @@ package backend; public class Move { + private int oldX,oldY,newX,newY; // starting and finishing coordinates of the move + private Piece pieceMoved;// the piece that has moved + + public Move(int oldX, int oldY,int newX,int newY, Piece pieceMoved) { + this.oldX=oldX; + this.oldY=oldY; + this.newX=newX; + this.newY=newY; + this.pieceMoved=pieceMoved; + } + + public int getOldX() { + return oldX; + + } + + public int getOldY() { + return oldY; + + } + + public int getNewX() { + return newX; + + } + + public int getNewY() { + return newY; + } + + public Piece getPieceMoved() { + return pieceMoved; + + } } diff --git a/src/backend/MovePiece.java b/src/backend/MovePiece.java index b3eb075..5008553 100644 --- a/src/backend/MovePiece.java +++ b/src/backend/MovePiece.java @@ -79,6 +79,13 @@ public class MovePiece { board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed return true; } + else { // checking for possibility of en passant + MovementCapabilities mc = new MovementCapabilities(); + if (mc.enPassant(piece, x, y, board)) { + return true; + } + + } return false; } diff --git a/src/backend/MovementCapabilities.java b/src/backend/MovementCapabilities.java new file mode 100644 index 0000000..be3bb63 --- /dev/null +++ b/src/backend/MovementCapabilities.java @@ -0,0 +1,52 @@ +package backend; + +public class MovementCapabilities { + public boolean enPassant(Piece piece, int x, int y, Board board) { + if (piece.getType() != PieceType.Pawn) { + return false; + } + + int currentX = piece.getX(); + int currentY = piece.getY(); + boolean isWhite = piece.isWhite(); + int direction = isWhite ? -1:1; + + //System.out.println("Checking en passant for pawn at " + currentX + "," + currentY + " to " + x + "," + y); + // to check if the en passant conditions are met + + if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y)== null) { + Move last = board.getLastMove(); + + System.out.println("Last move is " + last); // Keeping record of the last move the + + if (last != null) { + Piece lastMoved = last.getPieceMoved(); + System.out.println("Last moved: " + lastMoved.getType() + " at " + last.getOldX() + "," + last.getOldY() + " to " + last.getNewX() + "," + last.getNewY()); + + boolean isPawn = lastMoved != null && lastMoved.getType()== PieceType.Pawn ; + boolean isOpp = lastMoved.isWhite() != isWhite ; + boolean movedTwo = Math.abs(last.getOldY() - last.getNewY()) == 2; + boolean sameX = last.getNewX()== x; + boolean sameY = last.getNewY()== currentY; + + /* + * System.out.println(" isPawn: " + isPawn); System.out.println(" isEnemy: " + + * isOpp); System.out.println(" movedTwo: " + movedTwo); + * System.out.println(" sameY: " + sameY); System.out.println(" sameX: " + + * sameX); + */ + // to check if each condition is valid in the console + if (isPawn && isOpp && movedTwo && sameX && sameY) { + System.out.println(" En passant conditions met!"); + board.movePiece(currentX, currentY, x, y); + //board.setPiece(false, null, x, currentY); // DIDN'T WORK --> instead used removePiece method + board.removePiece(x, currentY); + return true; + } + } + } + return false; + } +} + + diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 9adca3e..0a8490d 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -6,6 +6,7 @@ public class Piece { private int y_coor; private PieceType piece; private boolean white; + private boolean didMove; // Defining if the piece has moved yet or not (false as a default) public Piece(int x, int y, PieceType laPiece, boolean isWhite) { this.x_coor=x; @@ -38,4 +39,13 @@ public class Piece { return white; } -} + public boolean didMove() { // added getter + return didMove; + + } + + public void setDidMove(boolean value) { // added setter + didMove=value; + + } + }