Knight movement updated

This commit is contained in:
charles.duteil 2025-05-18 15:12:01 +02:00
parent fbb4ffcddb
commit 837b0a56b5
1 changed files with 15 additions and 10 deletions

View File

@ -10,22 +10,27 @@ class Knight extends Piece {
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
// Knight moves
int[][] offsets = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}
{-2, -1}, {-2, 1},
{-1, -2}, {-1, 2},
{ 1, -2}, { 1, 2},
{ 2, -1}, { 2, 1}
};
for (int[] offset : offsets) {
int r = row + offset[0];
int c = col + offset[1];
if (board.isInBounds(r, c)) {
Piece target = board.getPieceAt(r, c);
int newY = row + offset[0];
int newX = col + offset[1];
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 (target != null) {
moves.add(new Move(this, row, col, newY, newX, target));
} else {
moves.add(new Move(this, row, col, newY, newX));
}
}
}
}