new method checkColor implemented, code is less spaghetti for king move

and knight move
This commit is contained in:
hugomanipoud2 2025-05-21 19:52:20 +02:00
parent 87aaad73e0
commit 189c7cb218
1 changed files with 32 additions and 67 deletions

View File

@ -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;
}
}