king movement updated

This commit is contained in:
charles.duteil 2025-05-18 15:11:07 +02:00
parent 1f27eec9aa
commit fbb4ffcddb
1 changed files with 15 additions and 11 deletions

View File

@ -10,19 +10,23 @@ class King extends Piece {
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
// row = current Y, col = current X
List<Move> moves = new ArrayList<>();
for (int dr = -1; dr <= 1; dr++) {
for (int dc = -1; dc <= 1; dc++) {
if (dr == 0 && dc == 0) continue;
int r = row + dr;
int c = col + dc;
if (board.isInBounds(r, c)) {
Piece target = board.getPieceAt(r, c);
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
if (dy == 0 && dx == 0) continue;
int newY = row + dy;
int newX = col + dx;
// Check bounds with (x, y)
if (board.isInBounds(newX, newY)) {
Piece target = board.getPieceAt(newX, newY);
if (target == null || target.isWhite() != isWhite) {
if (target != null)
moves.add(new Move(this, row, col, r, c, target));
else
moves.add(new Move(this, row, col, r, c));
// If there's a target, it's a capture
if (target != null) {
moves.add(new Move(this, row, col, newY, newX, target));
} else {
moves.add(new Move(this, row, col, newY, newX));
}
}
}
}