This commit is contained in:
CT
2026-04-24 15:44:13 -05:00
parent c3318f834c
commit 3e96095c66
2 changed files with 22 additions and 3 deletions

View File

@@ -35,17 +35,28 @@ public class King extends Piece {
}
public ArrayList<Position> getLegalMoves(Board board) {
return null;
ArrayList<Position> positions = getPseudoLegalMoves(board);
for (Position p : new ArrayList<>(positions)) {
Piece tempPiece = board.getPiece(p);
board.setPiece(pos, tempPiece);
board.setPiece(p, this);
if (inCheck(board, p)) {
positions.remove(p);
}
board.setPiece(p, tempPiece);
board.setPiece(pos, this);
}
return positions;
}
public boolean inCheck(Board board) {
public boolean inCheck(Board board, Position pos) {
for (Piece[] pieces : board.board) {
for (Piece p : pieces) {
if (p == null) continue;
if (!p.colorMatches(this) && !(p instanceof King)) {
ArrayList<Position> ar = p.getLegalMoves(board);
for (Position posi : ar) {
if (this.pos.equals(posi)) return true;
if (pos.equals(posi)) return true;
}
}
}