package backend; public class MovePiece { private Piece piece; private Board board; public MovePiece(Piece piece, Board board) { this.piece = piece; this.board = board; } /*alternative * public boolean validateMove(int x, int y, boolean simulate) { PieceType type = piece.getType(); switch(type) { case Pawn: return validatePawnMove(x, y, simulate); case Rook: return validateRookMove(x, y, simulate); case Knight: return validateKnightMove(x, y, simulate); case Bishop: return validateBishopMove(x, y, simulate); case Queen: return validateQueenMove(x, y, simulate); case King: return validateKingMove(x, y, simulate); default: return false; } } public boolean moveKnight(int x, int y) { return validateMove(x, y, false); } public boolean moveKnightSimulate(int x, int y) { return validateMove(x, y, true); } private boolean validateKnightMove(int x, int y, boolean simulate) { int currentX = piece.getX(); int currentY = piece.getY(); int[][] knightMoves = { //we establish all possible moves a knight can have {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2} }; for (int[] move : knightMoves) { // we create a loop through each possible move if (currentX + move[0] == x && currentY + move[1] == y) { // checks if move matches the target coordination (move0 is in x direction, move 1 in y) Piece targetPiece = board.getPiece(x, y); //get the piece on the target spot if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { board.movePiece(currentX, currentY, x, y); //move our figure to the target spot return true; } } } return false; } */ ///Pawn movement logic public boolean movePawn(int x, int y) { //method definition for pawn int currentX = piece.getX(); // we call the method getX to get x coordinates int currentY = piece.getY(); // we call the method getY to get y coordinates boolean isWhite = piece.isWhite(); // checks if the pawn is white or black int direction = isWhite ? -1 : 1; // we check, if its white we have -1 as it goes down if false we have black then we have 1 boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // checks if its the first move. are the pawns still on their lines? if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { // we go one step forward board.movePiece(currentX, currentY, x, y); return true; }else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { // we go two steps forward bcs its the start of the game //and we ensure that there really is nothing in between the original position and the 2nd step board.movePiece(currentX, currentY, x, y); return true; } // ⚠️ Note: In a full version, you should also check the square in between is empty (e.g., board.getPiece(x, currentY + direction) == null) — otherwise the pawn could "jump over" a piece else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) { 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; } //Rook movement logic public boolean moveRook(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); if (currentX == x || currentY == y) // Check if the move is either in the same row or the same column if (isPathClear(currentX, currentY, x, y)) { //Check if there are obstacles in the path Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //Check if the target is empty or an enemy piece board.movePiece(currentX, currentY, x, y); return true; //move was successful } } return false; //if not it was unsuccessful } //Knight movement logic public boolean moveKnight(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); int[][] knightMoves = { //we establish all possible moves a knight can have {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2} }; for (int[] move : knightMoves) { // we create a loop through each possible move if (currentX + move[0] == x && currentY + move[1] == y) { // checks if move matches the target coordination (move0 is in x direction, move 1 in y) Piece targetPiece = board.getPiece(x, y); //get the piece on the target spot if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { board.movePiece(currentX, currentY, x, y); //move our figure to the target spot return true; } } } return false; } // Bishop Movement Logic public boolean moveBishop(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); if (Math.abs(currentX - x) == Math.abs(currentY - y) && isPathClear(currentX, currentY, x, y)) { Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { board.movePiece(currentX, currentY, x, y); return true; } } return false; } //Queen movement logic public boolean moveQueen(int x, int y) { int currentX=piece.getX(); int currentY=piece.getY(); boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear Piece targetPiece= board.getPiece(x, y); //choose the empty space if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece board.movePiece(currentX, currentY, x, y); return true; } } return false; } //King movement logic public boolean moveKing(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); int stepX = Math.abs(x - currentX); //spaces moved horizontally int stepY = Math.abs(y - currentY);//spaces moved vertically if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty o board.movePiece(currentX, currentY, x, y); return true; } } // Castling move MovementCapabilities mc = new MovementCapabilities(); if (mc.castling(piece, x, y, board)) { return true; } return false; } // Path Clearance Logic for Rook and Bishop private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear if (currentX == targetX) { //checks if it moves vertically int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step for (int y = currentY + step; y != targetY; y += step) { // we have a loop in here if (board.getPiece(currentX, y) != null) { return false; // Blocked path } } } else if (currentY == targetY) { //checks if it moves horizontally int step = (targetX > currentX) ? 1 : -1; for (int x = currentX + step; x != targetX; x += step) { if (board.getPiece(x, currentY) != null) { return false; // Blocked path } } } else if (Math.abs(currentX - targetX) == Math.abs(currentY - targetY)) { int stepX = (targetX > currentX) ? 1 : -1; int stepY = (targetY > currentY) ? 1 : -1; int x = currentX + stepX; int y = currentY + stepY; while (x != targetX) { if (board.getPiece(x, y) != null) { return false; } x += stepX; y += stepY; } } return true; // No obstacles found } //adding simulations part for the highlighting the pre-moves that can be performed(only checking where the piece can go to avoid changing the game state) //Pawn movement logic simulation public boolean movePawnSimulate(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); boolean isWhite = piece.isWhite(); int direction = isWhite ? -1 : 1; boolean firstMove = isWhite ? currentY == 6 : currentY == 1; // Same logic as movePawn but no actual move if (x == currentX && y == currentY + direction && board.getPiece(x, y) == null) { return true; }else if (x == currentX && y == currentY + (2 * direction) && firstMove && board.getPiece(x, y) == null && board.getPiece(x, currentY + direction) == null) { return true; }else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) { return true; }return false; } //bishop movement simulate public boolean moveBishopSimulate(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); if (Math.abs(currentX - x) == Math.abs(currentY - y) && isPathClear(currentX, currentY, x, y)) { Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { return true; } } return false; } //Rook movement simulate public boolean moveRookSimulate(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); if (currentX == x || currentY == y) // Check if the move is either in the same row or the same column if (isPathClear(currentX, currentY, x, y)) { //Check if there are obstacles in the path Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //Check if the target is empty or an enemy piece return true; //move was successful } } return false; //if not it was unsuccessful } //Knight movement simulate public boolean moveKnightSimulate(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); int[][] knightMoves = { //we establish all possible moves a knight can have {2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {1, -2}, {-1, 2}, {-1, -2} }; for (int[] move : knightMoves) { // we create a loop through each possible move if (currentX + move[0] == x && currentY + move[1] == y) { // checks if move matches the target coordination (move0 is in x direction, move 1 in y) Piece targetPiece = board.getPiece(x, y); //get the piece on the target spot if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { return true; } } } return false; } //Queen movement simulate public boolean moveQueenSimulate(int x, int y) { int currentX=piece.getX(); int currentY=piece.getY(); boolean Diagonal = Math.abs(currentX - x)== Math.abs(currentY-y); //diagonal spaces identified boolean Straight= currentX == x || currentY == y; //horizontal or vertical spaces identified if((Diagonal || Straight) && isPathClear(currentX, currentY, x, y)) { //if either space is available and clear Piece targetPiece= board.getPiece(x, y); //choose the empty space if(targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move piece to null space or capture opposite colour piece return true; } } return false; } //King movement simulate public boolean moveKingSimulate(int x, int y) { int currentX = piece.getX(); int currentY = piece.getY(); int stepX = Math.abs(x - currentX); //spaces moved horizontally int stepY = Math.abs(y - currentY);//spaces moved vertically if ((stepX <= 1 && stepY <= 1) && !(stepX == 0 && stepY == 0)) { //moving 1 space in any direction Piece targetPiece = board.getPiece(x, y); if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) { //move is space is empty o return true; } } return false; } }