From 7e33ed74ee1b30350994b6d8339e93743594ddec Mon Sep 17 00:00:00 2001 From: Marleen Peterson Date: Fri, 9 May 2025 10:55:21 +0200 Subject: [PATCH] added comments --- src/Main.java | 13 +++++++++ src/backend/Board.java | 55 ++++++++++++++++++++++---------------- src/backend/CheckKing.java | 41 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 23 deletions(-) diff --git a/src/Main.java b/src/Main.java index d7a6ac4..d3ac53c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,5 @@ import backend.Board; +import backend.CheckKing; import windowInterface.MyInterface; public class Main { @@ -11,6 +12,18 @@ public class Main { System.out.println(testBoard.toString()); System.out.println(testBoard.getWidth() + "*" + testBoard.getHeight()); + // Check for checkmate on white and black after setting up the board + boolean isWhiteInCheckmate = CheckKing.isCheckmate(testBoard, true); // Check for white + boolean isBlackInCheckmate = CheckKing.isCheckmate(testBoard, false); // Check for black + + if (isWhiteInCheckmate) { + System.out.println("White is in checkmate!"); + } else if (isBlackInCheckmate) { + System.out.println("Black is in checkmate!"); + } else { + System.out.println("No checkmate detected."); + } + // launches graphical interface : MyInterface mjf = new MyInterface(); mjf.setVisible(true); diff --git a/src/backend/Board.java b/src/backend/Board.java index a728040..ec13274 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -5,33 +5,36 @@ import java.util.HashSet; import java.util.Set; import java.util.Stack; +//The Board class represents the chessboard and manages the game state, including piece positions, +//move history, and turn management. + public class Board { - private int colNum; - private int lineNum; - private Piece[][] board; - private Stack moveHistory = new Stack<>(); - private EnPassant enPassant = new EnPassant(); - private int turnNumber = 0; - private boolean isWhiteTurn = true; + private int colNum; //number of columns in the board + private int lineNum; //number of rows in the board + private Piece[][] board; //matrix representing the board + private Stack moveHistory = new Stack<>(); //stack for move history + private EnPassant enPassant = new EnPassant(); //en passant handler + private int turnNumber = 0; //current turn number + private boolean isWhiteTurn = true; //indicates whose turn it is - private Piece selectedPiece = null; - private Set highlightedPositions = new HashSet<>(); + private Piece selectedPiece = null; //currently selected piece + private Set highlightedPositions = new HashSet<>(); //highlighted moves for the selected piece - public Board(int colNum, int lineNum) { + public Board(int colNum, int lineNum) { //to initialize the board with given dimensions. this.colNum = colNum; this.lineNum = lineNum; this.board = new Piece[lineNum][colNum]; } - public Set getAllMoves(boolean isWhite) { + public Set getAllMoves(boolean isWhite) { //returns all possible moves for white/black Set allMoves = new HashSet<>(); for (Piece piece : getPieces()) { if (piece.isWhite() == isWhite) { allMoves.addAll(getValidMoves(piece, true)); } } - return allMoves; + return allMoves; //set of all valid move positions in the format "x,y" } public int getWidth() { @@ -54,7 +57,8 @@ public class Board { public boolean isTurnWhite() { return isWhiteTurn; } - + + //places a piece on the board at the specified location public void setPiece(boolean isWhite, PieceType type, int x, int y) { board[y][x] = new Piece(x, y, type, isWhite); } @@ -69,21 +73,25 @@ public class Board { setPiece(false, PieceType.Rook, 7, 0); setPiece(true, PieceType.Rook, 0, 7); setPiece(true, PieceType.Rook, 7, 7); + setPiece(false, PieceType.Knight, 1, 0); setPiece(false, PieceType.Knight, 6, 0); setPiece(true, PieceType.Knight, 1, 7); setPiece(true, PieceType.Knight, 6, 7); + setPiece(false, PieceType.Bishop, 2, 0); setPiece(false, PieceType.Bishop, 5, 0); setPiece(true, PieceType.Bishop, 2, 7); setPiece(true, PieceType.Bishop, 5, 7); + setPiece(false, PieceType.Queen, 3, 0); setPiece(true, PieceType.Queen, 3, 7); + setPiece(false, PieceType.King, 4, 0); setPiece(true, PieceType.King, 4, 7); } - public void cleanBoard() { + public void cleanBoard() { //clears the board by setting all positions to null for (int y = 0; y < getHeight(); y++) { //each column for (int x = 0; x < getWidth(); x++) { //each row board[y][x] = null; //starts at 0,0 @@ -93,7 +101,7 @@ public class Board { highlightedPositions.clear(); } - public ArrayList getPieces() { + public ArrayList getPieces() { //returns a list of all pieces currently on the board ArrayList pieces = new ArrayList<>(); for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth(); x++) { @@ -105,7 +113,7 @@ public class Board { return pieces; } - public String toString() { + public String toString() { //provides a string representation of the board for debugging StringBuilder sb = new StringBuilder(); for (int y = 0; y < getHeight(); y++) { for (int x = 0; x < getWidth(); x++) { @@ -150,15 +158,15 @@ public class Board { } } - public boolean isSelected(int x, int y) { + public boolean isSelected(int x, int y) { //if the specified position is the currently selected piece's position return selectedPiece != null && selectedPiece.getX() == x && selectedPiece.getY() == y; } - public boolean isHighlighted(int x, int y) { + public boolean isHighlighted(int x, int y) { //if the specified position is highlighted as a valid move return highlightedPositions.contains(x + "," + y); } - void movePiece(int fromX, int fromY, int toX, int toY) { //from private to not + void movePiece(int fromX, int fromY, int toX, int toY) { //piece from a starting position to a target position Piece moving = board[fromY][fromX]; Piece captured = board[toY][toX]; @@ -189,7 +197,7 @@ public class Board { enPassant.updateTarget(moving, fromY, toY, toX); } - public Set getValidMoves(Piece piece, boolean skipKingCheck) { + public Set getValidMoves(Piece piece, boolean skipKingCheck) { //retrieves all valid moves for a specific piece Set validMoves = new HashSet<>(); int x = piece.getX(); @@ -200,17 +208,17 @@ public class Board { int direction = isWhite ? -1 : 1; switch (type) { - case Pawn: + case Pawn: //standard move if (inBounds(x, y + direction) && board[y + direction][x] == null) { validMoves.add(x + "," + (y + direction)); } - boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1); + boolean onStartRow = (isWhite && y == 6) || (!isWhite && y == 1); //double move if (onStartRow && board[y + direction][x] == null && board[y + 2 * direction][x] == null) { validMoves.add(x + "," + (y + 2 * direction)); } - int[] dx = {-1, 1}; + int[] dx = {-1, 1}; //captures for (int i : dx) { int targetX = x + i; int targetY = y + direction; @@ -222,6 +230,7 @@ public class Board { } } + //en passant int[] epTarget = enPassant.getTarget(); if (epTarget != null && Math.abs(epTarget[0] - x) == 1 && epTarget[1] == y + direction) { validMoves.add(epTarget[0] + "," + epTarget[1]); diff --git a/src/backend/CheckKing.java b/src/backend/CheckKing.java index 7eb94c2..b15fe0b 100644 --- a/src/backend/CheckKing.java +++ b/src/backend/CheckKing.java @@ -61,4 +61,45 @@ public class CheckKing { return legalMoves; } + + // 3. Check if the given color is in checkmate + public static boolean isCheckmate(Board board, boolean white) { + // If the king is not in check, it's not checkmate + if (!isKingInCheck(board, white)) { + return false; + } + + // Check if there are any valid moves for the given color + Piece[][] grid = board.getBoardMatrix(); + + // Iterate through all pieces of the given color + for (int y = 0; y < board.getHeight(); y++) { + for (int x = 0; x < board.getWidth(); x++) { + Piece piece = grid[y][x]; + if (piece != null && piece.isWhite() == white) { + // Get the valid moves for this piece + Set validMoves = board.getValidMoves(piece, true); + + // If any valid move is available that does not leave the king in check, it's not checkmate + for (String move : validMoves) { + String[] parts = move.split(","); + int toX = Integer.parseInt(parts[0]); + int toY = Integer.parseInt(parts[1]); + + // Simulate the move + Board copy = new Board(board); + copy.movePiece(piece.getX(), piece.getY(), toX, toY); + + // Check if the move puts the king in check + if (!isKingInCheck(copy, white)) { + return false; // There's at least one valid move that doesn't leave the king in check + } + } + } + } + } + + // If no valid moves are available, it's checkmate + return true; + } }