diff --git a/OOP_3B5_Project/src/backend/Board.java b/OOP_3B5_Project/src/backend/Board.java index f855b0b..da26444 100644 --- a/OOP_3B5_Project/src/backend/Board.java +++ b/OOP_3B5_Project/src/backend/Board.java @@ -246,85 +246,8 @@ public class Board { } } else if (type == PieceType.Bishop) { - int k = 8; // default range value for bishop to cover the board wherever he is - for(int i = 1; i < k; i++) { // range from 1 to k up until the follwing if statement is true - if (getPiece(selectX + i, selectY + i) != null) { // true Iff there is a piece at the specified position which are the direction of the specified piece ! - if (color != getPiece(selectX + i, selectY + i).isWhite()) { // another condition that will be lauched IFF the case has a piece in it and IFF the piece is of a != color than the played piece - k = i+1; // if color is diffrent, change K to i (number of case before a piece) + 1 (if the piece is of a different color i can eat it) - - if (selectX + i == x && selectY + i == y) { // iterates trough i but k = i+1 so the for loop will itterate until i+1 allowing for the opposite color piece to be eaten - isAPieceHere = true; //set the boolean var to true - } - break; // break the loop - - } else { - k = i; // if the condition "if (color != getPiece(selectX + i, selectY + i).isWhite())" is not compliant, this else statement will change k to be i, hence, the same colored piece present in its path will not be eaten - break; - } - } - if(selectX + i == x && selectY + i == y) { // iterate trough i up until i as k = i, it is needed to not light up the pieces of the same color a the played piece - isAPieceHere = true;//set the boolean var to true - } - - - - }k = 8; - for (int i = 1; i < k; i++) { - if (getPiece(selectX - i, selectY - i) != null) { - if (color != getPiece(selectX - i, selectY - i).isWhite()) { - k = i+1; - if (selectX - i == x && selectY - i == y) { - isAPieceHere = true; - } - break; - - }else{ - k = i; - break; - } - } - if (selectX - i == x && selectY - i == y) { - isAPieceHere = true; - } - - }k = 8; - for (int i = 1; i < k; i++) { - if (getPiece(selectX - i, selectY + i) != null) { - if (color != getPiece(selectX - i, selectY + i).isWhite()) { - k = i+1; - if (selectX - i == x && selectY + i == y) { - isAPieceHere = true; - } - break; - - }else{ - k = i; - break; - } - } - if (selectX - i == x && selectY + i == y) { - isAPieceHere = true; - } - } - k = 8; - for (int i = 1; i < k; i++) { - if (getPiece(selectX + i, selectY - i) != null) { - if (color != getPiece(selectX + i, selectY - i).isWhite()) { - k = i+1; - if (selectX + i == x && selectY - i == y) { - isAPieceHere = true; - } - break; - - }else{ - k = i; - break; - } - } - if (selectX + i == x && selectY - i == y) { - isAPieceHere = true; - } - } + // Assuming Move class is instantiated or accessible + isAPieceHere = move.isBishopMoveValid(x, y, color, selectX, selectY); } else if (type == PieceType.Rook) { for(int i = 1; i < 8;i++) { diff --git a/OOP_3B5_Project/src/backend/Move.java b/OOP_3B5_Project/src/backend/Move.java index c3bd5cc..31e311b 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -60,4 +60,83 @@ public class Move { private void pawnPromotion(Piece piece) { piece.setType(PieceType.Queen); } + public boolean isBishopMoveValid(int x, int y, boolean color, int selectX, int selectY) { + int k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX + i, selectY + i) != null) { + if (color != board.getPiece(selectX + i, selectY + i).isWhite()) { + k = i + 1; + if (selectX + i == x && selectY + i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX + i == x && selectY + i == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX - i, selectY - i) != null) { + if (color != board.getPiece(selectX - i, selectY - i).isWhite()) { + k = i + 1; + if (selectX - i == x && selectY - i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX - i == x && selectY - i == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX - i, selectY + i) != null) { + if (color != board.getPiece(selectX - i, selectY + i).isWhite()) { + k = i + 1; + if (selectX - i == x && selectY + i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX - i == x && selectY + i == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX + i, selectY - i) != null) { + if (color != board.getPiece(selectX + i, selectY - i).isWhite()) { + k = i + 1; + if (selectX + i == x && selectY - i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX + i == x && selectY - i == y) { + return true; + } + } + + return false; + } } \ No newline at end of file