diff --git a/src/backend/Board.java b/src/backend/Board.java index ce4ed4b..95213a2 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -121,6 +121,8 @@ public class Board { movementSuccess=movement.moveKing(x, y); }else if (chosenPiece.getType()==PieceType.Queen) { movementSuccess=movement.moveQueen(x, y); + }else if (chosenPiece.getType()==PieceType.Knight) { + movementSuccess=movement.moveKnight(x, y); } if(movementSuccess) { chosenPiece=null; diff --git a/src/backend/MoveHighlighter.java b/src/backend/MoveHighlighter.java index 29a9e5b..8d14150 100644 --- a/src/backend/MoveHighlighter.java +++ b/src/backend/MoveHighlighter.java @@ -7,23 +7,23 @@ public class MoveHighlighter { ArrayList validMoves = new ArrayList(); MovePiece movement = new MovePiece(piece, board); PieceType type= piece.getType(); - - for(int x=0; x 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 && y != targetY) { - if (board.getPiece(x, y) != null) { - return false; - } - x += stepX; - y += stepY; - } - } - - return true; // No obstacles found - } - //Knight movement logic public boolean moveKnight(int x, int y) { int currentX = piece.getX(); @@ -120,7 +122,6 @@ public class MovePiece { } return false; } - // Bishop Movement Logic public boolean moveBishop(int x, int y) { int currentX = piece.getX(); @@ -142,7 +143,7 @@ public class MovePiece { 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(currentX, currentY); //choose the empty space + 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; @@ -167,7 +168,45 @@ public class MovePiece { } return false; } - //Pawn movement logic simulation: only checking where the piece can go to avoid changing the game state + + // 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 && y != targetY) { + 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(); @@ -178,35 +217,101 @@ public class MovePiece { // 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 && + }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 && + }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; } } - - - /* public void addPossibleMoves() { - switch (piece.getType()) { - case Pawn:highlightManager.highlightPawnMoves(piece); - case Rook:highlightManager.highlightRookMoves(piece); - case Knight:highlightManager.highlightKnightMoves(piece); - case Bishop:highlightManager.highlightBishopMoves(piece); - case Queen:highlightManager.highlightQueenMoves(piece); - case King:highlightManager.highlightKingMoves(piece); - default:System.out.println("Unknown piece type"); - } - } -} - - */ + +