From 2f0f0c8d919767776398cef247bee46c8d9db1e1 Mon Sep 17 00:00:00 2001 From: clement Date: Tue, 13 May 2025 16:27:08 +0200 Subject: [PATCH] Message + trying no eat the king --- OOP_2B6_PROJECT/src/backend/Game.java | 193 ++++++++++++++------------ 1 file changed, 108 insertions(+), 85 deletions(-) diff --git a/OOP_2B6_PROJECT/src/backend/Game.java b/OOP_2B6_PROJECT/src/backend/Game.java index 4c64f70..85c649a 100644 --- a/OOP_2B6_PROJECT/src/backend/Game.java +++ b/OOP_2B6_PROJECT/src/backend/Game.java @@ -1,110 +1,133 @@ package backend; import windowInterface.MyInterface; +import javax.swing.JOptionPane; +import javax.swing.SwingUtilities; public class Game extends Thread { - private AutoPlayer aiPlayer; - private Board board; + private AutoPlayer aiPlayer; + private Board board; - private MyInterface mjf; - private int COL_NUM = 8; - private int LINE_NUM = 8; - private int loopDelay = 250; - boolean[] activationAIFlags; + private MyInterface mjf; + private int COL_NUM = 8; + private int LINE_NUM = 8; + private int loopDelay = 250; + boolean[] activationAIFlags; - public Game(MyInterface mjfParam) { - mjf = mjfParam; - board = new Board(COL_NUM, LINE_NUM); - loopDelay = 250; - LINE_NUM = 8; - COL_NUM = 8; - activationAIFlags = new boolean[2]; - aiPlayer = new AutoPlayer(); - } + public Game(MyInterface mjfParam) { + mjf = mjfParam; + board = new Board(COL_NUM, LINE_NUM); + loopDelay = 250; + LINE_NUM = 8; + COL_NUM = 8; + activationAIFlags = new boolean[2]; + aiPlayer = new AutoPlayer(); + } - public int getWidth() { - return board.getWidth(); - } + public int getWidth() { + return board.getWidth(); + } - public int getHeight() { - return board.getHeight(); - } + public int getHeight() { + return board.getHeight(); + } - public void run() { - while(true) { - aiPlayerTurn(); - mjf.update(board.getTurnNumber(), board.isTurnWhite()); - try { - Thread.sleep(loopDelay); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - - private boolean isAITurn() { - return activationAIFlags[board.isTurnWhite()?1:0]; - } + @Override + public void run() { + while (true) { + aiPlayerTurn(); + mjf.update(board.getTurnNumber(), board.isTurnWhite()); + try { + Thread.sleep(loopDelay); + } catch (InterruptedException e) { + // Thread interrupted -> end game loop + break; + } + } + } + + private boolean isAITurn() { + return activationAIFlags[board.isTurnWhite() ? 1 : 0]; + } - private void aiPlayerTurn() { - if(isAITurn()) { - board.playMove(aiPlayer.computeBestMove(new Board(board))); - } - } + /** + * Vérifie si l'adversaire est en échec & mat après un coup, + * affiche le message et interrompt le thread si c'est le cas. + */ + private void checkForCheckmate() { + boolean nextIsWhite = board.isTurnWhite(); + if (board.isCheckmate(nextIsWhite)) { + String winner = nextIsWhite ? "Black" : "White"; + SwingUtilities.invokeLater(() -> + JOptionPane.showMessageDialog(mjf, + winner + " wins by checkmate!", + "Game Over", + JOptionPane.INFORMATION_MESSAGE) + ); + this.interrupt(); + } + } - public void clickCoords(int x, int y) { - int width = this.getWidth(); - int height = this.getHeight(); - if(0>x || 0>y || x>width || y>height) { - System.out.println("Click out of bounds"); - return; - } - if(!isAITurn()) { - board.userTouch(x, y); - } - - } + private void aiPlayerTurn() { + if (isAITurn()) { + board.playMove(aiPlayer.computeBestMove(new Board(board))); + checkForCheckmate(); + } + } - public void setPiece(boolean isWhite, PieceType type, int x, int y) { - board.setPiece(isWhite, type, x, y); - } + public void clickCoords(int x, int y) { + int width = this.getWidth(); + int height = this.getHeight(); + if (0 > x || 0 > y || x > width || y > height) { + System.out.println("Click out of bounds"); + return; + } + if (!isAITurn()) { + board.userTouch(x, y); + checkForCheckmate(); + } + } - public String[] getFileRepresentation() { - return board.toFileRep(); - } + public void setPiece(boolean isWhite, PieceType type, int x, int y) { + board.setPiece(isWhite, type, x, y); + } - public void setLoopDelay(int delay) { - this.loopDelay = delay; - } + public String[] getFileRepresentation() { + return board.toFileRep(); + } - public void setDefaultSetup() { - board.cleanBoard(); - board.populateBoard(); - } + public void setLoopDelay(int delay) { + this.loopDelay = delay; + } - public void setBoard(String[] array) { - board = new Board(array); - } + public void setDefaultSetup() { + board.cleanBoard(); + board.populateBoard(); + } - public Iterable getPieces() { - return board.getPieces(); - } + public void setBoard(String[] array) { + board = new Board(array); + } - public boolean isSelected(int x, int y) { - return board.isSelected(x, y); - } + public Iterable getPieces() { + return board.getPieces(); + } - public boolean isHighlighted(int x, int y) { - return board.isHighlighted(x, y); - } + public boolean isSelected(int x, int y) { + return board.isSelected(x, y); + } - public void undoLastMove() { - board.undoLastMove(); - } + public boolean isHighlighted(int x, int y) { + return board.isHighlighted(x, y); + } - public void toggleAI(boolean isWhite) { - this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0]; - } + public void undoLastMove() { + board.undoLastMove(); + } + + public void toggleAI(boolean isWhite) { + this.activationAIFlags[isWhite ? 1 : 0] = !this.activationAIFlags[isWhite ? 1 : 0]; + } }