|
|
@ -14,6 +14,12 @@ import javafx.scene.input.*;
|
|
|
|
import javafx.scene.image.*;
|
|
|
|
import javafx.scene.image.*;
|
|
|
|
import javafx.geometry.*;
|
|
|
|
import javafx.geometry.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO:
|
|
|
|
|
|
|
|
// Prevent moving into check
|
|
|
|
|
|
|
|
// Add castling
|
|
|
|
|
|
|
|
// Add en passant
|
|
|
|
|
|
|
|
// Give feedback for check, checkmate, and stalemate.
|
|
|
|
|
|
|
|
|
|
|
|
//Main class and GUI
|
|
|
|
//Main class and GUI
|
|
|
|
|
|
|
|
|
|
|
|
public class Chess extends Application {
|
|
|
|
public class Chess extends Application {
|
|
|
@ -27,6 +33,7 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
|
|
private GraphicsContext graphics;
|
|
|
|
private GraphicsContext graphics;
|
|
|
|
private TextArea moveHistoryTextArea;
|
|
|
|
private TextArea moveHistoryTextArea;
|
|
|
|
|
|
|
|
private Label feedback;
|
|
|
|
|
|
|
|
|
|
|
|
//Load images
|
|
|
|
//Load images
|
|
|
|
private static final Image W_ROOK = new Image("img/w_rook_png_shadow_128px.png");
|
|
|
|
private static final Image W_ROOK = new Image("img/w_rook_png_shadow_128px.png");
|
|
|
@ -71,14 +78,20 @@ public class Chess extends Application {
|
|
|
|
hb.setAlignment(Pos.CENTER);
|
|
|
|
hb.setAlignment(Pos.CENTER);
|
|
|
|
root.getChildren().add(hb);
|
|
|
|
root.getChildren().add(hb);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Add a label for feedback
|
|
|
|
|
|
|
|
feedback = new Label("");
|
|
|
|
|
|
|
|
HBox hb2 = new HBox(20, feedback);
|
|
|
|
|
|
|
|
hb2.setAlignment(Pos.CENTER);
|
|
|
|
|
|
|
|
root.getChildren().add(hb2);
|
|
|
|
|
|
|
|
|
|
|
|
//Set up buttons
|
|
|
|
//Set up buttons
|
|
|
|
Button undo = new Button("Undo");
|
|
|
|
Button undo = new Button("Undo");
|
|
|
|
Button reset = new Button("Reset");
|
|
|
|
Button reset = new Button("Reset");
|
|
|
|
Button aiMove = new Button("AI move");
|
|
|
|
Button aiMove = new Button("AI move");
|
|
|
|
HBox h = new HBox(20, undo, reset, aiMove);
|
|
|
|
HBox hb3 = new HBox(20, undo, reset, aiMove);
|
|
|
|
h.setAlignment(Pos.CENTER);
|
|
|
|
hb3.setAlignment(Pos.CENTER);
|
|
|
|
h.setPadding(new Insets(10,10,10,10));
|
|
|
|
hb3.setPadding(new Insets(10,10,10,10));
|
|
|
|
root.getChildren().add(h);
|
|
|
|
root.getChildren().add(hb3);
|
|
|
|
|
|
|
|
|
|
|
|
//Set up empty move history, game history, notation history.
|
|
|
|
//Set up empty move history, game history, notation history.
|
|
|
|
gameStateHistory = new ArrayList<GameState>();
|
|
|
|
gameStateHistory = new ArrayList<GameState>();
|
|
|
@ -107,10 +120,25 @@ public class Chess extends Application {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void updateScreen() {
|
|
|
|
private void updateScreen() {
|
|
|
|
|
|
|
|
//Draw the board
|
|
|
|
for(int x = 0; x < 8; x++) {
|
|
|
|
for(int x = 0; x < 8; x++) {
|
|
|
|
for(int y = 0; y < 8; y++) {
|
|
|
|
for(int y = 0; y < 8; y++) {
|
|
|
|
graphics.setFill((x + y) % 2 == 0 ? Color.BURLYWOOD : Color.FORESTGREEN);
|
|
|
|
graphics.setFill((x + y) % 2 == 0 ? Color.BURLYWOOD : Color.FORESTGREEN);
|
|
|
|
graphics.fillRect(50*x, 50*y, 50, 50);
|
|
|
|
graphics.fillRect(50*x, 50*y, 50, 50);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Show the last move made in red
|
|
|
|
|
|
|
|
if (!moveHistory.isEmpty()) {
|
|
|
|
|
|
|
|
graphics.setFill(new Color(1, 0, 0, .6));
|
|
|
|
|
|
|
|
Move m = moveHistory.get(moveHistory.size() - 1);
|
|
|
|
|
|
|
|
graphics.fillRect(50*m.getSourceCol(), 50*m.getSourceRow(), 50, 50);
|
|
|
|
|
|
|
|
graphics.fillRect(50*m.getDestCol(), 50*m.getDestRow(), 50, 50);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Draw the pieces
|
|
|
|
|
|
|
|
for(int x = 0; x < 8; x++) {
|
|
|
|
|
|
|
|
for(int y = 0; y < 8; y++) {
|
|
|
|
Square s = board().getSquare(x, y);
|
|
|
|
Square s = board().getSquare(x, y);
|
|
|
|
if ( s.getType() == Square.PAWN && s.isWhite()) graphics.drawImage(W_PAWN, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.PAWN && s.isWhite()) graphics.drawImage(W_PAWN, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.PAWN && !s.isWhite()) graphics.drawImage(B_PAWN, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.PAWN && !s.isWhite()) graphics.drawImage(B_PAWN, 50*x, 50*y, 50, 50);
|
|
|
@ -124,8 +152,18 @@ public class Chess extends Application {
|
|
|
|
if ( s.getType() == Square.QUEEN && !s.isWhite()) graphics.drawImage(B_QUEEN, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.QUEEN && !s.isWhite()) graphics.drawImage(B_QUEEN, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.KING && s.isWhite()) graphics.drawImage(W_KING, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.KING && s.isWhite()) graphics.drawImage(W_KING, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.KING && !s.isWhite()) graphics.drawImage(B_KING, 50*x, 50*y, 50, 50);
|
|
|
|
if ( s.getType() == Square.KING && !s.isWhite()) graphics.drawImage(B_KING, 50*x, 50*y, 50, 50);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Highlight the selected square (if any) in yellow
|
|
|
|
|
|
|
|
if (clickRow != null) {
|
|
|
|
|
|
|
|
graphics.setFill(new Color(1, 1, 0, .5));
|
|
|
|
|
|
|
|
graphics.fillRect(50*clickCol, 50*clickRow, 50, 50);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Update feedback
|
|
|
|
|
|
|
|
feedback.setText(board().getFeedback());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private GameState board() {
|
|
|
|
private GameState board() {
|
|
|
@ -134,8 +172,6 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
|
|
//Reset to the intial game position.
|
|
|
|
//Reset to the intial game position.
|
|
|
|
public void reset(ActionEvent e) {
|
|
|
|
public void reset(ActionEvent e) {
|
|
|
|
System.out.println(board().getAllPossibleMoves());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Clear out all the histories
|
|
|
|
//Clear out all the histories
|
|
|
|
gameStateHistory = new ArrayList<GameState>();
|
|
|
|
gameStateHistory = new ArrayList<GameState>();
|
|
|
|
gameStateHistory.add(new GameState()); //Set up the initial board
|
|
|
|
gameStateHistory.add(new GameState()); //Set up the initial board
|
|
|
@ -173,6 +209,8 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
|
|
//Refresh the board
|
|
|
|
//Refresh the board
|
|
|
|
updateScreen();
|
|
|
|
updateScreen();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writeMoveHistoryForIndex(moveNotationHistory.size()-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//For the human moving
|
|
|
|
//For the human moving
|
|
|
@ -195,7 +233,6 @@ public class Chess extends Application {
|
|
|
|
|
|
|
|
|
|
|
|
//Undo button for the most recent move
|
|
|
|
//Undo button for the most recent move
|
|
|
|
public void undo(ActionEvent e) {
|
|
|
|
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.
|
|
|
|
if (gameStateHistory.size() <= 1) return; //can't pop the initial state.
|
|
|
|
|
|
|
|
|
|
|
|
//Pop those 3 history object things and update the GUI.
|
|
|
|
//Pop those 3 history object things and update the GUI.
|
|
|
@ -203,7 +240,22 @@ public class Chess extends Application {
|
|
|
|
moveHistory.remove(moveHistory.size() - 1);
|
|
|
|
moveHistory.remove(moveHistory.size() - 1);
|
|
|
|
moveNotationHistory.remove(moveNotationHistory.size() - 1);
|
|
|
|
moveNotationHistory.remove(moveNotationHistory.size() - 1);
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
clickRow = clickCol = null;
|
|
|
|
|
|
|
|
refreshMoveHistoryDisplay();
|
|
|
|
updateScreen();
|
|
|
|
updateScreen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void refreshMoveHistoryDisplay() {
|
|
|
|
|
|
|
|
moveHistoryTextArea.setText("");
|
|
|
|
|
|
|
|
for(int i = 0; i < moveHistory.size(); i++) {
|
|
|
|
|
|
|
|
writeMoveHistoryForIndex(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void writeMoveHistoryForIndex(int ply) {
|
|
|
|
|
|
|
|
GameState g = gameStateHistory.get(ply);
|
|
|
|
|
|
|
|
Move m = moveHistory.get(ply);
|
|
|
|
|
|
|
|
if (g.isWhitesTurn()) moveHistoryTextArea.appendText("\n" + (ply/2+1) +". "+m);
|
|
|
|
|
|
|
|
else moveHistoryTextArea.appendText(" " +m);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|