diff --git a/src/Board.java b/src/Board.java index e04fd3b..0722d8c 100644 --- a/src/Board.java +++ b/src/Board.java @@ -30,9 +30,11 @@ public class Board { ArrayList legalMoves = null; Stack moveHistory; public boolean isGameOver; + public boolean isStalemate; public boolean victor; Image youWin; Image youLose; + Image stalemate; public Board() { moveHistory = new Stack<>(); @@ -41,6 +43,7 @@ public class Board { panel = new DrawingPanel(DIMENSION, DIMENSION); youWin = panel.loadImage("you-win.png"); youLose = panel.loadImage("you-lose.png"); + stalemate = panel.loadImage("stalemate.png"); graphics = panel.getGraphics(); // Connect up event handlers @@ -160,6 +163,7 @@ public class Board { void handleMouseDown(int x, int y) { if (isGameOver) { isGameOver = false; + isStalemate = false; setup(); return; } @@ -223,14 +227,14 @@ public class Board { if (inCheck) { isGameOver = true; for (Move move : getAllLegalMoves(!movedPiece.black)) { - move(move); if (!oppositeKing.isInCheck(move, this)) { isGameOver = false; - undoMove(); break; } - undoMove(); } + } else if (getAllLegalMoves(movedPiece.black).isEmpty()) { + isGameOver = true; + isStalemate = true; } victor = movedPiece.black; } @@ -284,7 +288,7 @@ public class Board { // Draw game over text if (isGameOver) { - graphics.drawImage(victor ? youLose : youWin, 0, 0, panel); + graphics.drawImage(isStalemate ? stalemate : (victor ? youLose : youWin), 0, 0, panel); } } diff --git a/src/ChessAI.java b/src/ChessAI.java index bf20af5..9d3c1cd 100644 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.Collections; + public class ChessAI { private static final int MAX_DEPTH = 3; @@ -5,7 +8,9 @@ public class ChessAI { int bestScore = Integer.MIN_VALUE; Move bestMove = null; - for (Move move : board.getAllLegalMoves(true)) { + ArrayList legalMoves = board.getAllLegalMoves(true); + Collections.shuffle(legalMoves); + for (Move move : legalMoves) { board.move(move); int score = minimax(board, MAX_DEPTH, Integer.MIN_VALUE, Integer.MAX_VALUE, false); board.undoMove(); diff --git a/src/Pawn.java b/src/Pawn.java index 5058adc..8974601 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -13,9 +13,7 @@ public class Pawn extends Piece { ArrayList possibleMoves = new ArrayList<>(); if (this.black) { if (board.get(position.x, position.y + 1) == null) { - Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y + 1)); - oneForward.isPromotion = position.y + 1 == Board.BOARD_SIZE - 1; - possibleMoves.add(oneForward); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); if (!moved && board.get(position.x, position.y + 2) == null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); } @@ -26,12 +24,10 @@ public class Pawn extends Piece { if (board.get(position.x + 1, position.y + 1) != null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1,position.y + 1))); } - + for (Move move : possibleMoves) move.isPromotion = position.y + 1 == 0; } else { if (board.get(position.x, position.y - 1) == null) { - Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y - 1)); - oneForward.isPromotion = position.y - 1 == 0; - possibleMoves.add(oneForward); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); if (!moved && board.get(position.x, position.y - 2) == null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); } @@ -42,6 +38,7 @@ public class Pawn extends Piece { if (board.get(position.x + 1, position.y - 1) != null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1,position.y - 1))); } + for (Move move : possibleMoves) move.isPromotion = position.y - 1 == 0; } return possibleMoves; } diff --git a/stalemate.png b/stalemate.png new file mode 100644 index 0000000..895fc02 Binary files /dev/null and b/stalemate.png differ diff --git a/stalemate.svg b/stalemate.svg new file mode 100644 index 0000000..bc78b9d --- /dev/null +++ b/stalemate.svg @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + STALEMATE + click to play again + + diff --git a/you-lose.svg b/you-lose.svg index e6eee75..eb7c42b 100644 --- a/you-lose.svg +++ b/you-lose.svg @@ -8,7 +8,7 @@ version="1.1" id="svg5" sodipodi:docname="you-lose.svg" - inkscape:export-filename="you-win.png" + inkscape:export-filename="you-lose.png" inkscape:export-xdpi="25.4" inkscape:export-ydpi="25.4" inkscape:version="1.2.2 (b0a8486541, 2022-12-01)" @@ -28,8 +28,8 @@ inkscape:document-units="mm" showgrid="false" inkscape:zoom="0.3901936" - inkscape:cx="1232.7214" - inkscape:cy="1014.8808" + inkscape:cx="786.78891" + inkscape:cy="981.56402" inkscape:window-width="1920" inkscape:window-height="1048" inkscape:window-x="1920" @@ -99,9 +99,9 @@ style="font-size:341.333px;white-space:pre;shape-inside:url(#rect952);display:inline;fill:#3d3d3d;fill-opacity:1;filter:url(#filter1272)">YOU LOSE + id="tspan1821">YOU LOSE