OOP_Groupe_1A3_Project/src/backend/MovementCapabilities.java

109 lines
3.7 KiB
Java

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;
}
public boolean castling(Piece king, int x, int y, Board board) {
if (king.getType()!= PieceType.King|| king.getDidMove()) {
return false;
}
int currentX = king.getX();
int currentY = king.getY();
boolean isWhite = king.isWhite(); // getting coordinates and color of the king
if (y != currentY) { // checking to see if the king has moved during the game yet
return false;
}
if (x==currentX+2) { // moving the king to the right of the rook AKA "kingside castling"
Piece rook = board.getPiece(7, currentY);
// writing each condition separately instead of in the if loop for more clarity
boolean isRook = rook != null && rook.getType()== PieceType.Rook; // check if rook exists
boolean rookMoved = !rook.getDidMove();
boolean emptySpaceRight = board.getPiece(5, currentY)== null && board.getPiece(6, currentY)== null;
if (isRook && rookMoved) {
if (emptySpaceRight) {
board.movePiece(currentX, currentY, 6, currentY); // moving the king to ...
board.movePiece(7, currentY, 5, currentY); // moving the rook to ...
return true;
}
}
}
if (x==currentX-2) { // moving the king to the left of the rook AKA "queenside castling")
Piece rook = board.getPiece(0, currentY);
boolean isRook = rook != null && rook.getType()== PieceType.Rook;
boolean rookMoved = !rook.getDidMove();
boolean emptySpaceLeft = board.getPiece(3, currentY) == null && board.getPiece(2, currentY) == null && board.getPiece(1, currentY)== null;
if (isRook && rookMoved) {
if (emptySpaceLeft) {
board.movePiece(currentX, currentY, 2, currentY); // moving king to...
board.movePiece(0,currentY,3,currentY); // moving rook to...
return true;
}
}
}
return false;
}
}