promotion with the undo !

This commit is contained in:
HP 2025-05-17 20:36:12 +02:00
parent 0d0686b323
commit 3509653392
2 changed files with 52 additions and 1 deletions

View File

@ -0,0 +1,50 @@
package backend;
import java.util.ArrayList;
public class CheckManager {
private Board board;
public CheckManager(Board board) {
this.board = board;
}
// Check if the king of a given color is in check
public boolean isKingInCheck(boolean isWhite) {
int[] kingPosition = board.findKingPosition(isWhite); // You must implement this
for (Piece piece : board.getPieces()) {
if (piece.isWhite() != isWhite) {
ArrayList<int[]> enemyMoves = board.computeLegalMoves(piece); // you already have this?
for (int[] move : enemyMoves) {
if (move[0] == kingPosition[0] && move[1] == kingPosition[1]) {
return true;
}
}
}
}
return false;
}
// Filter out illegal moves (those that leave king in check)
public ArrayList<int[]> getLegalMovesFiltered(boolean isWhite) {
ArrayList<int[]> allMoves = board.getAllMoves(isWhite); // You must write this or adapt it
ArrayList<int[]> legalMoves = new ArrayList<>();
for (int[] move : allMoves) {
// board.makeMove(move); // Simulate move
boolean inCheck = isKingInCheck(isWhite);
// board.undoMove(); // Undo move
if (!inCheck) {
legalMoves.add(move);
}
}
return legalMoves;
}
public boolean isCheckmate(boolean isWhite) {
if (!isKingInCheck(isWhite)) return false;
return getLegalMovesFiltered(isWhite).isEmpty();
}
}

View File

@ -24,6 +24,7 @@ public class Move {
public boolean isWhite() { return isWhite; }
public PieceType getType() { return type; }
public Piece getCapturedPiece() { return capturedPiece; }
}