From 21ff8c1b01c5683d832d3ad8585e6fbd97fa8c3b Mon Sep 17 00:00:00 2001 From: leahb Date: Mon, 19 May 2025 21:32:31 +0200 Subject: [PATCH 1/5] little ajustements --- src/backend/Board.java | 9 +++------ src/windowInterface/JPanelChessBoard.java | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 6bd2c0b..3fce592 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -14,13 +14,10 @@ public class Board { ArrayList highlightedSquares = new ArrayList<>(); private boolean pawnDoubleStep; private int xCoordinatePawn; - private int yCoordinatePawn; -//<<<<<<< HEAD - private boolean enPassant; -//======= + private int yCoordinatePawn; + private boolean enPassant; 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) { 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; From 58f038d0f2fe49f7941308fa56fe27fbaa36662d Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 20 May 2025 11:08:08 +0200 Subject: [PATCH 2/5] Autoplayer implemented (needs to modify speed and checkmate condition) --- src/backend/Board.java | 7 +++++-- src/backend/Game.java | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 3fce592..ffff2fe 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -381,8 +381,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..7a29fae 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -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) { From 06ed1a0ffb9cba8380c06d63c27e4aa60ccab795 Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 20 May 2025 11:50:29 +0200 Subject: [PATCH 3/5] Autoplayer can detect and use en passant and castling --- src/backend/AutoPlayer.java | 19 ++++++++++++++++++- src/backend/Game.java | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) 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/Game.java b/src/backend/Game.java index 7a29fae..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) { From f746b99554319bd733dfc63defa74c6bc434955b Mon Sep 17 00:00:00 2001 From: leahb Date: Wed, 21 May 2025 16:24:29 +0200 Subject: [PATCH 4/5] DELETE USELESS CLASS --- src/backend/SpecialMoves.java | 147 ---------------------------------- 1 file changed, 147 deletions(-) delete mode 100644 src/backend/SpecialMoves.java 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 From 5aab9ded917e7a7af79006495ba735cdada0c416 Mon Sep 17 00:00:00 2001 From: leahb Date: Wed, 21 May 2025 16:50:48 +0200 Subject: [PATCH 5/5] cdnswicfjeqzipdo --- clara1.tkt | 6 +++--- src/backend/Piece.java | 10 ---------- 2 files changed, 3 insertions(+), 13 deletions(-) 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/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;