Message + trying no eat the king

This commit is contained in:
clement 2025-05-13 16:27:08 +02:00
parent 4dc6dee154
commit 2f0f0c8d91
1 changed files with 108 additions and 85 deletions

View File

@ -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<Piece> 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<Piece> 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];
}
}