Knight rules

This commit is contained in:
Tilman Crosetti 2025-05-13 14:51:28 +02:00
parent 076adb0291
commit 9db0c04eee
1 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package backend;
import java.util.ArrayList;
import java.util.List;
public class Knight extends Piece {
public Knight(boolean isWhite, int x, int y) {
super(isWhite, PieceType.Knight, x, y);
}
@Override
public PieceType getType() {
return PieceType.Knight;
}
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
int[][] offsets = {
{-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 p = board.getPieceAt(r, c);
if (p == null || p.isWhite() != this.isWhite)
moves.add(new Move(this, row, col, r, c));
}
}
return moves;
}
@Override
public Piece clone() {
return new Knight(this.isWhite, this.x, this.y);
}
}