promotion with the undo !
This commit is contained in:
parent
0d0686b323
commit
3509653392
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ public class Move {
|
|||
public boolean isWhite() { return isWhite; }
|
||||
public PieceType getType() { return type; }
|
||||
public Piece getCapturedPiece() { return capturedPiece; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue