diff --git a/src/backend/Board.java b/src/backend/Board.java index 4a5441b..c003f94 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -20,6 +20,7 @@ public class Board { this.selectedY = -1; this.turnNumber = 0; this.isTurnWhite = true; + Move.clearHighlightedPositions(); // Ensure no highlights at start } public int getWidth() { @@ -95,6 +96,7 @@ public class Board { selectedY = -1; turnNumber = 0; isTurnWhite = true; + Move.clearHighlightedPositions(); } public String toString() { @@ -130,47 +132,62 @@ public class Board { return; } + Piece clickedPiece = getPieceAt(x, y); if (selectedX == -1 && selectedY == -1) { - Piece piece = getPieceAt(x, y); - if (piece != null) { - System.out.println("Selected piece at (" + x + "," + y + "): " + piece.getType() + ", isWhite=" + piece.isWhite()); + if (clickedPiece != null) { + System.out.println("Selected piece at (" + x + "," + y + "): " + clickedPiece.getType() + ", isWhite=" + clickedPiece.isWhite()); selectedX = x; selectedY = y; + Move.setHighlightedPositions(this, clickedPiece); } else { System.out.println("No piece at (" + x + "," + y + ")"); } } else { System.out.println("Selected position: (" + selectedX + "," + selectedY + ")"); + Piece selectedPiece = getPieceAt(selectedX, selectedY); + if (selectedPiece == null) { + System.out.println("No piece at (" + selectedX + "," + selectedY + ")"); + selectedX = -1; + selectedY = -1; + Move.clearHighlightedPositions(); + return; + } + // If clicking the same position, unselect if (selectedX == x && selectedY == y) { System.out.println("Unselecting piece"); selectedX = -1; selectedY = -1; - } else { - Piece selectedPiece = getPieceAt(selectedX, selectedY); - if (selectedPiece != null) { - if (!selectedPiece.canMoveTo(x, y, this)) { - System.out.println("Invalid move for " + selectedPiece.getType() + " from (" + - selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); - selectedX = -1; - selectedY = -1; - return; - } - System.out.println("Moving piece from (" + selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); - Piece pieceAtDestination = getPieceAt(x, y); - if (pieceAtDestination != null) { - System.out.println("Capturing piece at destination: " + pieceAtDestination.getType() + ", isWhite=" + pieceAtDestination.isWhite()); - } else { - System.out.println("No piece at destination"); - } - Move move = new Move(selectedX, selectedY, x, y, selectedPiece, pieceAtDestination); - playMove(move); - selectedX = -1; - selectedY = -1; - } else { - System.out.println("No piece at (" + selectedX + "," + selectedY + ")"); + Move.clearHighlightedPositions(); + } + // If clicking another piece of the same color, select that piece instead + else if (clickedPiece != null && clickedPiece.isWhite() == selectedPiece.isWhite()) { + System.out.println("Switching selection to piece at (" + x + "," + y + "): " + clickedPiece.getType() + ", isWhite=" + clickedPiece.isWhite()); + selectedX = x; + selectedY = y; + Move.setHighlightedPositions(this, clickedPiece); + } + // Otherwise, attempt to move + else { + if (!selectedPiece.canMoveTo(x, y, this)) { + System.out.println("Invalid move for " + selectedPiece.getType() + " from (" + + selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); selectedX = -1; selectedY = -1; + Move.clearHighlightedPositions(); + return; } + System.out.println("Moving piece from (" + selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); + Piece pieceAtDestination = getPieceAt(x, y); + if (pieceAtDestination != null) { + System.out.println("Capturing piece at destination: " + pieceAtDestination.getType() + ", isWhite=" + pieceAtDestination.isWhite()); + } else { + System.out.println("No piece at destination"); + } + Move move = new Move(selectedX, selectedY, x, y, selectedPiece, pieceAtDestination); + playMove(move); + selectedX = -1; + selectedY = -1; + Move.clearHighlightedPositions(); } } } @@ -179,6 +196,10 @@ public class Board { return selectedX == x && selectedY == y; } + public boolean isHighlighted(int x, int y) { + return Move.isHighlighted(x, y); + } + public void playMove(Move move) { System.out.println("playMove: Moving from (" + move.getFromX() + "," + move.getFromY() + ") to (" + move.getToX() + "," + move.getToY() + ")"); @@ -215,10 +236,6 @@ public class Board { // TODO } - public boolean isHighlighted(int x, int y) { - return false; - } - public void undoLastMove() { // TODO } diff --git a/src/backend/Move.java b/src/backend/Move.java index 57f6502..4578bbe 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -7,6 +7,7 @@ public class Move { private int toY; private Piece pieceMoved; private Piece pieceCaptured; + private static String[] highlightedPositions = new String[0]; // Store highlighted positions public Move(int fromX, int fromY, int toX, int toY, Piece pieceMoved, Piece pieceCaptured) { this.fromX = fromX; @@ -41,6 +42,31 @@ public class Move { return pieceCaptured; } + // Static method to calculate and set highlighted positions for a piece + public static void setHighlightedPositions(Board board, Piece piece) { + if (piece == null) { + highlightedPositions = new String[0]; + } else { + highlightedPositions = calculateValidMoves(board, piece); + } + } + + // Static method to clear highlighted positions + public static void clearHighlightedPositions() { + highlightedPositions = new String[0]; + } + + // Static method to check if a position is highlighted + public static boolean isHighlighted(int x, int y) { + String position = x + "," + y; + for (int i = 0; i < highlightedPositions.length; i++) { + if (highlightedPositions[i] != null && highlightedPositions[i].equals(position)) { + return true; + } + } + return false; + } + // Static method to calculate all valid moves for a piece public static String[] calculateValidMoves(Board board, Piece piece) { String[] validMoves = new String[32]; // Allocate space for up to 32 moves