remove public static statement

This commit is contained in:
Valentine GIRAL 2025-05-21 16:47:33 +02:00
parent 15fc3a6851
commit 7fe947e4b6
2 changed files with 17 additions and 90 deletions

View File

@ -6,6 +6,7 @@ public class GameResult {
private boolean whiteWon = false;
private boolean blackWon = false;
private String message = "";
private VictoryChecker victoryChecker = new VictoryChecker();
// Checks for game victory after a move is made
public GameResult checkForVictory(Board board) {
@ -15,14 +16,14 @@ public class GameResult {
}
// Check if White has won
if (VictoryChecker.checkVictory(board, true)) {
if (victoryChecker.checkVictory(board, true)) {
gameOver = true;
whiteWon = true;
message = "White wins by checkmate!";
System.out.println(message);
}
// Check if Black has won
else if (VictoryChecker.checkVictory(board, false)) {
else if (victoryChecker.checkVictory(board, false)) {
gameOver = true;
blackWon = true;
message = "Black wins by checkmate!";

View File

@ -4,26 +4,14 @@ import java.util.ArrayList;
public class VictoryChecker {
// For debugging
private static final boolean DEBUG = true;
// Check if a player has won the chess game via checkmate
public static boolean checkVictory(Board board, boolean isWhitePlayer) {
public boolean checkVictory(Board board, boolean isWhitePlayer) {
// The opponent is the one who would be checkmated so opposite color
boolean opponentColor = !isWhitePlayer;
// Debug info
if (DEBUG) {
System.out.println("Checking if " + (isWhitePlayer ? "White" : "Black") + " has won...");
}
// Find the opponent's king
int[] kingPosition = findKingPosition(board, opponentColor);
if (kingPosition == null) {
if (DEBUG) {
System.err.println((opponentColor ? "White" : "Black") + " king not found on the board!");
}
// If opponent's king is not on board, the player has won
return true;
}
@ -31,43 +19,24 @@ public class VictoryChecker {
// Check if opponent's king is in check
boolean kingInCheck = isKingInCheck(board, opponentColor, kingPosition);
if (!kingInCheck) {
if (DEBUG) {
System.out.println((opponentColor ? "White" : "Black") + " king is not in check, no checkmate.");
}
return false;
}
if (DEBUG) {
System.out.println((opponentColor ? "White" : "Black") + " king is in check.");
}
// Check if opponent has any legal moves left
boolean hasLegalMoves = hasLegalMoves(board, opponentColor);
if (hasLegalMoves) {
if (DEBUG) {
System.out.println((opponentColor ? "White" : "Black") + " has legal moves available, no checkmate.");
}
return false;
}
// If the king is in check and there are no legal moves, it's checkmate
if (DEBUG) {
System.out.println("CHECKMATE! " + (isWhitePlayer ? "White" : "Black") + " wins!");
}
return true;
}
// Determine if a player's king is in check
private static boolean isKingInCheck(Board board, boolean isWhite, int[] kingPosition) {
private boolean isKingInCheck(Board board, boolean isWhite, int[] kingPosition) {
// Get all opponent pieces
boolean opponentColor = !isWhite;
if (DEBUG) {
System.out.println("Checking if " + (isWhite ? "White" : "Black") +
" king at [" + kingPosition[0] + "," + kingPosition[1] + "] is in check");
}
for (Piece piece : board.getPieces()) {
if (piece.isWhite() == opponentColor) {
// Get valid moves for this opponent piece
@ -76,10 +45,6 @@ public class VictoryChecker {
for (int[] move : validMoves) {
// If the move targets the king's position, the king is in check
if (move[0] == kingPosition[0] && move[1] == kingPosition[1]) {
if (DEBUG) {
System.out.println("King is attacked by " + piece.getType() +
" at [" + piece.getX() + "," + piece.getY() + "]");
}
return true;
}
}
@ -90,34 +55,18 @@ public class VictoryChecker {
}
// Find the position of a player's king on the board
private static int[] findKingPosition(Board board, boolean isWhite) {
if (DEBUG) {
System.out.println("Looking for " + (isWhite ? "White" : "Black") + " king");
}
private int[] findKingPosition(Board board, boolean isWhite) {
for (Piece piece : board.getPieces()) {
if (piece.getType() == PieceType.King && piece.isWhite() == isWhite) {
if (DEBUG) {
System.out.println("Found king at [" + piece.getX() + "," + piece.getY() + "]");
}
return new int[] {piece.getX(), piece.getY()};
}
}
if (DEBUG) {
System.err.println((isWhite ? "White" : "Black") + " king not found!");
}
return null;
}
// Check if a player has any legal moves available
private static boolean hasLegalMoves(Board board, boolean isWhite) {
if (DEBUG) {
System.out.println("Checking if " + (isWhite ? "White" : "Black") + " has any legal moves");
}
int legalMovesCount = 0;
private boolean hasLegalMoves(Board board, boolean isWhite) {
// For each piece of the player
for (Piece piece : board.getPieces()) {
if (piece.isWhite() == isWhite) {
@ -127,36 +76,21 @@ public class VictoryChecker {
for (int[] move : validMoves) {
// Check if this move would still leave the king in check
if (isLegalMoveWithoutCheck(board, piece, move[0], move[1], isWhite)) {
legalMovesCount++;
if (DEBUG) {
System.out.println("Legal move found: " + piece.getType() +
" from [" + piece.getX() + "," + piece.getY() + "] to [" +
move[0] + "," + move[1] + "]");
// We found at least one legal move, can return early
return true;
}
}
}
}
}
if (DEBUG) {
System.out.println("Total legal moves found: " + legalMovesCount);
}
return legalMovesCount > 0;
return false;
}
// Check if a move is legal considering check rules
private static boolean isLegalMoveWithoutCheck(Board board, Piece piece, int toX, int toY, boolean isWhite) {
private boolean isLegalMoveWithoutCheck(Board board, Piece piece, int toX, int toY, boolean isWhite) {
int fromX = piece.getX();
int fromY = piece.getY();
if (DEBUG) {
System.out.println("Testing move: " + piece.getType() +
" from [" + fromX + "," + fromY + "] to [" + toX + "," + toY + "]");
}
// Create a copy of the board to simulate the move
Board simulatedBoard = new Board(board);
@ -167,20 +101,12 @@ public class VictoryChecker {
int[] kingPos = findKingPosition(simulatedBoard, isWhite);
if (kingPos == null) {
// This shouldn't happen unless the moved piece was the king and it was captured somehow
if (DEBUG) {
System.err.println("King disappeared after simulated move!");
}
return false;
}
// Check if the king is in check after the move
boolean kingInCheck = isKingInCheck(simulatedBoard, isWhite, kingPos);
if (DEBUG && !kingInCheck) {
System.out.println("Move is legal (doesn't leave king in check)");
}
return !kingInCheck;
}
}