parent
0ffa47ab0c
commit
e41f775d32
|
|
@ -0,0 +1,97 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class KingCheck {
|
||||||
|
public ArrayList<ArrayList<Boolean>> getLegalMoves(Piece piece, ArrayList<ArrayList<Piece>> board) {
|
||||||
|
ArrayList<ArrayList<Boolean>> rawMoves = piece.getPossibleMoves(board);
|
||||||
|
|
||||||
|
ArrayList<ArrayList<Boolean>> legalMoves = new ArrayList<>();
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
ArrayList<Boolean> row = new ArrayList<>();
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
row.add(false);
|
||||||
|
}
|
||||||
|
legalMoves.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
if (rawMoves.get(y).get(x)) {
|
||||||
|
Move move = new Move(piece.getX(), piece.getY(), x, y);
|
||||||
|
if (isMoveSafe(board, move, piece.isWhite())) {
|
||||||
|
legalMoves.get(y).set(x, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return legalMoves;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isKingInCheck(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn) {
|
||||||
|
int kingX = -1, kingY = -1;
|
||||||
|
|
||||||
|
// Find the king
|
||||||
|
for (ArrayList<Piece> row : board) {
|
||||||
|
for (Piece p : row) {
|
||||||
|
if (p != null && p.isWhite() == isWhiteTurn && p.getType() == PieceType.King) {
|
||||||
|
kingX = p.getX();
|
||||||
|
kingY = p.getY();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kingX == -1 || kingY == -1) {
|
||||||
|
return true; // King not found, technically in check
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if any enemy piece can capture the king
|
||||||
|
for (ArrayList<Piece> row : board) {
|
||||||
|
for (Piece p : row) {
|
||||||
|
if (p != null && p.isWhite() != isWhiteTurn) {
|
||||||
|
ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board);
|
||||||
|
if (moves.get(kingY).get(kingX)) {
|
||||||
|
return true; // King can be captured
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false; // King is safe
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isMoveSafe(ArrayList<ArrayList<Piece>> board, Move move, boolean isWhiteTurn) {
|
||||||
|
ArrayList<ArrayList<Piece>> simulatedBoard = simulateMove(board, move);
|
||||||
|
return !isKingInCheck(simulatedBoard, isWhiteTurn);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayList<ArrayList<Piece>> simulateMove(ArrayList<ArrayList<Piece>> board, Move move) {
|
||||||
|
ArrayList<ArrayList<Piece>> newBoard = deepCopyBoard(board);
|
||||||
|
|
||||||
|
Piece piece = newBoard.get(move.fromY).get(move.fromX);
|
||||||
|
newBoard.get(move.fromY).set(move.fromX, null);
|
||||||
|
piece.x = move.toX;
|
||||||
|
piece.y = move.toY;
|
||||||
|
newBoard.get(move.toY).set(move.toX, piece);
|
||||||
|
|
||||||
|
return newBoard;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayList<ArrayList<Piece>> deepCopyBoard(ArrayList<ArrayList<Piece>> original) {
|
||||||
|
ArrayList<ArrayList<Piece>> copy = new ArrayList<>();
|
||||||
|
for (ArrayList<Piece> row : original) {
|
||||||
|
ArrayList<Piece> newRow = new ArrayList<>();
|
||||||
|
for (Piece p : row) {
|
||||||
|
if (p != null) {
|
||||||
|
newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
||||||
|
} else {
|
||||||
|
newRow.add(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copy.add(newRow);
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue