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

@@ -41,6 +41,14 @@ public class Board {
return this.board[x][y]; return this.board[x][y];
} }
public void setPiece(int x, int y, Piece p) {
this.board[x][y] = p;
}
public void setPiece(Position pos, Piece p) {
this.board[pos.x][pos.y] = p;
}
public static Board copy(Board b) { public static Board copy(Board b) {
Board newBoard = new Board(); Board newBoard = new Board();

View File

@@ -35,17 +35,28 @@ public class King extends Piece {
} }
public ArrayList<Position> getLegalMoves(Board board) { 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[] pieces : board.board) {
for (Piece p : pieces) { for (Piece p : pieces) {
if (p == null) continue; if (p == null) continue;
if (!p.colorMatches(this) && !(p instanceof King)) { if (!p.colorMatches(this) && !(p instanceof King)) {
ArrayList<Position> ar = p.getLegalMoves(board); ArrayList<Position> ar = p.getLegalMoves(board);
for (Position posi : ar) { for (Position posi : ar) {
if (this.pos.equals(posi)) return true; if (pos.equals(posi)) return true;
} }
} }
} }