diff --git a/OOP_3B5_Project/src/backend/Board.java b/OOP_3B5_Project/src/backend/Board.java index da26444..444f6d7 100644 --- a/OOP_3B5_Project/src/backend/Board.java +++ b/OOP_3B5_Project/src/backend/Board.java @@ -246,17 +246,11 @@ public class Board { } } else if (type == PieceType.Bishop) { - // 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++) { - if(selectX == x+i && selectY == y || selectX == x && selectY == y+i || - selectX == x-i && selectY == y || selectX == x && selectY == y-i) { - isAPieceHere = true; - } - } - + } else if (type == PieceType.Rook) { + isAPieceHere = move.isRookMoveValid(x, y, color, selectX, selectY); + } else if (type == PieceType.Queen) { 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 31e311b..e42ed9e 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -60,23 +60,26 @@ public class Move { private void pawnPromotion(Piece piece) { piece.setType(PieceType.Queen); } + + // move method for each piece, everything is explained in the bishop method, it is quite the same for all pieces + 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; + 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 (board.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 != board.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 + return true; //set the boolean var to true } - break; + break; // break the loop } else { - k = i; + 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) { - return true; + 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 + return true; //set the boolean var to true } } @@ -139,4 +142,86 @@ public class Move { return false; } + + // move allowed for rook, everything is explained in the bishop method, it is quite the same for all pieces + + public boolean isRookMoveValid(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) != null) { + if (color != board.getPiece(selectX + i, selectY).isWhite()) { + k = i + 1; + if (selectX + i == x && selectY == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX + i == x && selectY == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX - i, selectY ) != null) { + if (color != board.getPiece(selectX - i, selectY).isWhite()) { + k = i + 1; + if (selectX - i == x && selectY == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX - i == x && selectY == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX , selectY + i) != null) { + if (color != board.getPiece(selectX, selectY + i).isWhite()) { + k = i + 1; + if (selectX == x && selectY + i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX == x && selectY + i == y) { + return true; + } + } + + k = 8; + for (int i = 1; i < k; i++) { + if (board.getPiece(selectX , selectY - i) != null) { + if (color != board.getPiece(selectX , selectY - i).isWhite()) { + k = i + 1; + if (selectX == x && selectY - i == y) { + return true; + } + break; + } else { + k = i; + break; + } + } + if (selectX == x && selectY - i == y) { + return true; + } + } + + return false; + } } \ No newline at end of file