rook finished
This commit is contained in:
parent
b11802b832
commit
d62bdf7ad0
|
|
@ -246,16 +246,10 @@ public class Board {
|
||||||
|
|
||||||
}
|
}
|
||||||
} else if (type == PieceType.Bishop) {
|
} else if (type == PieceType.Bishop) {
|
||||||
// Assuming Move class is instantiated or accessible
|
|
||||||
isAPieceHere = move.isBishopMoveValid(x, y, color, selectX, selectY);
|
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 ||
|
} else if (type == PieceType.Rook) {
|
||||||
selectX == x-i && selectY == y || selectX == x && selectY == y-i) {
|
isAPieceHere = move.isRookMoveValid(x, y, color, selectX, selectY);
|
||||||
isAPieceHere = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (type == PieceType.Queen) {
|
} else if (type == PieceType.Queen) {
|
||||||
for(int i = 1; i < 8;i++) {
|
for(int i = 1; i < 8;i++) {
|
||||||
|
|
|
||||||
|
|
@ -60,23 +60,26 @@ public class Move {
|
||||||
private void pawnPromotion(Piece piece) {
|
private void pawnPromotion(Piece piece) {
|
||||||
piece.setType(PieceType.Queen);
|
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) {
|
public boolean isBishopMoveValid(int x, int y, boolean color, int selectX, int selectY) {
|
||||||
int k = 8;
|
int k = 8; // default range value for bishop to cover the board wherever he is
|
||||||
for (int i = 1; i < k; i++) {
|
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) {
|
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()) {
|
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;
|
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) {
|
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;
|
return true; //set the boolean var to true
|
||||||
}
|
}
|
||||||
break;
|
break; // break the loop
|
||||||
} else {
|
} 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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (selectX + i == x && selectY + i == y) {
|
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;
|
return true; //set the boolean var to true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,4 +142,86 @@ public class Move {
|
||||||
|
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue