Still working on check and checkmate
This commit is contained in:
parent
b56780571b
commit
01fb01c489
|
|
@ -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 {
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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!");
|
||||
}
|
||||
// After move completed, check for check and checkmate
|
||||
for (int i = 0; i < 2; i++) {
|
||||
boolean isWhite = (i == 0);
|
||||
|
||||
if (isCheckmate(true)) {
|
||||
System.out.println("White is in checkmate!");
|
||||
if (isKingInCheck(isWhite)) {
|
||||
System.out.println((isWhite ? "White" : "Black") + " is in check!");
|
||||
|
||||
if (isCheckmate(isWhite)) {
|
||||
System.out.println((isWhite ? "White" : "Black") + " is in checkmate!");
|
||||
}
|
||||
}
|
||||
if (isCheckmate(false)) {
|
||||
System.out.println("Black is in checkmate!");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -338,7 +337,12 @@ public class Board {
|
|||
boolean kingInCheck = isKingInCheck(whiteKing);
|
||||
boolean hasEscape = false;
|
||||
|
||||
if (kingInCheck) {
|
||||
// 1. If the king is not in check, it's never checkmate
|
||||
if (!kingInCheck) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
|
|
@ -350,10 +354,10 @@ public class Board {
|
|||
int newX = move[0];
|
||||
int newY = move[1];
|
||||
|
||||
// Simulate the board
|
||||
// 3. Simulate this move on a copied board
|
||||
Board simBoard = new Board(this);
|
||||
|
||||
// Find the same piece in the simulated board
|
||||
// 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);
|
||||
|
|
@ -364,6 +368,7 @@ public class Board {
|
|||
}
|
||||
}
|
||||
|
||||
// 5. Apply the move and check if king is still in check
|
||||
if (simPiece != null) {
|
||||
Piece captured = simBoard.getPieceAt(newX, newY);
|
||||
if (captured != null) {
|
||||
|
|
@ -373,24 +378,16 @@ public class Board {
|
|||
simPiece.setX(newX);
|
||||
simPiece.setY(newY);
|
||||
|
||||
boolean stillInCheck = simBoard.isKingInCheck(whiteKing);
|
||||
|
||||
if (!stillInCheck) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
}
|
||||
}
|
||||
|
|
@ -214,4 +213,5 @@ public class MoveConditions {
|
|||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue