Queen movement solved

This commit is contained in:
charles.duteil 2025-05-18 15:05:30 +02:00
parent c084635905
commit 8203f64b63
1 changed files with 20 additions and 6 deletions

View File

@ -11,23 +11,36 @@ 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}, {1, 0}, {0, -1}, {0, 1},
{-1, -1}, {-1, 1}, {1, -1}, {1, 1}
{-1, 0}, // up
{ 1, 0}, // down
{ 0, -1}, // left
{ 0, 1}, // right
{-1, -1}, // up-left
{-1, 1}, // up-right
{ 1, -1}, // down-left
{ 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(r, c)) {
Piece target = board.getPieceAt(r, c);
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 (target.isWhite() != isWhite)
// 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];
}
@ -39,4 +52,5 @@ class Queen extends Piece {
public Piece clone() {
return new Queen(isWhite, x, y);
}
}
}