Message + trying no eat the king
This commit is contained in:
parent
4dc6dee154
commit
2f0f0c8d91
|
|
@ -1,110 +1,133 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import windowInterface.MyInterface;
|
import windowInterface.MyInterface;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
public class Game extends Thread {
|
public class Game extends Thread {
|
||||||
|
|
||||||
private AutoPlayer aiPlayer;
|
private AutoPlayer aiPlayer;
|
||||||
private Board board;
|
private Board board;
|
||||||
|
|
||||||
private MyInterface mjf;
|
private MyInterface mjf;
|
||||||
private int COL_NUM = 8;
|
private int COL_NUM = 8;
|
||||||
private int LINE_NUM = 8;
|
private int LINE_NUM = 8;
|
||||||
private int loopDelay = 250;
|
private int loopDelay = 250;
|
||||||
boolean[] activationAIFlags;
|
boolean[] activationAIFlags;
|
||||||
|
|
||||||
public Game(MyInterface mjfParam) {
|
public Game(MyInterface mjfParam) {
|
||||||
mjf = mjfParam;
|
mjf = mjfParam;
|
||||||
board = new Board(COL_NUM, LINE_NUM);
|
board = new Board(COL_NUM, LINE_NUM);
|
||||||
loopDelay = 250;
|
loopDelay = 250;
|
||||||
LINE_NUM = 8;
|
LINE_NUM = 8;
|
||||||
COL_NUM = 8;
|
COL_NUM = 8;
|
||||||
activationAIFlags = new boolean[2];
|
activationAIFlags = new boolean[2];
|
||||||
aiPlayer = new AutoPlayer();
|
aiPlayer = new AutoPlayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return board.getWidth();
|
return board.getWidth();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return board.getHeight();
|
return board.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
@Override
|
||||||
while(true) {
|
public void run() {
|
||||||
aiPlayerTurn();
|
while (true) {
|
||||||
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
aiPlayerTurn();
|
||||||
try {
|
mjf.update(board.getTurnNumber(), board.isTurnWhite());
|
||||||
Thread.sleep(loopDelay);
|
try {
|
||||||
} catch (InterruptedException e) {
|
Thread.sleep(loopDelay);
|
||||||
e.printStackTrace();
|
} catch (InterruptedException e) {
|
||||||
}
|
// Thread interrupted -> end game loop
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private boolean isAITurn() {
|
}
|
||||||
return activationAIFlags[board.isTurnWhite()?1:0];
|
|
||||||
}
|
private boolean isAITurn() {
|
||||||
|
return activationAIFlags[board.isTurnWhite() ? 1 : 0];
|
||||||
|
}
|
||||||
|
|
||||||
private void aiPlayerTurn() {
|
/**
|
||||||
if(isAITurn()) {
|
* Vérifie si l'adversaire est en échec & mat après un coup,
|
||||||
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
* 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) {
|
private void aiPlayerTurn() {
|
||||||
int width = this.getWidth();
|
if (isAITurn()) {
|
||||||
int height = this.getHeight();
|
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
||||||
if(0>x || 0>y || x>width || y>height) {
|
checkForCheckmate();
|
||||||
System.out.println("Click out of bounds");
|
}
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
if(!isAITurn()) {
|
|
||||||
board.userTouch(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void clickCoords(int x, int y) {
|
||||||
board.setPiece(isWhite, type, x, 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() {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
return board.toFileRep();
|
board.setPiece(isWhite, type, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLoopDelay(int delay) {
|
public String[] getFileRepresentation() {
|
||||||
this.loopDelay = delay;
|
return board.toFileRep();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDefaultSetup() {
|
public void setLoopDelay(int delay) {
|
||||||
board.cleanBoard();
|
this.loopDelay = delay;
|
||||||
board.populateBoard();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void setBoard(String[] array) {
|
public void setDefaultSetup() {
|
||||||
board = new Board(array);
|
board.cleanBoard();
|
||||||
}
|
board.populateBoard();
|
||||||
|
}
|
||||||
|
|
||||||
public Iterable<Piece> getPieces() {
|
public void setBoard(String[] array) {
|
||||||
return board.getPieces();
|
board = new Board(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSelected(int x, int y) {
|
public Iterable<Piece> getPieces() {
|
||||||
return board.isSelected(x, y);
|
return board.getPieces();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isHighlighted(int x, int y) {
|
public boolean isSelected(int x, int y) {
|
||||||
return board.isHighlighted(x, y);
|
return board.isSelected(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
public boolean isHighlighted(int x, int y) {
|
||||||
board.undoLastMove();
|
return board.isHighlighted(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toggleAI(boolean isWhite) {
|
public void undoLastMove() {
|
||||||
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
board.undoLastMove();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void toggleAI(boolean isWhite) {
|
||||||
|
this.activationAIFlags[isWhite ? 1 : 0] = !this.activationAIFlags[isWhite ? 1 : 0];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue