This commit is contained in:
Tilman Crosetti 2025-05-13 14:51:11 +02:00
parent bad9ff9dc3
commit 076adb0291
1 changed files with 38 additions and 2 deletions

View File

@ -1,5 +1,41 @@
package backend;
public class King {
import java.util.ArrayList;
import java.util.List;
}
public class King extends Piece {
public King(boolean isWhite, int x, int y) {
super(isWhite, PieceType.King, x, y);
}
@Override
public PieceType getType() {
return PieceType.King;
}
@Override
public List<Move> getLegalMoves(Board board, int row, int col) {
List<Move> moves = new ArrayList<>();
int[] d = {-1, 0, 1};
for (int dr : d) {
for (int dc : d) {
if (dr == 0 && dc == 0) continue;
int r = row + dr;
int c = col + dc;
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 King(this.isWhite, this.x, this.y);
}
}