112 lines
2.2 KiB
Java
112 lines
2.2 KiB
Java
package backend;
|
|
|
|
import windowInterface.MyInterface;
|
|
|
|
public class Game extends Thread {
|
|
|
|
private AutoPlayer aiPlayer;
|
|
private Board board;
|
|
private Piece newPiece;
|
|
|
|
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 int getWidth() {
|
|
return board.getWidth();
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
private void aiPlayerTurn() {
|
|
if(isAITurn()) {
|
|
board.playMove(aiPlayer.computeBestMove(new Board(board)));
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
|
this.newPiece = new Piece(x, y,type, isWhite);
|
|
}
|
|
|
|
public String[] getFileRepresentation() {
|
|
return board.toFileRep();
|
|
}
|
|
|
|
public void setLoopDelay(int delay) {
|
|
this.loopDelay = delay;
|
|
}
|
|
|
|
public void setDefaultSetup() {
|
|
board.cleanBoard();
|
|
board.populateBoard();
|
|
}
|
|
|
|
public void setBoard(String[] array) {
|
|
board = new Board(array);
|
|
}
|
|
|
|
public Iterable<Piece> getPieces() {
|
|
return board.getPieces();
|
|
}
|
|
|
|
public boolean isSelected(int x, int y) {
|
|
return board.isSelected(x, y);
|
|
}
|
|
|
|
public boolean isHighlighted(int x, int y) {
|
|
return board.isHighlighted(x, y);
|
|
}
|
|
|
|
public void undoLastMove() {
|
|
board.undoLastMove();
|
|
}
|
|
|
|
public void toggleAI(boolean isWhite) {
|
|
this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0];
|
|
}
|
|
|
|
}
|