|
|
@ -30,12 +30,17 @@ public class Board {
|
|
|
|
ArrayList<BoardCoordinate> legalMoves = null;
|
|
|
|
ArrayList<BoardCoordinate> legalMoves = null;
|
|
|
|
Stack<Move> moveHistory;
|
|
|
|
Stack<Move> moveHistory;
|
|
|
|
public boolean isGameOver;
|
|
|
|
public boolean isGameOver;
|
|
|
|
|
|
|
|
public boolean victor;
|
|
|
|
|
|
|
|
Image youWin;
|
|
|
|
|
|
|
|
Image youLose;
|
|
|
|
|
|
|
|
|
|
|
|
public Board() {
|
|
|
|
public Board() {
|
|
|
|
moveHistory = new Stack<>();
|
|
|
|
moveHistory = new Stack<>();
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize DrawingPanel
|
|
|
|
// Initialize DrawingPanel
|
|
|
|
panel = new DrawingPanel(DIMENSION, DIMENSION);
|
|
|
|
panel = new DrawingPanel(DIMENSION, DIMENSION);
|
|
|
|
|
|
|
|
youWin = panel.loadImage("you-win.png");
|
|
|
|
|
|
|
|
youLose = panel.loadImage("you-lose.png");
|
|
|
|
graphics = panel.getGraphics();
|
|
|
|
graphics = panel.getGraphics();
|
|
|
|
|
|
|
|
|
|
|
|
// Connect up event handlers
|
|
|
|
// Connect up event handlers
|
|
|
@ -46,6 +51,11 @@ public class Board {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
panel.onMouseUp(this::handleMouseUp);
|
|
|
|
panel.onMouseUp(this::handleMouseUp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize board
|
|
|
|
|
|
|
|
setup();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setup() {
|
|
|
|
// Initialize board
|
|
|
|
// Initialize board
|
|
|
|
board = new Piece[BOARD_SIZE][BOARD_SIZE];
|
|
|
|
board = new Piece[BOARD_SIZE][BOARD_SIZE];
|
|
|
|
|
|
|
|
|
|
|
@ -131,6 +141,11 @@ public class Board {
|
|
|
|
// Mouse down event handler
|
|
|
|
// Mouse down event handler
|
|
|
|
// This sets the currently dragging piece
|
|
|
|
// This sets the currently dragging piece
|
|
|
|
void handleMouseDown(int x, int y) {
|
|
|
|
void handleMouseDown(int x, int y) {
|
|
|
|
|
|
|
|
if (isGameOver) {
|
|
|
|
|
|
|
|
isGameOver = false;
|
|
|
|
|
|
|
|
setup();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
// Get board coordinate of mouse click
|
|
|
|
// Get board coordinate of mouse click
|
|
|
|
BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard();
|
|
|
|
BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard();
|
|
|
|
// If there's no piece there, return
|
|
|
|
// If there's no piece there, return
|
|
|
@ -154,23 +169,11 @@ public class Board {
|
|
|
|
for (BoardCoordinate legalMove : legalMoves) {
|
|
|
|
for (BoardCoordinate legalMove : legalMoves) {
|
|
|
|
if (newCoordinate.equals(legalMove)) {
|
|
|
|
if (newCoordinate.equals(legalMove)) {
|
|
|
|
move(dragging, newCoordinate);
|
|
|
|
move(dragging, newCoordinate);
|
|
|
|
// QUICK TESTING CODE
|
|
|
|
checkForCheckmate();
|
|
|
|
Piece movedPiece = get(newCoordinate);
|
|
|
|
if (!isGameOver) {
|
|
|
|
King oppositeKing = movedPiece.black ? whiteKing : blackKing;
|
|
|
|
|
|
|
|
BoardCoordinate oppositeKingPosition = null;
|
|
|
|
|
|
|
|
boolean inCheck = false;
|
|
|
|
|
|
|
|
for (BoardCoordinate move : movedPiece.getLegalMoves(newCoordinate, this)) {
|
|
|
|
|
|
|
|
if (get(move) == oppositeKing) {
|
|
|
|
|
|
|
|
oppositeKingPosition = move;
|
|
|
|
|
|
|
|
inCheck = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0)
|
|
|
|
|
|
|
|
isGameOver = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
move(ChessAI.findBestMove(this));
|
|
|
|
move(ChessAI.findBestMove(this));
|
|
|
|
|
|
|
|
checkForCheckmate();
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -181,6 +184,23 @@ public class Board {
|
|
|
|
draw();
|
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void checkForCheckmate() {
|
|
|
|
|
|
|
|
BoardCoordinate movedCoordinate = moveHistory.peek().to;
|
|
|
|
|
|
|
|
Piece movedPiece = get(movedCoordinate);
|
|
|
|
|
|
|
|
King oppositeKing = movedPiece.black ? whiteKing : blackKing;
|
|
|
|
|
|
|
|
BoardCoordinate oppositeKingPosition = null;
|
|
|
|
|
|
|
|
boolean inCheck = false;
|
|
|
|
|
|
|
|
for (BoardCoordinate move : movedPiece.getLegalMoves(movedCoordinate, this)) {
|
|
|
|
|
|
|
|
if (get(move) == oppositeKing) {
|
|
|
|
|
|
|
|
oppositeKingPosition = move;
|
|
|
|
|
|
|
|
inCheck = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
isGameOver = inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0;
|
|
|
|
|
|
|
|
victor = movedPiece.black;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void draw() {
|
|
|
|
public void draw() {
|
|
|
|
draw(null);
|
|
|
|
draw(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -224,6 +244,11 @@ public class Board {
|
|
|
|
// Otherwise, render it at the center of the board tile
|
|
|
|
// Otherwise, render it at the center of the board tile
|
|
|
|
piece.draw(graphics, panel, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen());
|
|
|
|
piece.draw(graphics, panel, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Draw game over text
|
|
|
|
|
|
|
|
if (isGameOver) {
|
|
|
|
|
|
|
|
graphics.drawImage(victor ? youLose : youWin, 0, 0, panel);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Functional interfaces for forEachPiece
|
|
|
|
// Functional interfaces for forEachPiece
|
|
|
|