Still working on check and checkmate

This commit is contained in:
manon 2025-05-13 14:08:26 +02:00
parent b56780571b
commit 01fb01c489
3 changed files with 52 additions and 62 deletions

View File

@ -5,13 +5,6 @@ import backend.PieceType;
import windowInterface.MyInterface;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
public class Main {

View File

@ -180,18 +180,17 @@ public class Board {
selectedY = -1;
highlightedSquares.clear();
if (isKingInCheck(true)) {
System.out.println("White is in check!");
}
if (isKingInCheck(false)) {
System.out.println("Black is in check!");
}
if (isCheckmate(true)) {
System.out.println("White is in checkmate!");
}
if (isCheckmate(false)) {
System.out.println("Black is in checkmate!");
// After move completed, check for check and checkmate
for (int i = 0; i < 2; i++) {
boolean isWhite = (i == 0);
if (isKingInCheck(isWhite)) {
System.out.println((isWhite ? "White" : "Black") + " is in check!");
if (isCheckmate(isWhite)) {
System.out.println((isWhite ? "White" : "Black") + " is in checkmate!");
}
}
}
}
@ -338,59 +337,57 @@ public class Board {
boolean kingInCheck = isKingInCheck(whiteKing);
boolean hasEscape = false;
if (kingInCheck) {
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
// 1. If the king is not in check, it's never checkmate
if (!kingInCheck) {
return false;
}
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> rawMoves = getValidMoves(piece);
// 2. Try every possible move of every piece belonging to the checked side
for (int i = 0; i < pieces.size(); i++) {
Piece piece = pieces.get(i);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);
int newX = move[0];
int newY = move[1];
if (piece.isWhite() == whiteKing) {
ArrayList<int[]> rawMoves = getValidMoves(piece);
// Simulate the board
Board simBoard = new Board(this);
for (int j = 0; j < rawMoves.size(); j++) {
int[] move = rawMoves.get(j);
int newX = move[0];
int newY = move[1];
// Find the same piece in the simulated board
Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
// 3. Simulate this move on a copied board
Board simBoard = new Board(this);
// 4. Find the corresponding piece on the cloned board
Piece simPiece = null;
for (int k = 0; k < simBoard.getPieces().size(); k++) {
Piece p = simBoard.getPieces().get(k);
if (p.getX() == piece.getX() && p.getY() == piece.getY()
&& p.getType() == piece.getType()
&& p.isWhite() == piece.isWhite()) {
simPiece = p;
}
}
// 5. Apply the move and check if king is still in check
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
if (simPiece != null) {
Piece captured = simBoard.getPieceAt(newX, newY);
if (captured != null) {
simBoard.getPieces().remove(captured);
}
simPiece.setX(newX);
simPiece.setY(newY);
simPiece.setX(newX);
simPiece.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
if (!stillInCheck) {
hasEscape = true;
}
if (!simBoard.isKingInCheck(whiteKing)) {
hasEscape = true;
}
}
}
}
}
boolean checkmate = false;
if (kingInCheck && !hasEscape) {
checkmate = true;
}
return checkmate;
// 6. If the king is in check and no move avoids it checkmate
return kingInCheck && !hasEscape;
}
}

View File

@ -181,7 +181,7 @@ public class MoveConditions {
if (target == null || target.isWhite() != isWhite) {
Board simBoard = new Board(board);
// Find this same King in the simulated board
// Safely locate the cloned king
Piece simKing = null;
for (int j = 0; j < simBoard.getPieces().size(); j++) {
Piece p = simBoard.getPieces().get(j);
@ -201,8 +201,7 @@ public class MoveConditions {
simKing.setX(newX);
simKing.setY(newY);
boolean stillInCheck = simBoard.isKingInCheck(isWhite);
if (!stillInCheck) {
if (!simBoard.isKingInCheck(isWhite)) {
moves.add(new int[]{newX, newY});
}
}
@ -212,6 +211,7 @@ public class MoveConditions {
return moves;
}
}