From 189c7cb218a1d7774638641116ca08012bb80ac5 Mon Sep 17 00:00:00 2001 From: hugomanipoud2 Date: Wed, 21 May 2025 19:52:20 +0200 Subject: [PATCH] new method checkColor implemented, code is less spaghetti for king move and knight move --- OOP_3B5_Project/src/backend/Move.java | 99 +++++++++------------------ 1 file changed, 32 insertions(+), 67 deletions(-) diff --git a/OOP_3B5_Project/src/backend/Move.java b/OOP_3B5_Project/src/backend/Move.java index 4c9167f..8dcae1d 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -233,67 +233,41 @@ public class Move { if (selectX == x + 1 && selectY == y + 2) { // check one of the 8 possible cases - - if(board.getPiece(x, y) != null) { // check at the exact tox and ToY coordinates if there is a piece - return color != board.getPiece(x, y).isWhite(); // no need for additional if statement compared to previous move methods, its a comparator in between the color of the played piece and the color of the piece at the destination, if different, it will return true (possibility to eat the piecebc ishighlighted will be true) , if the same it will return false - } - return true; + return checkColor(x, y, color); // go see chechColor for more informations } if (selectX == x - 1 && selectY == y - 2) { - - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x - 1 && selectY == y + 2) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x + 1 && selectY == y - 2) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x + 2 && selectY == y + 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x - 2 && selectY == y - 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x - 2 && selectY == y + 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x + 2 && selectY == y - 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } return false; @@ -304,67 +278,58 @@ public boolean isKingMoveValid(int x, int y, boolean color, int selectX, int se if (selectX == x + 1 && selectY == y + 1) { // check one of the 8 possible cases - if(board.getPiece(x, y) != null) { // check at the exact tox and ToY coordinates if there is a piece - return color != board.getPiece(x, y).isWhite(); // no need for additional if statement compared to previous move methods, its a comparator in between the color of the played piece and the color of the piece at the destination, if different, it will return true (possibility to eat the piecebc ishighlighted will be true) , if the same it will return false - } - return true; + return checkColor(x, y, color); // go see checkColor for more infos } if (selectX == x - 1 && selectY == y - 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x - 1 && selectY == y + 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x + 1 && selectY == y - 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x && selectY == y + 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x && selectY == y - 1) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } if (selectX == x - 1 && selectY == y) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + return checkColor(x, y, color); } - if (selectX == x + 1 && selectY == y) { - if(board.getPiece(x, y) != null) { - return color != board.getPiece(x, y).isWhite(); - } - return true; + if (selectX == x + 1 && selectY == y) { + return checkColor(x, y, color); } return false; } + + + private boolean checkColor(int x, int y, boolean color) { + + if(board.getPiece(x, y) != null) { // check at the exact tox and ToY coordinates if there is a piece + return color != board.getPiece(x, y).isWhite(); // no need for additional if statement compared to rook and bishop move methods, its a comparator in between the color of the played piece and the color of the piece at the destination (x,y) , if they are different (so if the comparison is true), it will return true (possibility to eat the piece bc ishighlighted will be true) , if of the same color, return false + } + return true; + + + + + + +} } \ No newline at end of file