From 3c77607556daf932f94ae75d9016e8f169d682e8 Mon Sep 17 00:00:00 2001 From: "louise.berteloot" Date: Wed, 7 May 2025 15:26:11 +0200 Subject: [PATCH 1/5] jsp --- src/backend/SpecialMoves.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java index e7d240d..18f9468 100644 --- a/src/backend/SpecialMoves.java +++ b/src/backend/SpecialMoves.java @@ -1,5 +1,14 @@ package backend; public class SpecialMoves { + + private Piece piece; + private Board board; + + + + + + } From 01fb01c4894b15def0f2fce70398355b8be96056 Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 13 May 2025 14:08:26 +0200 Subject: [PATCH 2/5] Still working on check and checkmate --- src/Main.java | 7 --- src/backend/Board.java | 101 ++++++++++++++++---------------- src/backend/MoveConditions.java | 6 +- 3 files changed, 52 insertions(+), 62 deletions(-) diff --git a/src/Main.java b/src/Main.java index d35d291..847f065 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,13 +5,6 @@ import backend.PieceType; import windowInterface.MyInterface; - -import java.io.PrintWriter; -import java.io.IOException; -import java.io.FileReader; -import java.io.BufferedReader; -import java.util.ArrayList; - public class Main { diff --git a/src/backend/Board.java b/src/backend/Board.java index 303e67f..3f25832 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -180,18 +180,17 @@ public class Board { selectedY = -1; highlightedSquares.clear(); - if (isKingInCheck(true)) { - System.out.println("White is in check!"); - } - if (isKingInCheck(false)) { - System.out.println("Black is in check!"); - } - - if (isCheckmate(true)) { - System.out.println("White is in checkmate!"); - } - if (isCheckmate(false)) { - System.out.println("Black is in checkmate!"); + // After move completed, check for check and checkmate + for (int i = 0; i < 2; i++) { + boolean isWhite = (i == 0); + + if (isKingInCheck(isWhite)) { + System.out.println((isWhite ? "White" : "Black") + " is in check!"); + + if (isCheckmate(isWhite)) { + System.out.println((isWhite ? "White" : "Black") + " is in checkmate!"); + } + } } } @@ -338,59 +337,57 @@ public class Board { boolean kingInCheck = isKingInCheck(whiteKing); boolean hasEscape = false; - if (kingInCheck) { - for (int i = 0; i < pieces.size(); i++) { - Piece piece = pieces.get(i); + // 1. If the king is not in check, it's never checkmate + if (!kingInCheck) { + return false; + } - if (piece.isWhite() == whiteKing) { - ArrayList rawMoves = getValidMoves(piece); + // 2. Try every possible move of every piece belonging to the checked side + for (int i = 0; i < pieces.size(); i++) { + Piece piece = pieces.get(i); - for (int j = 0; j < rawMoves.size(); j++) { - int[] move = rawMoves.get(j); - int newX = move[0]; - int newY = move[1]; + if (piece.isWhite() == whiteKing) { + ArrayList rawMoves = getValidMoves(piece); - // Simulate the board - Board simBoard = new Board(this); + for (int j = 0; j < rawMoves.size(); j++) { + int[] move = rawMoves.get(j); + int newX = move[0]; + int newY = move[1]; - // Find the same piece in the simulated board - Piece simPiece = null; - for (int k = 0; k < simBoard.getPieces().size(); k++) { - Piece p = simBoard.getPieces().get(k); - if (p.getX() == piece.getX() && p.getY() == piece.getY() - && p.getType() == piece.getType() - && p.isWhite() == piece.isWhite()) { - simPiece = p; - } + // 3. Simulate this move on a copied board + Board simBoard = new Board(this); + + // 4. Find the corresponding piece on the cloned board + Piece simPiece = null; + for (int k = 0; k < simBoard.getPieces().size(); k++) { + Piece p = simBoard.getPieces().get(k); + if (p.getX() == piece.getX() && p.getY() == piece.getY() + && p.getType() == piece.getType() + && p.isWhite() == piece.isWhite()) { + simPiece = p; + } + } + + // 5. Apply the move and check if king is still in check + if (simPiece != null) { + Piece captured = simBoard.getPieceAt(newX, newY); + if (captured != null) { + simBoard.getPieces().remove(captured); } - if (simPiece != null) { - Piece captured = simBoard.getPieceAt(newX, newY); - if (captured != null) { - simBoard.getPieces().remove(captured); - } + simPiece.setX(newX); + simPiece.setY(newY); - simPiece.setX(newX); - simPiece.setY(newY); - - boolean stillInCheck = simBoard.isKingInCheck(whiteKing); - - if (!stillInCheck) { - hasEscape = true; - } + if (!simBoard.isKingInCheck(whiteKing)) { + hasEscape = true; } } } } } - boolean checkmate = false; - - if (kingInCheck && !hasEscape) { - checkmate = true; - } - - return checkmate; + // 6. If the king is in check and no move avoids it → checkmate + return kingInCheck && !hasEscape; } } diff --git a/src/backend/MoveConditions.java b/src/backend/MoveConditions.java index 5075bf7..914ede2 100644 --- a/src/backend/MoveConditions.java +++ b/src/backend/MoveConditions.java @@ -181,7 +181,7 @@ public class MoveConditions { if (target == null || target.isWhite() != isWhite) { Board simBoard = new Board(board); - // Find this same King in the simulated board + // Safely locate the cloned king Piece simKing = null; for (int j = 0; j < simBoard.getPieces().size(); j++) { Piece p = simBoard.getPieces().get(j); @@ -201,8 +201,7 @@ public class MoveConditions { simKing.setX(newX); simKing.setY(newY); - boolean stillInCheck = simBoard.isKingInCheck(isWhite); - if (!stillInCheck) { + if (!simBoard.isKingInCheck(isWhite)) { moves.add(new int[]{newX, newY}); } } @@ -212,6 +211,7 @@ public class MoveConditions { return moves; } + } \ No newline at end of file From 20edcf124bceed007b65c79ffaa2c1466c538cc5 Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 13 May 2025 14:11:58 +0200 Subject: [PATCH 3/5] removed conflict residual tags --- src/Main.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Main.java b/src/Main.java index df45504..277f651 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,12 +4,7 @@ import backend.Piece; import backend.PieceType; import windowInterface.MyInterface; - -<<<<<<< HEAD -======= - - ->>>>>>> branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git + public class Main { From 6985d182382ee6b5220ec3baf78f72569c7c2ae4 Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 13 May 2025 14:40:05 +0200 Subject: [PATCH 4/5] Checkmate dynamic fixed (finallyyyy) --- src/backend/Board.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backend/Board.java b/src/backend/Board.java index 3b228be..37fd29a 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -383,6 +383,7 @@ public class Board { if (!simBoard.isKingInCheck(whiteKing)) { hasEscape = true; + System.out.println("ESCAPE FOUND: " + piece.getType() + " from (" + piece.getX() + "," + piece.getY() + ") to (" + newX + "," + newY + ")"); } } } From b677a3d3d938028795ff01b3788ef72044bc4abb Mon Sep 17 00:00:00 2001 From: manon Date: Tue, 13 May 2025 14:54:50 +0200 Subject: [PATCH 5/5] Merge branch 'master' of https://gitarero.ecam.fr/louise.berteloot/OOP_2A5_Project.git --- src/backend/Board.java | 16 +++++++++++++--- src/backend/Game.java | 1 + src/backend/SpecialMoves.java | 5 +---- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 12dac32..9bda6b4 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -34,6 +34,11 @@ public class Board { public boolean isTurnWhite() { return this.isWhiteTurn; } + + public void resetTurn() { + this.turnNumber = 0; + this.isWhiteTurn = true; + } public void setPiece(boolean isWhite, PieceType type, int x, int y) { Piece newPiece = new Piece(x, y, isWhite, type); @@ -394,6 +399,8 @@ public class Board { return kingInCheck && !hasEscape; } + + /* private void enPassant(Board board, List moves) { int x = this.x; int y = this.y; @@ -416,19 +423,22 @@ public class Board { } } - /** + * Checks if the pawn can capture another pawn by en passant * @param pt location of the other pawn * @return true if can be captured - */ + + private boolean isEnPassant(Board board, Point pt) { Piece temp = board.getPieceAt(pt); - if(temp != null) + if(temp != null) { if (temp instanceof Pawn && temp.getColor() != this.color) if (((Pawn)temp).enPassantOk) return true; return false; } + */ + } diff --git a/src/backend/Game.java b/src/backend/Game.java index 51d0356..7ab9929 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -83,6 +83,7 @@ public class Game extends Thread { public void setDefaultSetup() { board.cleanBoard(); board.populateBoard(); + board.resetTurn(); } public void setBoard(String[] array) { diff --git a/src/backend/SpecialMoves.java b/src/backend/SpecialMoves.java index 71719ef..3ce85ee 100644 --- a/src/backend/SpecialMoves.java +++ b/src/backend/SpecialMoves.java @@ -27,10 +27,7 @@ public class SpecialMoves { int[][] offsets = {{1, 2}, {-1, 2}}; }*/ -<<<<<<< HEAD - public boolean checkCoordinates(int x, int y, PieceType type, boolean isWhite) { - if (type == PieceType.Pawn && isWhite == true && y == 3) { -======= + /* 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