diff --git a/src/Board.java b/src/Board.java index d85a837..eaaa012 100644 --- a/src/Board.java +++ b/src/Board.java @@ -30,12 +30,17 @@ public class Board { ArrayList legalMoves = null; Stack moveHistory; public boolean isGameOver; + public boolean victor; + Image youWin; + Image youLose; public Board() { moveHistory = new Stack<>(); // Initialize DrawingPanel panel = new DrawingPanel(DIMENSION, DIMENSION); + youWin = panel.loadImage("you-win.png"); + youLose = panel.loadImage("you-lose.png"); graphics = panel.getGraphics(); // Connect up event handlers @@ -46,6 +51,11 @@ public class Board { }); panel.onMouseUp(this::handleMouseUp); + // Initialize board + setup(); + } + + public void setup() { // Initialize board board = new Piece[BOARD_SIZE][BOARD_SIZE]; @@ -131,6 +141,11 @@ public class Board { // Mouse down event handler // This sets the currently dragging piece void handleMouseDown(int x, int y) { + if (isGameOver) { + isGameOver = false; + setup(); + return; + } // Get board coordinate of mouse click BoardCoordinate coordinate = new ScreenCoordinate(x, y).toBoard(); // If there's no piece there, return @@ -154,23 +169,11 @@ public class Board { for (BoardCoordinate legalMove : legalMoves) { if (newCoordinate.equals(legalMove)) { move(dragging, newCoordinate); - // QUICK TESTING CODE - Piece movedPiece = get(newCoordinate); - 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; - } + checkForCheckmate(); + if (!isGameOver) { + move(ChessAI.findBestMove(this)); + checkForCheckmate(); } - if (inCheck && oppositeKing.getLegalMoves(oppositeKingPosition, this).size() == 0) - isGameOver = true; - - - move(ChessAI.findBestMove(this)); break; } } @@ -181,6 +184,23 @@ public class Board { 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() { draw(null); } @@ -224,6 +244,11 @@ public class Board { // Otherwise, render it at the center of the board tile 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 diff --git a/you-lose.png b/you-lose.png new file mode 100644 index 0000000..588a0de Binary files /dev/null and b/you-lose.png differ diff --git a/you-lose.svg b/you-lose.svg new file mode 100644 index 0000000..e6eee75 --- /dev/null +++ b/you-lose.svg @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + YOU LOSE + click to play again + + diff --git a/you-win.png b/you-win.png new file mode 100644 index 0000000..b813cd7 Binary files /dev/null and b/you-win.png differ diff --git a/you-win.svg b/you-win.svg new file mode 100644 index 0000000..51a18d0 --- /dev/null +++ b/you-win.svg @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + YOU WIN! + click to play again + +