game over written in console if king selected

This commit is contained in:
Jérôme BEDIER 2025-05-22 22:07:48 +02:00
parent ee4b64f6e1
commit 3b41d77eea
1 changed files with 11 additions and 2 deletions

View File

@ -2,6 +2,9 @@ package backend;
import java.util.ArrayList;
import java.util.Stack;
import javax.swing.JLabel;
import javax.swing.JPanel;
/*Task: Chess Game Board Implementation
Class name: Board.java
@ -316,7 +319,7 @@ public class Board {
}
}
private ArrayList<Move> getLegalMoves(Piece piece) {
public ArrayList<Move> getLegalMoves(Piece piece) {
ArrayList<Move> moves = new ArrayList<>();
int x = piece.getX();
int y = piece.getY();
@ -611,17 +614,22 @@ public class Board {
}
private void highlightKingInCheck() {
int highlightedCount = 0;
// Check if white king is in check
Piece whiteKing = findKing(true);
if (whiteKing != null && isKingInCheck(true)) {
highlightedSquares[whiteKing.getX()][whiteKing.getY()] = true;
}
//check if white king checkmate
// Check if black king is in check
Piece blackKing = findKing(false);
if (blackKing != null && isKingInCheck(false)) {
highlightedSquares[blackKing.getX()][blackKing.getY()] = true;
}
} // check
public boolean canSelectPieceWhenInCheck(int x, int y) {
@ -683,7 +691,7 @@ public class Board {
private void filterMovesForCheck() {
Piece selectedPiece = board[selectedX][selectedY];
boolean[][] newHighlights = new boolean[width][height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (highlightedSquares[x][y]) {
@ -698,6 +706,7 @@ public class Board {
// If moving the king, check if the destination square is attacked
if (selectedPiece.getType() == PieceType.King) {
leavesKingInCheck = isSquareAttacked(x, y, !selectedPiece.isWhite());
System.out.println("Game Over"); // only work if king selected
}
else {