diff --git a/OOP_2B6_PROJECT/src/backend/Board.java b/OOP_2B6_PROJECT/src/backend/Board.java index 31582ac..2046fb7 100644 --- a/OOP_2B6_PROJECT/src/backend/Board.java +++ b/OOP_2B6_PROJECT/src/backend/Board.java @@ -17,6 +17,9 @@ public class Board { private Stack moveHistory = new Stack<>(); + // Indique si la partie est terminée (roi capturé) + private boolean gameOver = false; + public Board(int colNum, int lineNum) { this.width = colNum; this.height = lineNum; @@ -39,12 +42,20 @@ public class Board { return turnIsWhite; } + // Expose l'état de fin de partie + public boolean isGameOver() { + return gameOver; + } + public void setPiece(boolean isWhite, PieceType type, int x, int y) { pieces.removeIf(p -> p.getX() == x && p.getY() == y); pieces.add(new Piece(isWhite, type, x, y)); } public void populateBoard() { + // Réinitialiser l'état de fin de partie + gameOver = false; + pieces.clear(); setPiece(true, PieceType.Rook, 0, 7); setPiece(true, PieceType.Knight, 1, 7); @@ -68,10 +79,17 @@ public class Board { } public void cleanBoard() { + // Réinitialiser l'état de fin de partie + gameOver = false; + pieces.clear(); selectedX = null; selectedY = null; highlightedPositions.clear(); + boolean kingInCheck = isInCheck(turnIsWhite); + if (kingInCheck) { + System.out.println("Échec !"); + } moveHistory.clear(); } @@ -80,6 +98,11 @@ public class Board { } public void userTouch(int x, int y) { + if (gameOver) { + // Ne pas autoriser de nouveaux coups si la partie est terminée + return; + } + Piece clickedPiece = getPieceAt(x, y); if (selectedX == null || selectedY == null) { if (clickedPiece != null && clickedPiece.isWhite() == turnIsWhite) { @@ -90,7 +113,11 @@ public class Board { } else { Piece selectedPiece = getPieceAt(selectedX, selectedY); if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) { + // Détecter capture de roi Piece captured = getPieceAt(x, y); + if (captured != null && captured.getType() == PieceType.King) { + gameOver = true; + } pieces.removeIf(p -> p.getX() == x && p.getY() == y); pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY); Piece moved = new Piece( @@ -129,6 +156,8 @@ public class Board { selectedX = null; selectedY = null; highlightedPositions.clear(); + // Réinitialiser gameOver si on ressuscite un roi + gameOver = false; } } @@ -306,6 +335,7 @@ public class Board { } } turnIsWhite = array[8].equalsIgnoreCase("W"); + gameOver = false; } public Board(Board board) { @@ -315,10 +345,16 @@ public class Board { this.turnNumber = board.getTurnNumber(); this.turnIsWhite = board.isTurnWhite(); this.moveHistory = new Stack<>(); + this.gameOver = board.isGameOver(); } public void playMove(Move move) { - if (move == null) return; + if (move == null || gameOver) return; + // Détecter capture du roi avant d'appliquer le coup + Piece captured = move.getCapturedPiece(); + if (captured != null && captured.getType() == PieceType.King) { + gameOver = true; + } pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY()); pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY()); pieces.add(new Piece( @@ -334,6 +370,32 @@ public class Board { selectedY = null; highlightedPositions.clear(); } + + private boolean isInCheck(boolean whiteKing) { + Piece king = null; + for (Piece p : pieces) { + if (p.getType() == PieceType.King && p.isWhite() == whiteKing) { + king = p; + break; + } + } + + if (king == null) return false; // Cas improbable + + int kingX = king.getX(); + int kingY = king.getY(); + + for (Piece p : pieces) { + if (p.isWhite() != whiteKing) { + ArrayList moves = getLegalMovesFor(p); + for (int[] move : moves) { + if (move[0] == kingX && move[1] == kingY) { + return true; + } + } + } + } + + return false; + } } - -