From 014faea8ca1038ed6707d6d869832a699a82ece8 Mon Sep 17 00:00:00 2001 From: clement Date: Mon, 19 May 2025 19:02:31 +0200 Subject: [PATCH] Roque + qlq corrections syntaxe --- OOP_2B6_PROJECT/src/backend/Board.java | 95 +++++++++++++++++--------- OOP_2B6_PROJECT/src/backend/Game.java | 9 ++- OOP_2B6_PROJECT/src/backend/Move.java | 66 +++++++++--------- 3 files changed, 99 insertions(+), 71 deletions(-) diff --git a/OOP_2B6_PROJECT/src/backend/Board.java b/OOP_2B6_PROJECT/src/backend/Board.java index a05e8f2..8715b91 100644 --- a/OOP_2B6_PROJECT/src/backend/Board.java +++ b/OOP_2B6_PROJECT/src/backend/Board.java @@ -18,7 +18,6 @@ public class Board { private Stack moveHistory = new Stack<>(); - // EN PASSANT private Integer enPassantX = null; private Integer enPassantY = null; @@ -76,6 +75,10 @@ public class Board { public Integer getEnPassantX() { return enPassantX; } public Integer getEnPassantY() { return enPassantY; } + public void setGameOver(boolean gameOver) { + this.gameOver = 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)); @@ -119,16 +122,11 @@ public class Board { public ArrayList getPieces() { return pieces; } - /** - * Gère la sélection et le déplacement d'une pièce par l'utilisateur. - * Empêche la capture du roi et termine la partie sur échec et mat. - */ public void userTouch(int x, int y) { if (gameOver) return; Piece clickedPiece = getPieceAt(x, y); - // Sélection initiale if (selectedX == null || selectedY == null) { if (clickedPiece != null && clickedPiece.isWhite() == turnIsWhite) { selectedX = x; @@ -138,7 +136,6 @@ public class Board { } else { Piece selectedPiece = getPieceAt(selectedX, selectedY); if (selectedPiece != null && selectedPiece.isWhite() == turnIsWhite) { - // Simuler le mouvement pour vérifier l'absence d'échec Board simulation = new Board(this); simulation.pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY); simulation.pieces.removeIf(p -> p.getX() == x && p.getY() == y); @@ -151,32 +148,55 @@ public class Board { if (!simulation.isKingInCheck(turnIsWhite)) { Piece target = getPieceAt(x, y); - // EN PASSANT (avant la capture normale) boolean enPassantMove = false; if (selectedPiece.getType() == PieceType.Pawn && getEnPassantX() != null && getEnPassantY() != null && x == getEnPassantX() && y == getEnPassantY() && target == null && selectedX != x) { - // On va prendre en passant enPassantMove = true; int capturedY = turnIsWhite ? y + 1 : y - 1; pieces.removeIf(p -> p.getX() == x && p.getY() == capturedY); - target = getPieceAt(x, capturedY); // Pour le move history + target = getPieceAt(x, capturedY); + } + + boolean castlingMove = false; + if (selectedPiece.getType() == PieceType.King) { + // Petit roque blanc + if (selectedPiece.isWhite() && selectedX == 4 && selectedY == 7 && x == 6 && y == 7) { + pieces.removeIf(p -> p.getX() == 7 && p.getY() == 7); + pieces.add(new Piece(true, PieceType.Rook, 5, 7)); + castlingMove = true; + } + // Grand roque blanc + if (selectedPiece.isWhite() && selectedX == 4 && selectedY == 7 && x == 2 && y == 7) { + pieces.removeIf(p -> p.getX() == 0 && p.getY() == 7); + pieces.add(new Piece(true, PieceType.Rook, 3, 7)); + castlingMove = true; + } + // Petit roque noir + if (!selectedPiece.isWhite() && selectedX == 4 && selectedY == 0 && x == 6 && y == 0) { + pieces.removeIf(p -> p.getX() == 7 && p.getY() == 0); + pieces.add(new Piece(false, PieceType.Rook, 5, 0)); + castlingMove = true; + } + // Grand roque noir + if (!selectedPiece.isWhite() && selectedX == 4 && selectedY == 0 && x == 2 && y == 0) { + pieces.removeIf(p -> p.getX() == 0 && p.getY() == 0); + pieces.add(new Piece(false, PieceType.Rook, 3, 0)); + castlingMove = true; + } } - // Si on cible le roi adverse, c'est un cas de mat -> terminer la partie if (target != null && target.getType() == PieceType.King) { gameOver = true; return; } - // Capture normale if (!enPassantMove && target != null) { pieces.removeIf(p -> p.getX() == x && p.getY() == y); } - // Déplacer la pièce sélectionnée pieces.removeIf(p -> p.getX() == selectedX && p.getY() == selectedY); pieces.add(new Piece( selectedPiece.isWhite(), @@ -184,31 +204,27 @@ public class Board { x, y )); - // EN PASSANT : Maj de la case en passant if (selectedPiece.getType() == PieceType.Pawn && Math.abs(y - selectedY) == 2) { enPassantX = x; - enPassantY = (y + selectedY) / 2; // Case traversée + enPassantY = (y + selectedY) / 2; } else { enPassantX = null; enPassantY = null; } - // Move history (on précise enPassant ou non) Move move = new Move(selectedPiece, selectedX, selectedY, x, y, target, - enPassantMove); + enPassantMove, castlingMove); moveHistory.push(move); - // Changer de tour turnNumber++; turnIsWhite = !turnIsWhite; - // Vérifier échec et mat sur le prochain joueur if (isCheckmate(turnIsWhite)) { gameOver = true; } } } - // Réinitialiser la sélection + selectedX = null; selectedY = null; highlightedPositions.clear(); @@ -267,7 +283,7 @@ public class Board { break; } } - if (king == null) return true; // pas de roi => considérer comme échec + if (king == null) return true; for (Piece enemy : pieces) { if (enemy.isWhite() != isWhite) { ArrayList moves = getLegalMovesFor(enemy); @@ -314,12 +330,10 @@ public class Board { moves.add(new int[]{x, twoStepY}); } } - // Capture à gauche if (inBounds(x - 1, oneStepY)) { Piece left = getPieceAt(x - 1, oneStepY); if (left != null && left.isWhite() != isWhite) moves.add(new int[]{x - 1, oneStepY}); - // EN PASSANT GAUCHE if (getEnPassantX() != null && getEnPassantY() != null && x - 1 == getEnPassantX() && oneStepY == getEnPassantY() && getPieceAt(x - 1, y) != null && @@ -328,12 +342,10 @@ public class Board { moves.add(new int[]{x - 1, oneStepY}); } } - // Capture à droite if (inBounds(x + 1, oneStepY)) { Piece right = getPieceAt(x + 1, oneStepY); if (right != null && right.isWhite() != isWhite) moves.add(new int[]{x + 1, oneStepY}); - // EN PASSANT DROITE if (getEnPassantX() != null && getEnPassantY() != null && x + 1 == getEnPassantX() && oneStepY == getEnPassantY() && getPieceAt(x + 1, y) != null && @@ -452,29 +464,45 @@ public class Board { for (String line : toFileRep()) System.out.println(line); } - /** - * Appliquer un coup déjà construit. - * Bloque la capture du roi et termine la partie sur échec et mat. - */ public void playMove(Move move) { if (move == null || gameOver) return; Piece captured = getPieceAt(move.getToX(), move.getToY()); - // EN PASSANT (avant la capture normale) + if (move.getMovedPiece().getType() == PieceType.Pawn && move.isEnPassant()) { int capturedY = move.getMovedPiece().isWhite() ? move.getToY() + 1 : move.getToY() - 1; pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == capturedY); } - // Si tentative de capturer le roi, fin de partie + if (captured != null && captured.getType() == PieceType.King) { gameOver = true; return; } - // Capture normale + if (!(move.getMovedPiece().getType() == PieceType.Pawn && move.isEnPassant()) && captured != null) { pieces.removeIf(p -> p.getX() == move.getToX() && p.getY() == move.getToY()); } - // Déplacement + + // Roque (déplacement de la tour) + if (move.getMovedPiece().getType() == PieceType.King && move.isCastling()) { + if (move.getMovedPiece().isWhite() && move.getFromX() == 4 && move.getFromY() == 7 && move.getToX() == 6 && move.getToY() == 7) { + pieces.removeIf(p -> p.getX() == 7 && p.getY() == 7); + pieces.add(new Piece(true, PieceType.Rook, 5, 7)); + } + if (move.getMovedPiece().isWhite() && move.getFromX() == 4 && move.getFromY() == 7 && move.getToX() == 2 && move.getToY() == 7) { + pieces.removeIf(p -> p.getX() == 0 && p.getY() == 7); + pieces.add(new Piece(true, PieceType.Rook, 3, 7)); + } + if (!move.getMovedPiece().isWhite() && move.getFromX() == 4 && move.getFromY() == 0 && move.getToX() == 6 && move.getToY() == 0) { + pieces.removeIf(p -> p.getX() == 7 && p.getY() == 0); + pieces.add(new Piece(false, PieceType.Rook, 5, 0)); + } + if (!move.getMovedPiece().isWhite() && move.getFromX() == 4 && move.getFromY() == 0 && move.getToX() == 2 && move.getToY() == 0) { + pieces.removeIf(p -> p.getX() == 0 && p.getY() == 0); + pieces.add(new Piece(false, PieceType.Rook, 3, 0)); + } + } + pieces.removeIf(p -> p.getX() == move.getFromX() && p.getY() == move.getFromY()); pieces.add(new Piece( move.getMovedPiece().isWhite(), @@ -482,7 +510,6 @@ public class Board { move.getToX(), move.getToY() )); - // EN PASSANT : Maj de la case en passant if (move.getMovedPiece().getType() == PieceType.Pawn && Math.abs(move.getToY() - move.getFromY()) == 2) { enPassantX = move.getToX(); enPassantY = (move.getToY() + move.getFromY()) / 2; diff --git a/OOP_2B6_PROJECT/src/backend/Game.java b/OOP_2B6_PROJECT/src/backend/Game.java index c829e1e..ede644c 100644 --- a/OOP_2B6_PROJECT/src/backend/Game.java +++ b/OOP_2B6_PROJECT/src/backend/Game.java @@ -44,7 +44,7 @@ public class Game extends Thread { try { Thread.sleep(loopDelay); } catch (InterruptedException e) { - // Thread interrupted -> fin de la boucle de jeu + break; } } @@ -61,11 +61,11 @@ public class Game extends Thread { private void checkForCheckmate() { boolean nextIsWhite = board.isTurnWhite(); if (board.isCheckmate(nextIsWhite)) { - // 1) on fixe le flag gameOver dans Board + board.setGameOver(true); - // 2) on force l'UI à se repaint + mjf.update(board.getTurnNumber(), board.isTurnWhite()); - // 3) on quitte la boucle run() + this.interrupt(); } } @@ -78,7 +78,6 @@ public class Game extends Thread { } public void clickCoords(int x, int y) { - // bornes ajustées : 0 <= x < width, 0 <= y < height if (x < 0 || y < 0 || x >= getWidth() || y >= getHeight()) { System.out.println("Click out of bounds"); return; diff --git a/OOP_2B6_PROJECT/src/backend/Move.java b/OOP_2B6_PROJECT/src/backend/Move.java index bf5f87c..bff7cd8 100644 --- a/OOP_2B6_PROJECT/src/backend/Move.java +++ b/OOP_2B6_PROJECT/src/backend/Move.java @@ -6,9 +6,11 @@ public class Move { private int toX, toY; private Piece capturedPiece; - + // Pour coups spéciaux private boolean isEnPassant = false; + private boolean isCastling = false; + // Constructeur normal public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece) { this.movedPiece = movedPiece; this.fromX = fromX; @@ -18,7 +20,24 @@ public class Move { this.capturedPiece = capturedPiece; } - public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece, boolean isEnPassant) { + // Constructeur pour en passant OU roque + public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece, boolean isEnPassantOrCastling) { + this.movedPiece = movedPiece; + this.fromX = fromX; + this.fromY = fromY; + this.toX = toX; + this.toY = toY; + this.capturedPiece = capturedPiece; + // Heuristique simple : si c'est un pion, c'est "en passant", sinon c'est un roque + if (movedPiece.getType() == PieceType.Pawn) { + this.isEnPassant = isEnPassantOrCastling; + } else if (movedPiece.getType() == PieceType.King) { + this.isCastling = isEnPassantOrCastling; + } + } + + // Constructeur complet, si besoin de tout préciser + public Move(Piece movedPiece, int fromX, int fromY, int toX, int toY, Piece capturedPiece, boolean isEnPassant, boolean isCastling) { this.movedPiece = movedPiece; this.fromX = fromX; this.fromY = fromY; @@ -26,38 +45,21 @@ public class Move { this.toY = toY; this.capturedPiece = capturedPiece; this.isEnPassant = isEnPassant; + this.isCastling = isCastling; } - public Piece getMovedPiece() { - return movedPiece; - } + public Piece getMovedPiece() { return movedPiece; } + public int getFromX() { return fromX; } + public int getFromY() { return fromY; } + public int getToX() { return toX; } + public int getToY() { return toY; } + public Piece getCapturedPiece() { return capturedPiece; } - public int getFromX() { - return fromX; - } + // Pour en passant + public boolean isEnPassant() { return isEnPassant; } + public void setEnPassant(boolean enPassant) { this.isEnPassant = enPassant; } - public int getFromY() { - return fromY; - } - - public int getToX() { - return toX; - } - - public int getToY() { - return toY; - } - - public Piece getCapturedPiece() { - return capturedPiece; - } - - - public boolean isEnPassant() { - return isEnPassant; - } - - public void setEnPassant(boolean enPassant) { - this.isEnPassant = enPassant; - } + // Pour roque + public boolean isCastling() { return isCastling; } + public void setCastling(boolean castling) { this.isCastling = castling; } }