Moves for pawn, rook, knight and bishop are done
This commit is contained in:
parent
edb712f161
commit
cbb87a015e
|
|
@ -10,7 +10,7 @@ public class MovePiece {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Pawn movement logic
|
||||||
public boolean movePawn(int x, int y) { //method definition for pawn
|
public boolean movePawn(int x, int y) { //method definition for pawn
|
||||||
int currentX = piece.getX(); // we call the method getX to get x coordinates
|
int currentX = piece.getX(); // we call the method getX to get x coordinates
|
||||||
int currentY = piece.getY(); // we call the method getY to get y coordinates
|
int currentY = piece.getY(); // we call the method getY to get y coordinates
|
||||||
|
|
@ -32,9 +32,12 @@ public class MovePiece {
|
||||||
else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
|
else if (Math.abs(x - currentX) == 1 && y == currentY + direction && board.getPiece(x, y) != null && board.getPiece(x, y).isWhite() != isWhite) {
|
||||||
board.movePiece(currentX, currentY, x, y); // we go diagonally where another figure is placed
|
board.movePiece(currentX, currentY, x, y); // we go diagonally where another figure is placed
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Rook movement logic
|
||||||
public boolean moveRook(int x, int y) {
|
public boolean moveRook(int x, int y) {
|
||||||
int currentX = piece.getX();
|
int currentX = piece.getX();
|
||||||
int currentY = piece.getY();
|
int currentY = piece.getY();
|
||||||
|
|
@ -54,6 +57,8 @@ public class MovePiece {
|
||||||
|
|
||||||
return false; //if not it was unsuccessful
|
return false; //if not it was unsuccessful
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path Clearance Logic for Rook and Bishop
|
||||||
private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear
|
private boolean isPathClear(int currentX, int currentY, int targetX, int targetY) { //method to check if the path is clear
|
||||||
if (currentX == targetX) { //checks if it moves vertically
|
if (currentX == targetX) { //checks if it moves vertically
|
||||||
int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step
|
int step = (targetY > currentY) ? 1 : -1; // decides the direction of the step
|
||||||
|
|
@ -70,12 +75,76 @@ public class MovePiece {
|
||||||
return false; // Blocked path
|
return false; // Blocked path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else if (Math.abs(currentX - targetX) == Math.abs(currentY - targetY)) {
|
||||||
|
int stepX = (targetX > currentX) ? 1 : -1;
|
||||||
|
int stepY = (targetY > currentY) ? 1 : -1;
|
||||||
|
int x = currentX + stepX;
|
||||||
|
int y = currentY + stepY;
|
||||||
|
while (x != targetX && y != targetY) {
|
||||||
|
if (board.getPiece(x, y) != null) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
x += stepX;
|
||||||
|
y += stepY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true; // No obstacles found
|
return true; // No obstacles found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Knight movement logic
|
||||||
|
public boolean moveKnight(int x, int y) {
|
||||||
|
int currentX = piece.getX();
|
||||||
|
int currentY = piece.getY();
|
||||||
|
|
||||||
|
int[][] knightMoves = { //we establish all possible moves a knight can have
|
||||||
|
{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
|
||||||
|
{1, 2}, {1, -2}, {-1, 2}, {-1, -2}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int[] move : knightMoves) { // we create a loop through each possible move
|
||||||
|
if (currentX + move[0] == x && currentY + move[1] == y) { // checks if move matches the target coordination (move0 is in x direction, move 1 in y)
|
||||||
|
Piece targetPiece = board.getPiece(x, y); //get the piece on the target spot
|
||||||
|
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) {
|
||||||
|
board.movePiece(currentX, currentY, x, y); //move our figure to the target spot
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bishop Movement Logic
|
||||||
|
public boolean moveBishop(int x, int y) {
|
||||||
|
int currentX = piece.getX();
|
||||||
|
int currentY = piece.getY();
|
||||||
|
|
||||||
|
if (Math.abs(currentX - x) == Math.abs(currentY - y) && isPathClear(currentX, currentY, x, y)) {
|
||||||
|
Piece targetPiece = board.getPiece(x, y);
|
||||||
|
if (targetPiece == null || targetPiece.isWhite() != piece.isWhite()) {
|
||||||
|
board.movePiece(currentX, currentY, x, y);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue