check maths
This commit is contained in:
parent
57e75dc5f5
commit
e518dc3d3c
Binary file not shown.
|
|
@ -344,37 +344,19 @@ public class Board {
|
|||
highlightedPositions.clear();
|
||||
highlightedPositions.addAll(validMoves);
|
||||
}
|
||||
// Calculate valid moves for a piece
|
||||
private Set<Position> getValidMoves(Piece piece) {
|
||||
Set<Position> validMoves = new HashSet<>();
|
||||
// Get all possible moves without considering check
|
||||
Set<Position> candidateMoves = getRawValidMoves(piece);
|
||||
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
addPawnMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Rook:
|
||||
addRookMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Knight:
|
||||
addKnightMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Bishop:
|
||||
addBishopMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case Queen:
|
||||
addRookMoves(validMoves, x, y, isWhite);
|
||||
addBishopMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
case King:
|
||||
addKingMoves(validMoves, x, y, isWhite);
|
||||
break;
|
||||
// Filter out moves that would leave king in check
|
||||
Set<Position> legalMoves = new HashSet<>();
|
||||
for (Position move : candidateMoves) {
|
||||
if (!wouldMoveLeaveKingInCheck(piece, move.x, move.y)) {
|
||||
legalMoves.add(move);
|
||||
}
|
||||
}
|
||||
|
||||
return validMoves;
|
||||
return legalMoves;
|
||||
}
|
||||
|
||||
// Add valid pawn moves
|
||||
|
|
@ -522,6 +504,92 @@ public class Board {
|
|||
}
|
||||
}
|
||||
}
|
||||
// Add this method to check if a king is in check
|
||||
private boolean isKingInCheck(boolean isWhiteKing) {
|
||||
// Find the king's position
|
||||
Piece king = null;
|
||||
for (Piece p : pieces) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhiteKing) {
|
||||
king = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (king == null) return false;
|
||||
|
||||
// Check if any opponent piece can attack the king
|
||||
for (Piece p : pieces) {
|
||||
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
|
||||
|
||||
// Get raw moves without check validation
|
||||
Set<Position> attackMoves = getRawValidMoves(p);
|
||||
|
||||
// If any piece can move to king's position, king is in check
|
||||
if (attackMoves.contains(new Position(king.getX(), king.getY()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add this method to simulate a move and check if it leaves king in check
|
||||
private boolean wouldMoveLeaveKingInCheck(Piece piece, int newX, int newY) {
|
||||
// Create temporary board to simulate move
|
||||
Board tempBoard = new Board(this);
|
||||
|
||||
// Find the piece in the temp board
|
||||
Piece tempPiece = null;
|
||||
for (Piece p : tempBoard.pieces) {
|
||||
if (p.getX() == piece.getX() && p.getY() == piece.getY()) {
|
||||
tempPiece = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (tempPiece == null) return true; // Safety check
|
||||
|
||||
// Remove any piece at destination
|
||||
Piece capturedPiece = tempBoard.getPieceAt(newX, newY);
|
||||
if (capturedPiece != null) {
|
||||
tempBoard.pieces.remove(capturedPiece);
|
||||
}
|
||||
|
||||
// Move the piece
|
||||
tempPiece.moveTo(newX, newY);
|
||||
|
||||
// Check if king is in check after move
|
||||
return tempBoard.isKingInCheck(piece.isWhite());
|
||||
}
|
||||
|
||||
// Add this helper method to get moves without check validation
|
||||
private Set<Position> getRawValidMoves(Piece piece) {
|
||||
Set<Position> moves = new HashSet<>();
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
addPawnMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
case Rook:
|
||||
addRookMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
case Knight:
|
||||
addKnightMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
case Bishop:
|
||||
addBishopMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
case Queen:
|
||||
addRookMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
addBishopMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
case King:
|
||||
addKingMoves(moves, piece.getX(), piece.getY(), piece.isWhite());
|
||||
break;
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
public void playMove(Move move) {
|
||||
//TODO
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue