Bishop movement updated

This commit is contained in:
charles.duteil 2025-05-18 15:08:32 +02:00
parent 8203f64b63
commit 1f27eec9aa
2 changed files with 21 additions and 10 deletions

View File

@ -11,20 +11,31 @@ class Bishop extends Piece {
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
int[][] directions = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
// diagonals only
int[][] directions = {
{-1, -1}, // up-left
{-1, 1}, // up-right
{ 1, -1}, // down-left
{ 1, 1} // down-right
};
for (int[] dir : directions) {
int r = row + dir[0];
int c = col + dir[1];
while (board.isInBounds(r, c)) {
Piece target = board.getPieceAt(r, c);
while (board.isInBounds(c, r)) {
Piece target = board.getPieceAt(c, r);
if (target == null) {
moves.add(new Move(this, row, col, r, c));
} else {
if (target.isWhite() != isWhite)
if (target.isWhite() != isWhite) {
moves.add(new Move(this, row, col, r, c, target));
}
break;
}
r += dir[0];
c += dir[1];
}

View File

@ -11,7 +11,7 @@ class Queen extends Piece {
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
// directions: each [dRow, dCol], effectively [dy, dx]
int[][] directions = {
{-1, 0}, // up
{ 1, 0}, // down
@ -23,24 +23,24 @@ class Queen extends Piece {
{ 1, 1} // down-right
};
// For each direction, keep going while in bounds
for (int[] dir : directions) {
int r = row + dir[0];
int c = col + dir[1];
while (board.isInBounds(c, r)) {
Piece target = board.getPieceAt(c, r);
if (target == null) {
// empty square
moves.add(new Move(this, row, col, r, c));
} else {
// if opposite color, can capture
if (target.isWhite() != isWhite) {
moves.add(new Move(this, row, col, r, c, target));
}
// cannot jump beyond this piece
break;
}
// move further in the same direction
r += dir[0];
c += dir[1];
}