diff --git a/clara1.tkt b/clara1.tkt index dd4f2aa..c1bdfd0 100644 --- a/clara1.tkt +++ b/clara1.tkt @@ -1,5 +1,5 @@ -7 -false +2 +true Rook,0,0,false Knight,1,0,false Bishop,2,0,false @@ -9,7 +9,7 @@ Bishop,5,0,false Knight,6,0,false Rook,7,0,false Pawn,0,1,false -Pawn,1,1,false +Pawn,1,3,false Pawn,2,1,false Pawn,3,1,false Pawn,4,1,false diff --git a/src/backend/AutoPlayer.java b/src/backend/AutoPlayer.java index 0d43701..6f9c980 100644 --- a/src/backend/AutoPlayer.java +++ b/src/backend/AutoPlayer.java @@ -20,9 +20,26 @@ public class AutoPlayer { int[] moveCoords = moves.get(j); int toX = moveCoords[0]; int toY = moveCoords[1]; + + int fromX = piece.getX(); + int fromY = piece.getY(); + Piece captured = board.getPieceAt(toX, toY); - Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured); + // Detect en passant + if (piece.getType() == PieceType.Pawn && board.isEnPassant() && toX == board.getXCoordinatePawn() && + toY == (piece.isWhite() ? board.getYCoordinatePawn() - 1 : board.getYCoordinatePawn() + 1)) { + + captured = board.getPieceAt(board.getXCoordinatePawn(), board.getYCoordinatePawn()); + } + + // --- Castling detection --- + if (piece.getType() == PieceType.King && Math.abs(toX - fromX) == 2 && toY == fromY) { + System.out.println("AI considering castling move."); + // No need to set captured, just allow the move + } + + Move move = new Move(piece, piece.getX(), piece.getY(), toX, toY, captured); allMoves.add(move); } } diff --git a/src/backend/Board.java b/src/backend/Board.java index 49fa183..32f3681 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -16,8 +16,13 @@ public class Board { private int xCoordinatePawn; private int yCoordinatePawn; private boolean enPassant; +<<<<<<< HEAD private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore +======= + private boolean isGameOver = false;//flag for when a king is checkmate and no moves can be made anymore + +>>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git private ArrayList previousStates; public Board(int colNum, int lineNum) { @@ -381,8 +386,11 @@ public class Board { } public void playMove(Move move) { - if (move == null) return; - + if (move == null || move.getPiece() == null) { + System.out.println("Invalid move: " + move); + return; + } + Piece piece = move.getPiece(); int toX = move.getToX(); int toY = move.getToY(); diff --git a/src/backend/Game.java b/src/backend/Game.java index 5673f34..903d1b9 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -10,7 +10,7 @@ public class Game extends Thread { private MyInterface mjf; private int COL_NUM = 8; private int LINE_NUM = 8; - private int loopDelay = 250; + private int loopDelay = 1000; boolean[] activationAIFlags; public Game(MyInterface mjfParam) { @@ -52,9 +52,10 @@ public class Game extends Thread { } private void aiPlayerTurn() { - if(isAITurn()) { - board.playMove(aiPlayer.computeBestMove(new Board(board))); - } + if (isAITurn()) { + Move move = aiPlayer.computeBestMove(board); // Use the actual board + board.playMove(move); // Move from the actual board + } } public void clickCoords(int x, int y) { diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 9ca1974..1b64105 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -6,16 +6,6 @@ public class Piece { private boolean isWhite; private PieceType type; - /*private boolean enPassantEligible = false; - - public boolean isEnPassantEligible() { - return enPassantEligible; - } - - public void setEnPassantEligible(boolean eligible) { - this.enPassantEligible = eligible; - } -*/ public Piece(int x, int y, boolean isWhite, PieceType type) { this.x=x; this.y=y; diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java deleted file mode 100644 index 5c50a38..0000000 --- a/src/backend/SpecialMoves.java +++ /dev/null @@ -1,147 +0,0 @@ -/*package backend; - -import java.util.ArrayList; - -public class SpecialMoves { - - private Piece piece; - private Board board; - private boolean isWhite; - - - public SpecialMoves(Piece piece, Board board) { - this.piece = piece; - this.board = board; - this.isWhite = piece.isWhite(); - } - - private int x = piece.getX(); - private int y = piece.getY(); - - private boolean pawnDoubleStep = board.isPawnDoubleStep(); - private int xCoordinatePawn = board.getXCoordinatePawn(); - private int yCoordinatePawn = board.getYCoordinatePawn(); - - for (int n = 2; n < 4; n++) { - int i = x + pieceMoves[n][0] - - int j = y + pieceMoves[n][1]; - Move move = new Move(x, y, i, j); - - // Diagonal capture - if (inBoard(i, j) && - board.getPiece(j, i) != null && - board.getPiece(j, i).isWhite() != isWhite && - !checkIf(move, coordinates[0], coordinates[1], isWhite)) { - - moves.add(new int[]{i, j}); - } - - // En Passant - else if (inBoard(i, j) && - board.getPiece(j, i) == null && - pawnDoubleStep && - i == xCoordinatePawn && - y == yCoordinatePawn && - Math.abs(x - xCoordinatePawn) == 1 && - board.getPiece(y, xCoordinatePawn) != null && - board.getPiece(y, xCoordinatePawn).getType() == PieceType.Pawn && - board.getPiece(y, xCoordinatePawn).isWhite() != isWhite && - !checkIf(move, coordinates[0], coordinates[1], isWhite)) { - - moves.add(new int[]{i, j}); - } - } - - return moves; - } - - private boolean inBoard(int x, int y) { - return x >= 0 && x < 8 && y >= 0 && y < 8; - } - - private boolean checkIf(Move move, int x, int y, boolean isWhite) { - // Stub – replace with your actual move-check logic - return false; - } -} - - - - - - /* public SpecialMoves(Piece piece, Board board) { - this.piece = piece; - this.board = board; - } //??? - - public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) { - if (type == PieceType.Pawn || isWhite == true || y == 6) { - int[][] offsets = {{1, -2}, {-1, -2}}; - } - if (type == PieceType.Pawn || isWhite == false || y == 1) { - int[][] offsets = {{1, 2}, {-1, 2}}; - }*/ - - - /* public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) { - if (type == PieceType.Pawn || isWhite == true || y == 3) { ->>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git - int[][] offsets = {{1, 0}, {-1, 0}}; - - } - if (type == PieceType.Pawn && isWhite == false && y == 4) { - int[][] offsets = {{1, 0}, {-1, 0}}; - } - - - - } - - public boolean isEnpassant() { - boolean isWhite = piece.isWhite(); -<<<<<<< HEAD - if (boolean isWhite == true && PieceType getType() == PieceType.Pawn) { //no idea honestly -======= - if (isWhite == true || PieceType getType() == PieceType.Pawn) { //no idea honestly ->>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git - if () - - } - if (boolean isWhite == false && //PieceType type == Pawn) { - if () - } - } - -} -package backend; - -public class SpecialMoves { - - private Piece piece; - private Board board; - - public SpecialMoves(Piece piece, Board board) { - this.piece = piece; - this.board = board; - } - - // Call this when trying to move a pawn to check for en passant - public boolean isEnpassant(int fromX, int fromY, int toX, int toY) { - if (piece.getType() != PieceType.Pawn) return false; - - boolean isWhite = piece.isWhite(); - - // En passant capture happens diagonally - int dx = toX - fromX; - int dy = toY - fromY; - - if (Math.abs(dx) != 1 || (isWhite && dy != -1) || (!isWhite && dy != 1)) { - return false; - } - - // Check the adjacent piece - Piece sidePawn = board.getPieceAt(toX, fromY); - if (sidePawn == null || sidePawn.getType() != PieceType.Pawn || sidePawn.isWhite() == isWhite) { - -*/ \ No newline at end of file diff --git a/src/windowInterface/JPanelChessBoard.java b/src/windowInterface/JPanelChessBoard.java index e163bca..d85ffe7 100644 --- a/src/windowInterface/JPanelChessBoard.java +++ b/src/windowInterface/JPanelChessBoard.java @@ -85,18 +85,18 @@ public class JPanelChessBoard extends JPanel { if (myGame == null) { // Display title before game starts g.setColor(Color.WHITE); - g.setFont(g.getFont().deriveFont(48f)); // larger font size + g.setFont(g.getFont().deriveFont(48f)); String title = "Chess Project"; int stringWidth = g.getFontMetrics().stringWidth(title); int x = (getWidth() - stringWidth) / 2; int y = getHeight() / 2; g.drawString(title, x, y); - g.setFont(g.getFont().deriveFont(24f)); // Smaller font for subtitle + g.setFont(g.getFont().deriveFont(24f)); String subtitle = "Groupe 2A5"; int subtitleWidth = g.getFontMetrics().stringWidth(subtitle); int x2 = (getWidth() - subtitleWidth) / 2; - int y2 = y + 40; // 40 pixels below the title, adjust spacing if needed + int y2 = y + 40; g.drawString(subtitle, x2, y2); return;