From c9cf5fa8fb19521f7488fea1151d7bd40f31599a Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Wed, 15 Mar 2023 18:43:33 -0700 Subject: [PATCH] Switch from BoardCoordinate to Move for most move handling --- src/Bishop.java | 4 ++-- src/Board.java | 49 +++++++++++++++++++++++++++++++------------------ src/King.java | 23 +++++++++++++---------- src/Knight.java | 21 ++++++++++----------- src/Move.java | 6 +++++- src/Pawn.java | 12 ++++++------ src/Piece.java | 33 +++++++++++++++++---------------- src/Queen.java | 4 ++-- src/Rook.java | 5 ++--- 9 files changed, 88 insertions(+), 69 deletions(-) diff --git a/src/Bishop.java b/src/Bishop.java index 85c5b06..74f9cd7 100644 --- a/src/Bishop.java +++ b/src/Bishop.java @@ -9,8 +9,8 @@ public class Bishop extends Piece { return 3; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); getPossibleMovesInDirection(1, 1, position, board, possibleMoves); getPossibleMovesInDirection(-1, -1, position, board, possibleMoves); diff --git a/src/Board.java b/src/Board.java index 74ff175..51833ab 100644 --- a/src/Board.java +++ b/src/Board.java @@ -27,7 +27,7 @@ public class Board { Piece[][] board; // The current board coordinate that's being dragged BoardCoordinate dragging = null; - ArrayList legalMoves = null; + ArrayList legalMoves = null; Stack moveHistory; public boolean isGameOver; public boolean victor; @@ -134,8 +134,14 @@ public class Board { public void undoMove() { if (moveHistory.isEmpty()) return; Move lastMove = moveHistory.pop(); - set(lastMove.from, get(lastMove.to)); - set(lastMove.to, lastMove.captured); + undoMove(lastMove); + undoMove(lastMove.submove); + } + + void undoMove(Move move) { + if (move == null) return; + set(move.from, get(move.to)); + set(move.to, move.captured); } // Mouse down event handler @@ -158,6 +164,10 @@ public class Board { draw(x, y); } + void setLastMovedPieceAsMoved() { + get(moveHistory.peek().to).moved = true; + } + void handleMouseUp(int x, int y) { // Get board coordinate of mouse release BoardCoordinate newCoordinate = new ScreenCoordinate(x, y).toBoard(); @@ -165,13 +175,15 @@ public class Board { if (dragging != null && !newCoordinate.equals(dragging)) { // dragging is BoardCoordinate of piece being dragged Piece piece = get(dragging); - ArrayList legalMoves = piece.getLegalMoves(dragging, this); - for (BoardCoordinate legalMove : legalMoves) { - if (newCoordinate.equals(legalMove)) { + ArrayList legalMoves = piece.getLegalMoves(dragging, this); + for (Move legalMove : legalMoves) { + if (newCoordinate.equals(legalMove.to)) { move(dragging, newCoordinate); + setLastMovedPieceAsMoved(); checkForCheckmate(); if (!isGameOver) { move(ChessAI.findBestMove(this)); + setLastMovedPieceAsMoved(); checkForCheckmate(); } break; @@ -190,9 +202,9 @@ public class Board { 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; + for (Move move : movedPiece.getLegalMoves(movedCoordinate, this)) { + if (get(move.to) == oppositeKing) { + oppositeKingPosition = move.to; inCheck = true; break; } @@ -238,13 +250,16 @@ public class Board { drawRect(x, y); if (dragging != null) { graphics.setColor(new Color(0, 128, 0, 128)); - for (BoardCoordinate legalMove : legalMoves) - drawRect(legalMove); + for (Move legalMove : legalMoves) + drawRect(legalMove.to); if (mousePosition != null) { BoardCoordinate hovering = mousePosition.toBoard(); - if (legalMoves.contains(hovering)) { - graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128)); - drawRect(mousePosition.toBoard()); + for (Move legalMove : legalMoves) { + if (legalMove.to.equals(hovering)) { + graphics.setColor(get(hovering) == null ? new Color(0, 0, 255, 128) : new Color(255, 0, 0, 128)); + drawRect(mousePosition.toBoard()); + break; + } } } } @@ -302,10 +317,8 @@ public class Board { ArrayList allLegalMoves = new ArrayList<>(); forEachPiece((from, piece) -> { if (piece.black != black) return; - ArrayList legalTiles = piece.getLegalMoves(from, this); - for (BoardCoordinate to : legalTiles) { - allLegalMoves.add(new Move(from, to)); - } + ArrayList legalTiles = piece.getLegalMoves(from, this); + allLegalMoves.addAll(legalTiles); }); return allLegalMoves; } diff --git a/src/King.java b/src/King.java index cbf987a..95ed8f2 100644 --- a/src/King.java +++ b/src/King.java @@ -9,16 +9,19 @@ public class King extends Piece { return 12; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); - possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 1)); - possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 1)); - possibleMoves.add(new BoardCoordinate(position.x - 1, position.y)); - possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 1)); - possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 1)); - possibleMoves.add(new BoardCoordinate(position.x + 1, position.y)); - possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); - possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); + + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y - 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y + 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y - 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y + 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); + Piece rightRook = board.get(position.x + 3, position.y); + return possibleMoves; } } \ No newline at end of file diff --git a/src/Knight.java b/src/Knight.java index c9ed6e4..2af73ac 100644 --- a/src/Knight.java +++ b/src/Knight.java @@ -10,17 +10,16 @@ public class Knight extends Piece { return 3; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); - possibleMoves.add(new BoardCoordinate(position.x - 2, position.y - 1)); - possibleMoves.add(new BoardCoordinate(position.x - 1, position.y - 2)); - possibleMoves.add(new BoardCoordinate(position.x + 1, position.y - 2)); - possibleMoves.add(new BoardCoordinate(position.x + 2, position.y - 1)); - possibleMoves.add(new BoardCoordinate(position.x + 2, position.y + 1)); - possibleMoves.add(new BoardCoordinate(position.x + 1, position.y + 2)); - possibleMoves.add(new BoardCoordinate(position.x - 1, position.y + 2)); - possibleMoves.add(new BoardCoordinate(position.x - 2, position.y + 1)); - // test + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 2, position.y - 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y - 2))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y - 2))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 2, position.y - 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 2, position.y + 1))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y + 2))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 1, position.y + 2))); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x - 2, position.y + 1))); return possibleMoves; } } diff --git a/src/Move.java b/src/Move.java index a8f9c59..13bbd1f 100644 --- a/src/Move.java +++ b/src/Move.java @@ -1,7 +1,11 @@ +import java.util.ArrayList; + public class Move { public BoardCoordinate from; public BoardCoordinate to; public Piece captured; + // e.g. castling, castle move. Done after. + public Move submove; public Move(BoardCoordinate from, BoardCoordinate to) { this.from = from; @@ -13,4 +17,4 @@ public class Move { this.to = to; this.captured = captured; } -} +} \ No newline at end of file diff --git a/src/Pawn.java b/src/Pawn.java index 8c30774..ecbd95f 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -9,14 +9,14 @@ public class Pawn extends Piece { return 1; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); if (this.black) { - possibleMoves.add(new BoardCoordinate(position.x, position.y + 1)); - if (position.y == 1) possibleMoves.add(new BoardCoordinate(position.x, position.y + 2)); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); + if (!moved) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 2))); } else { - possibleMoves.add(new BoardCoordinate(position.x, position.y - 1)); - if (position.y == 6) possibleMoves.add(new BoardCoordinate(position.x, position.y - 2)); + possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); + if (!moved) possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 2))); } return possibleMoves; } diff --git a/src/Piece.java b/src/Piece.java index c0579f0..9309ce8 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -8,6 +8,7 @@ public abstract class Piece { static final int DIMENSION = 48; Image image; public boolean black; + public boolean moved = false; public abstract int getValue(); @@ -16,29 +17,29 @@ public abstract class Piece { image = panel.loadImage(black ? blackImagePath : whiteImagePath); } - public abstract ArrayList getPossibleMoves(BoardCoordinate position, Board board); + public abstract ArrayList getPossibleMoves(BoardCoordinate position, Board board); - public ArrayList getLegalMoves(BoardCoordinate position, Board board) { + public ArrayList getLegalMoves(BoardCoordinate position, Board board) { return getLegalMoves(position, board, true); } - public ArrayList getLegalMoves(BoardCoordinate position, Board board, boolean doCheckChecks) { - ArrayList legalMoves = getPossibleMoves(position, board); + public ArrayList getLegalMoves(BoardCoordinate position, Board board, boolean doCheckChecks) { + ArrayList legalMoves = getPossibleMoves(position, board); for (int i = 0; i < legalMoves.size(); i++) { - BoardCoordinate possibleMove = legalMoves.get(i); - Piece targetPiece = board.get(possibleMove); + Move possibleMove = legalMoves.get(i); + Piece targetPiece = board.get(possibleMove.to); if ( // other piece of same color (targetPiece != null && targetPiece.black == black) || // outside of bounds of board - possibleMove.x < 0 || - possibleMove.y < 0 || - possibleMove.x >= Board.BOARD_SIZE || - possibleMove.y >= Board.BOARD_SIZE || + possibleMove.to.x < 0 || + possibleMove.to.y < 0 || + possibleMove.to.x >= Board.BOARD_SIZE || + possibleMove.to.y >= Board.BOARD_SIZE || // is in check - (doCheckChecks && isInCheck(new Move(position, possibleMove), board)) + (doCheckChecks && isInCheck(new Move(position, possibleMove.to), board)) ) { legalMoves.remove(i); i--; @@ -55,9 +56,9 @@ public abstract class Piece { for (int x = 0; x < Board.BOARD_SIZE; x++) { Piece piece = board.get(x, y); if (piece == null || piece.black == black) continue; - ArrayList legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false); - for (BoardCoordinate legalMove : legalMoves) { - Piece pieceAtMove = board.get(legalMove); + ArrayList legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false); + for (Move legalMove : legalMoves) { + Piece pieceAtMove = board.get(legalMove.to); if (pieceAtMove instanceof King) { isInCheck = true; break outer; @@ -79,13 +80,13 @@ public abstract class Piece { draw(graphics, observer, coordinate.x, coordinate.y); } - void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList possibleMoves) { + void getPossibleMovesInDirection(int dx, int dy, BoardCoordinate position, Board board, ArrayList possibleMoves) { for ( int x = position.x + dx, y = position.y + dy; !board.outOfBounds(x, y); x += dx, y += dy) { BoardCoordinate coordinate = new BoardCoordinate(x, y); - possibleMoves.add(coordinate); + possibleMoves.add(new Move(position, coordinate)); if (board.get(coordinate) != null) break; } } diff --git a/src/Queen.java b/src/Queen.java index 833f34f..7451c86 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -9,8 +9,8 @@ public class Queen extends Piece { return 9; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); getPossibleMovesInDirection(0, 1, position, board, possibleMoves); getPossibleMovesInDirection(0, -1, position, board, possibleMoves); diff --git a/src/Rook.java b/src/Rook.java index e9aba63..80ea001 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -9,15 +9,14 @@ public class Rook extends Piece { return 5; } - public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { - ArrayList possibleMoves = new ArrayList<>(); + public ArrayList getPossibleMoves(BoardCoordinate position, Board board) { + ArrayList possibleMoves = new ArrayList<>(); getPossibleMovesInDirection(0, 1, position, board, possibleMoves); getPossibleMovesInDirection(0, -1, position, board, possibleMoves); getPossibleMovesInDirection(1, 0, position, board, possibleMoves); getPossibleMovesInDirection(-1, 0, position, board, possibleMoves); - return possibleMoves; } } \ No newline at end of file