diff --git a/OOP_3B6_Project/src/backend/AutoPlayer.java b/OOP_3B6_Project/src/backend/AutoPlayer.java index df8ac0e..f6d1d5b 100644 --- a/OOP_3B6_Project/src/backend/AutoPlayer.java +++ b/OOP_3B6_Project/src/backend/AutoPlayer.java @@ -4,7 +4,7 @@ import java.util.ArrayList; import java.util.Random; public class AutoPlayer { - private final int MAX_DEPTH = 3; // Depth of search + private final int MAX_DEPTH = 4; // Depth of search private final Random random = new Random(); /** @@ -13,6 +13,11 @@ public class AutoPlayer { * @return */ public Move computeBestMove(Board board) { + + if (isCheckmate(board)) { + return null; // no move if checkmate + } + ArrayList possibleMoves = generateAllPossibleMoves(board); if (possibleMoves.isEmpty()) { @@ -40,15 +45,54 @@ public class AutoPlayer { return bestMove; } + /** + * Vérifie si la position actuelle est un échec et mat + * @param board + * @return true si c'est échec et mat, false sinon + */ + private boolean isCheckmate(Board board) { + boolean isWhiteTurn = board.isTurnWhite(); + + // no move + check = checkmate + ArrayList possibleMoves = generateAllPossibleMoves(board); + + if (possibleMoves.isEmpty()) { + + Piece king = null; + for (Piece piece : board.getPieces()) { + if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) { + king = piece; + break; + } + } + + if (king != null) { + + for (Piece piece : board.getPieces()) { + if (piece.isWhite() != isWhiteTurn) { + ArrayList enemyMoves = computeLegalMoves(board, piece); + for (int[] move : enemyMoves) { + if (move[0] == king.getX() && move[1] == king.getY()) { + return true; // Le roi est en échec et aucun mouvement possible -> échec et mat + } + } + } + } + } + } + + return false; + } + private int negamax(Board board, int depth, int alpha, int beta) { - // If we've reached the maximum depth or the game is over, evaluate the board - if (depth == 0) { + + if (depth == 0 || isCheckmate(board)) { return evaluateBoard(board); } ArrayList possibleMoves = generateAllPossibleMoves(board); - // If no moves are available, this might be checkmate or stalemate + if (possibleMoves.isEmpty()) { return -1000; // Heavily penalize positions with no moves } @@ -239,6 +283,11 @@ public class AutoPlayer { int score = 0; boolean isWhiteTurn = board.isTurnWhite(); + + if (isCheckmate(board)) { + return isWhiteTurn ? -20000 : 20000; // bonus checkmate + } + // Material value for (Piece piece : board.getPieces()) { int pieceValue = getPieceValue(piece.getType()); @@ -264,8 +313,8 @@ public class AutoPlayer { private int getPieceValue(PieceType type) { switch (type) { case Pawn: return 100; - case Knight: return 320; - case Bishop: return 330; + case Knight: return 300; + case Bishop: return 300; case Rook: return 500; case Queen: return 900; case King: return 20000; // Very high value to prioritize king safety @@ -323,7 +372,7 @@ public class AutoPlayer { break; case King: - // Kings are safer at the edge in the middlegame + // Kings are safer at the edge if ((x <= 1 || x >= 6) && y <= 1) { bonus += 30; } @@ -333,4 +382,4 @@ public class AutoPlayer { // Adjust sign based on whose piece it is return piece.isWhite() == isWhiteTurn ? bonus : -bonus; } -} +} \ No newline at end of file diff --git a/OOP_3B6_Project/src/backend/Board.java b/OOP_3B6_Project/src/backend/Board.java index 943eb58..732f697 100644 --- a/OOP_3B6_Project/src/backend/Board.java +++ b/OOP_3B6_Project/src/backend/Board.java @@ -224,11 +224,11 @@ public class Board { // Check for castling if (selectedX == 4 && selectedY == 7) { - if (x == 6 && y == 7) { // King-side castling + if (x == 6 && y == 7) { // small castling // Move the rook too removePieceAt(7, 7); pieces.add(new Piece(5, 7, true, PieceType.Rook)); - } else if (x == 2 && y == 7) { // Queen-side castling + } else if (x == 2 && y == 7) { // big castling // Move the rook too removePieceAt(0, 7); pieces.add(new Piece(3, 7, true, PieceType.Rook)); @@ -239,11 +239,11 @@ public class Board { // Check for castling if (selectedX == 4 && selectedY == 0) { - if (x == 6 && y == 0) { // King-side castling + if (x == 6 && y == 0) { // small castling // Move the rook too removePieceAt(7, 0); pieces.add(new Piece(5, 0, false, PieceType.Rook)); - } else if (x == 2 && y == 0) { // Queen-side castling + } else if (x == 2 && y == 0) { // big castling // Move the rook too removePieceAt(0, 0); pieces.add(new Piece(3, 0, false, PieceType.Rook)); @@ -631,12 +631,11 @@ public class Board { } // Castling - if (isWhite && !whiteKingMoved && x == 4 && y == 7) { - // King-side castling + if (isWhite && !whiteKingMoved) { + // small castling if (!whiteRookKingSideMoved && getPieceAt(5, 7) == null && - getPieceAt(6, 7) == null && - getPieceAt(7, 7) != null && + getPieceAt(6, 7) == null && getPieceAt(7, 7).getType() == PieceType.Rook) { // Check if king would pass through check @@ -647,7 +646,7 @@ public class Board { } } - // Queen-side castling + // big castling if (!whiteRookQueenSideMoved && getPieceAt(1, 7) == null && getPieceAt(2, 7) == null && diff --git a/OOP_3B6_Project/src/windowInterface/MyInterface.java b/OOP_3B6_Project/src/windowInterface/MyInterface.java index 8fb00df..91e9658 100644 --- a/OOP_3B6_Project/src/windowInterface/MyInterface.java +++ b/OOP_3B6_Project/src/windowInterface/MyInterface.java @@ -1,15 +1,10 @@ package windowInterface; import java.awt.BorderLayout; -import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JPanel; -import javax.swing.SwingConstants; import javax.swing.border.EmptyBorder; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ChangeListener; - import backend.Game; import javax.swing.JButton; @@ -24,10 +19,6 @@ import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.awt.event.ActionEvent; -import javax.swing.JList; -import javax.swing.AbstractListModel; -import javax.swing.JToggleButton; -import javax.swing.JRadioButton; import javax.swing.JCheckBox; public class MyInterface extends JFrame { @@ -36,7 +27,6 @@ public class MyInterface extends JFrame { private JPanel contentPane; private JLabel turnLabel; private JLabel borderLabel; - private JLabel speedLabel; private JPanelChessBoard panelDraw; private Game game; private JLabel actionLabel; @@ -199,7 +189,6 @@ public class MyInterface extends JFrame { try { BufferedReader fileContent = new BufferedReader(new FileReader(fileName)); String line = fileContent.readLine(); - int colorID = 0; while (line != null) { lines.add(line); line = fileContent.readLine();