diff --git a/src/Board.java b/src/Board.java index 0722d8c..e04fd3b 100644 --- a/src/Board.java +++ b/src/Board.java @@ -30,11 +30,9 @@ 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<>(); @@ -43,7 +41,6 @@ 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 @@ -163,7 +160,6 @@ public class Board { void handleMouseDown(int x, int y) { if (isGameOver) { isGameOver = false; - isStalemate = false; setup(); return; } @@ -227,14 +223,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; } @@ -288,7 +284,7 @@ public class Board { // Draw game over text if (isGameOver) { - graphics.drawImage(isStalemate ? stalemate : (victor ? youLose : youWin), 0, 0, panel); + graphics.drawImage(victor ? youLose : youWin, 0, 0, panel); } } diff --git a/src/ChessAI.java b/src/ChessAI.java index 9d3c1cd..bf20af5 100644 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -1,6 +1,3 @@ -import java.util.ArrayList; -import java.util.Collections; - public class ChessAI { private static final int MAX_DEPTH = 3; @@ -8,9 +5,7 @@ public class ChessAI { int bestScore = Integer.MIN_VALUE; Move bestMove = null; - ArrayList legalMoves = board.getAllLegalMoves(true); - Collections.shuffle(legalMoves); - for (Move move : legalMoves) { + for (Move move : board.getAllLegalMoves(true)) { 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 8974601..5058adc 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -13,7 +13,9 @@ public class Pawn extends Piece { ArrayList possibleMoves = new ArrayList<>(); if (this.black) { if (board.get(position.x, position.y + 1) == null) { - possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); + Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y + 1)); + oneForward.isPromotion = position.y + 1 == Board.BOARD_SIZE - 1; + possibleMoves.add(oneForward); if (!moved && board.get(position.x, position.y + 2) == null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); } @@ -24,10 +26,12 @@ 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) { - possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); + Move oneForward = new Move(position, new BoardCoordinate(position.x, position.y - 1)); + oneForward.isPromotion = position.y - 1 == 0; + possibleMoves.add(oneForward); if (!moved && board.get(position.x, position.y - 2) == null) { possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); } @@ -38,7 +42,6 @@ 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 deleted file mode 100644 index 895fc02..0000000 Binary files a/stalemate.png and /dev/null differ diff --git a/stalemate.svg b/stalemate.svg deleted file mode 100644 index bc78b9d..0000000 --- a/stalemate.svg +++ /dev/null @@ -1,117 +0,0 @@ - - - - - - - - - - - - - - - - - - STALEMATE - click to play again - - diff --git a/you-lose.svg b/you-lose.svg index eb7c42b..e6eee75 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-lose.png" + inkscape:export-filename="you-win.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="786.78891" - inkscape:cy="981.56402" + inkscape:cx="1232.7214" + inkscape:cy="1014.8808" 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="tspan1801">YOU LOSE