- Filled the Move class

-Created a new class MovementCapabilities and created the enPassant
method
- Completed lines in the Board class
- Added getter and setter in Piece class
- Added some lines in the MovePiece class
This commit is contained in:
willo 2025-05-15 22:45:25 +02:00
parent 96fe108557
commit 6b0cc0e0c4
5 changed files with 121 additions and 1 deletions

View File

@ -10,6 +10,7 @@ public class Board {
private int turnNumber=0; private int turnNumber=0;
private boolean isTurnWhite=true; private boolean isTurnWhite=true;
private ArrayList<int[]> highlightedSquares= new ArrayList<>(); //stores highlighted board positions private ArrayList<int[]> 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) { public Board(int colNum, int lineNum) {
this.width=colNum; this.width=colNum;
@ -154,6 +155,12 @@ public class Board {
// Update its internal coordinates // Update its internal coordinates
piece.setX(newX); piece.setX(newX);
piece.setY(newY); 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 :*/ /* saving-loading feature :*/
@ -185,6 +192,17 @@ public class Board {
return false; } return false; }
public void removePiece(int x, int y) {
board[x][y]= null;
}
public Move getLastMove() {
return lastMove;
}
public void undoLastMove() { public void undoLastMove() {
//TODO //TODO

View File

@ -1,5 +1,39 @@
package backend; package backend;
public class Move { 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;
}
}

View File

@ -40,6 +40,13 @@ public class MovePiece {
board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed board.movePiece(currentX, currentY, x, y); // we go diagonally and capture where another black piece is placed
return true; return true;
}
else { // checking for possibility of en passant
MovementCapabilities mc = new MovementCapabilities();
if (mc.enPassant(piece, x, y, board)) {
return true;
}
} }
return false; return false;
} }

View File

@ -0,0 +1,51 @@
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);
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);
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);
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);
board.removePiece(x, currentY);
return true;
}
}
}
return false;
}
}

View File

@ -6,6 +6,7 @@ public class Piece {
private int y_coor; private int y_coor;
private PieceType piece; private PieceType piece;
private boolean white; 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) { public Piece(int x, int y, PieceType laPiece, boolean isWhite) {
this.x_coor=x; this.x_coor=x;
@ -38,4 +39,13 @@ public class Piece {
return white; return white;
} }
public boolean didMove() { // added getter
return didMove;
}
public void setDidMove(boolean value) { // added setter
didMove=value;
}
} }