From 2ab662f39497dbe91a6971900a87da0567ab0f3d Mon Sep 17 00:00:00 2001 From: "MSI-AB\\antoineB" Date: Fri, 16 May 2025 09:06:32 +0200 Subject: [PATCH 1/3] correct errors --- src/backend/Board.java | 72 +++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index a55b731..d3f34ed 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -13,6 +13,7 @@ public class Board { private Integer selectedY = null; private ArrayList highlightedSquares = new ArrayList<>(); private ArrayList moveHistory = new ArrayList<>(); + private int[] enPassantTarget = null; /*public Board(int colNum, int lineNum) { @@ -203,8 +204,38 @@ public class Board { selectedX = null; selectedY = null; highlightedSquares.clear(); + + // Check for en passant capture BEFORE updating enPassantTarget! + if (selectedPiece.getType() == PieceType.Pawn && + enPassantTarget != null && + x == enPassantTarget[0] && + y == enPassantTarget[1]) { + + int capturedY = selectedPiece.isWhite() ? y - 1 : y + 1; + Piece capturedPawn = getPieceAt(x, capturedY); + + // Validate that it's an enemy pawn that just moved two steps + if (capturedPawn != null && + capturedPawn.getType() == PieceType.Pawn && + capturedPawn.isWhite() != selectedPiece.isWhite()) { + + // Remove the pawn captured en passant + pieces.remove(capturedPawn); + } + } + + + // Update enPassantTarget + if (selectedPiece.getType() == PieceType.Pawn && selectedY != null && Math.abs(y - selectedY) == 2) { + int middleY = (y + selectedY) / 2; + enPassantTarget = new int[]{x, middleY}; + } else { + enPassantTarget = null; + } + } + public boolean isSelected(int x, int y) { return selectedX != null && selectedY != null && selectedX == x && selectedY == y; } @@ -416,19 +447,18 @@ public ArrayList computeLegalMoves(Piece piece) { PieceType type = piece.getType(); if (type == PieceType.Pawn) { - int dir = piece.isWhite() ? -1 : 1; // reverse the -1 and 1 to match the board + int dir = piece.isWhite() ? -1 : 1; int nextY = y + dir; - boolean isPromoting =(piece.isWhite() && nextY == 7)||(!piece.isWhite() && nextY==0); - + // Move forward if square is empty if (isEmpty(x, nextY)) { moves.add(new int[]{x, nextY}); - - // Check for first move double step - int startRow = piece.isWhite()? 6:1; // same here i reversed 6 and 1 + + // First move double step + int startRow = piece.isWhite() ? 6 : 1; int twoStepsY = y + 2 * dir; - if (y== startRow && isEmpty(x,twoStepsY)) { - moves.add(new int[] {x, twoStepsY}); + if (y == startRow && isEmpty(x, twoStepsY)) { + moves.add(new int[]{x, twoStepsY}); } } @@ -439,15 +469,23 @@ public ArrayList computeLegalMoves(Piece piece) { if (isEnemy(x + 1, nextY, piece.isWhite())) { moves.add(new int[]{x + 1, nextY}); } - //promotion à revoir ca marche pas - if (isPromoting) { - piece.setType(PieceType.Queen); - piece.setY(nextY); + + // En passant + if (enPassantTarget != null) { + int targetX = enPassantTarget[0]; + int targetY = enPassantTarget[1]; + if (nextY == targetY && Math.abs(x - targetX) == 1) { + moves.add(new int[]{targetX, targetY}); } - // en passant - } + // Promotion check (you shouldn't promote here — only on move execution) + // Do NOT change piece type inside this method + } + + + + if (type == PieceType.Rook) { // Directions : haut, bas, gauche, droite @@ -538,9 +576,13 @@ public ArrayList computeLegalMoves(Piece piece) { } } } + } - + + return moves; + + } } From 89c8f689dd4fc0b4afaf06a09f72b915387ac14f Mon Sep 17 00:00:00 2001 From: "g.levassor--gentil" Date: Fri, 16 May 2025 09:30:28 +0200 Subject: [PATCH 2/3] autoplayer upgraded 2 moves ahead --- src/backend/AutoPlayer.java | 86 +++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 27 deletions(-) diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index 1d15d8e..ea326e6 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -4,13 +4,69 @@ import java.util.ArrayList; public class AutoPlayer { + private static final int MAX_DEPTH = 2; // You can increase this for stronger AI + public Move computeBestMove(Board board) { + return minimax(board, MAX_DEPTH, board.isTurnWhite()).bestMove; + } + + private static class ScoredMove { + Move bestMove; + int score; + + ScoredMove(Move move, int score) { + this.bestMove = move; + this.score = score; + } + } + + private ScoredMove minimax(Board board, int depth, boolean isMaximizing) { + if (depth == 0) { + return new ScoredMove(null, evaluateBoard(board)); + } + + ArrayList possibleMoves = generateAllMoves(board, isMaximizing); + if (possibleMoves.isEmpty()) { + return new ScoredMove(null, evaluateBoard(board)); + } + + Move bestMove = null; + int bestScore = isMaximizing ? Integer.MIN_VALUE : Integer.MAX_VALUE; + + for (Move move : possibleMoves) { + Board boardCopy = new Board(board); // Deep copy + boardCopy.playMove(move); + + int score = minimax(boardCopy, depth - 1, !isMaximizing).score; + + if (isMaximizing && score > bestScore) { + bestScore = score; + bestMove = move; + } else if (!isMaximizing && score < bestScore) { + bestScore = score; + bestMove = move; + } + } + + return new ScoredMove(bestMove, bestScore); + } + + private int evaluateBoard(Board board) { + int score = 0; + for (Piece p : board.getPieces()) { + int value = pieceValue(p.getType()); + score += p.isWhite() ? value : -value; + } + return score; + } + + private ArrayList generateAllMoves(Board board, boolean isWhite) { ArrayList allMoves = new ArrayList<>(); - boolean isWhite = board.isTurnWhite(); for (Piece piece : board.getPieces()) { if (piece.isWhite() != isWhite) continue; - ArrayList legalMoves = getLegalMoves(board, piece); + + ArrayList legalMoves = board.computeLegalMoves(piece); for (int[] move : legalMoves) { Piece target = getPieceAt(board, move[0], move[1]); Move candidate = new Move( @@ -23,26 +79,7 @@ public class AutoPlayer { } } - // Evaluate moves - Move bestMove = null; - int bestScore = Integer.MIN_VALUE; - - for (Move move : allMoves) { - int score = evaluateMove(move); - if (score > bestScore) { - bestScore = score; - bestMove = move; - } - } - - return bestMove; - } - - private int evaluateMove(Move move) { - if (move.getCapturedPiece() != null) { - return pieceValue(move.getCapturedPiece().getType()); - } - return 0; + return allMoves; } private int pieceValue(PieceType type) { @@ -57,11 +94,6 @@ public class AutoPlayer { } } - private ArrayList getLegalMoves(Board board, Piece piece) { - // Reuse board's internal method - // But we must simulate access: simulate selection, compute, and reset - return board.computeLegalMoves(piece); - } private Piece getPieceAt(Board board, int x, int y) { for (Piece p : board.getPieces()) { From 6628ecdb12b495f67bccca0ba5d66d0346fc833b Mon Sep 17 00:00:00 2001 From: "MSI-AB\\antoineB" Date: Fri, 16 May 2025 09:55:29 +0200 Subject: [PATCH 3/3] .. --- src/Main.java | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Main.java b/src/Main.java index 74cca53..52e8742 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,23 +1,23 @@ -import backend.Board; -import backend.Move; -import backend.Piece; -import backend.PieceType; - -import windowInterface.MyInterface; - - -public class Main { - - - public static void main(String[] args) { - // testing : - Board testBoard = new Board(8, 8); - testBoard.populateBoard(); - System.out.println(testBoard.toString()); - - // launches graphical interface : - MyInterface mjf = new MyInterface(); - mjf.setVisible(true); + import backend.Board; + import backend.Move; + import backend.Piece; + import backend.PieceType; + + import windowInterface.MyInterface; + + + public class Main { + + + public static void main(String[] args) { + // testing : + Board testBoard = new Board(8, 8); + testBoard.populateBoard(); + System.out.println(testBoard.toString()); + + // launches graphical interface : + MyInterface mjf = new MyInterface(); + mjf.setVisible(true); + } + } - -}