|
|
|
@ -23,7 +23,7 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
//User's first click for a move.
|
|
|
|
|
private Integer clickRow; //null means no first click made currently.
|
|
|
|
|
private Integer clickColumn;
|
|
|
|
|
private Integer clickCol;
|
|
|
|
|
|
|
|
|
|
private GraphicsContext graphics;
|
|
|
|
|
private TextArea moveHistoryTextArea;
|
|
|
|
@ -87,7 +87,7 @@ public class Chess extends Application {
|
|
|
|
|
moveNotationHistory = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
//No clicks yet
|
|
|
|
|
clickRow = clickColumn = null;
|
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
|
|
|
|
|
|
//Set up mouse/click events
|
|
|
|
|
board.setOnMousePressed(this::mouseClick);
|
|
|
|
@ -96,7 +96,7 @@ public class Chess extends Application {
|
|
|
|
|
aiMove.setOnAction(this::moveAI);
|
|
|
|
|
|
|
|
|
|
//Refresh the board
|
|
|
|
|
paintBoard();
|
|
|
|
|
updateScreen();
|
|
|
|
|
|
|
|
|
|
//Fix mysterious broken width and prevent shrinking window
|
|
|
|
|
stage.setWidth(664);
|
|
|
|
@ -106,7 +106,7 @@ public class Chess extends Application {
|
|
|
|
|
stage.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void paintBoard() {
|
|
|
|
|
private void updateScreen() {
|
|
|
|
|
for(int x = 0; x < 8; x++) {
|
|
|
|
|
for(int y = 0; y < 8; y++) {
|
|
|
|
|
graphics.setFill((x + y) % 2 == 0 ? Color.BURLYWOOD : Color.FORESTGREEN);
|
|
|
|
@ -134,37 +134,76 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
//Reset to the intial game position.
|
|
|
|
|
public void reset(ActionEvent e) {
|
|
|
|
|
System.out.println(board().getAllPossibleMoves() );
|
|
|
|
|
//Set up the initial board
|
|
|
|
|
//Clear out clickRo and clickCol
|
|
|
|
|
//Clera out all the histories.
|
|
|
|
|
System.out.println(board().getAllPossibleMoves());
|
|
|
|
|
|
|
|
|
|
//Clear out all the histories
|
|
|
|
|
gameStateHistory = new ArrayList<GameState>();
|
|
|
|
|
gameStateHistory.add(new GameState()); //Set up the initial board
|
|
|
|
|
moveHistory = new ArrayList<Move>();
|
|
|
|
|
moveNotationHistory = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
//Clear out clickRow and clickCol
|
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
|
moveHistoryTextArea.setText("");
|
|
|
|
|
updateScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//AI makes a move
|
|
|
|
|
public void moveAI(ActionEvent e) {
|
|
|
|
|
//Ask the gameState for a list of all legal moves.
|
|
|
|
|
ArrayList<Move> a = board().getAllPossibleMoves();
|
|
|
|
|
if (a.size() <= 0) return; //no moves.
|
|
|
|
|
|
|
|
|
|
//Get one of those in the list uniformly at random.
|
|
|
|
|
// makeMove(...);
|
|
|
|
|
int r = (int)(Math.random() * a.size());
|
|
|
|
|
makeMove(a.get(r));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void makeMove(Move move) {
|
|
|
|
|
//Clone the gameState object for the current state of the game, tell it to make the move, add it to the move history
|
|
|
|
|
//add the notation for it
|
|
|
|
|
GameState g = board().getStateAfterMove(move);
|
|
|
|
|
if (g==null) return; //didn't actually move it; must not have been legal.
|
|
|
|
|
gameStateHistory.add(g);
|
|
|
|
|
|
|
|
|
|
//add the move to the history as well
|
|
|
|
|
moveHistory.add(move);
|
|
|
|
|
|
|
|
|
|
//add the notation for it
|
|
|
|
|
moveNotationHistory.add(move+"");
|
|
|
|
|
|
|
|
|
|
//Refresh the board
|
|
|
|
|
updateScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//For the human moving
|
|
|
|
|
public void mouseClick(MouseEvent e) {
|
|
|
|
|
//If it is the first click (null check) then record it, updtae the GUI to show highlighting
|
|
|
|
|
//else attempt the move:
|
|
|
|
|
int x = (int)(e.getX() / 50);
|
|
|
|
|
int y = (int)(e.getY() / 50);
|
|
|
|
|
|
|
|
|
|
// makeMove(...);
|
|
|
|
|
if (board().getSquare(x,y).isWhite() == board().isWhitesTurn() && board().getSquare(x,y).getType() != Square.EMPTY) { //if clicking on own piece...
|
|
|
|
|
clickRow = y;
|
|
|
|
|
clickCol = x;
|
|
|
|
|
}
|
|
|
|
|
else if (clickRow != null) {
|
|
|
|
|
makeMove(new Move(clickCol, clickRow, x, y));
|
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
|
}
|
|
|
|
|
updateScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Undo button for the most recent move
|
|
|
|
|
public void undo(ActionEvent e) {
|
|
|
|
|
//Pop those 3 history object things and update the GUI.
|
|
|
|
|
if (gameStateHistory.size() <= 1) return; //can't pop the initial state.
|
|
|
|
|
|
|
|
|
|
//Pop those 3 history object things and update the GUI.
|
|
|
|
|
gameStateHistory.remove(gameStateHistory.size() - 1);
|
|
|
|
|
moveHistory.remove(moveHistory.size() - 1);
|
|
|
|
|
moveNotationHistory.remove(moveNotationHistory.size() - 1);
|
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
|
updateScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|