possible moves highlighted

This commit is contained in:
cleme 2025-03-27 21:44:18 +01:00
parent 68d428058a
commit 19fd5a92c8
2 changed files with 73 additions and 30 deletions

View File

@ -20,6 +20,7 @@ public class Board {
this.selectedY = -1; this.selectedY = -1;
this.turnNumber = 0; this.turnNumber = 0;
this.isTurnWhite = true; this.isTurnWhite = true;
Move.clearHighlightedPositions(); // Ensure no highlights at start
} }
public int getWidth() { public int getWidth() {
@ -95,6 +96,7 @@ public class Board {
selectedY = -1; selectedY = -1;
turnNumber = 0; turnNumber = 0;
isTurnWhite = true; isTurnWhite = true;
Move.clearHighlightedPositions();
} }
public String toString() { public String toString() {
@ -130,47 +132,62 @@ public class Board {
return; return;
} }
Piece clickedPiece = getPieceAt(x, y);
if (selectedX == -1 && selectedY == -1) { if (selectedX == -1 && selectedY == -1) {
Piece piece = getPieceAt(x, y); if (clickedPiece != null) {
if (piece != null) { System.out.println("Selected piece at (" + x + "," + y + "): " + clickedPiece.getType() + ", isWhite=" + clickedPiece.isWhite());
System.out.println("Selected piece at (" + x + "," + y + "): " + piece.getType() + ", isWhite=" + piece.isWhite());
selectedX = x; selectedX = x;
selectedY = y; selectedY = y;
Move.setHighlightedPositions(this, clickedPiece);
} else { } else {
System.out.println("No piece at (" + x + "," + y + ")"); System.out.println("No piece at (" + x + "," + y + ")");
} }
} else { } else {
System.out.println("Selected position: (" + selectedX + "," + selectedY + ")"); 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) { if (selectedX == x && selectedY == y) {
System.out.println("Unselecting piece"); System.out.println("Unselecting piece");
selectedX = -1; selectedX = -1;
selectedY = -1; selectedY = -1;
} else { Move.clearHighlightedPositions();
Piece selectedPiece = getPieceAt(selectedX, selectedY); }
if (selectedPiece != null) { // If clicking another piece of the same color, select that piece instead
if (!selectedPiece.canMoveTo(x, y, this)) { else if (clickedPiece != null && clickedPiece.isWhite() == selectedPiece.isWhite()) {
System.out.println("Invalid move for " + selectedPiece.getType() + " from (" + System.out.println("Switching selection to piece at (" + x + "," + y + "): " + clickedPiece.getType() + ", isWhite=" + clickedPiece.isWhite());
selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); selectedX = x;
selectedX = -1; selectedY = y;
selectedY = -1; Move.setHighlightedPositions(this, clickedPiece);
return; }
} // Otherwise, attempt to move
System.out.println("Moving piece from (" + selectedX + "," + selectedY + ") to (" + x + "," + y + ")"); else {
Piece pieceAtDestination = getPieceAt(x, y); if (!selectedPiece.canMoveTo(x, y, this)) {
if (pieceAtDestination != null) { System.out.println("Invalid move for " + selectedPiece.getType() + " from (" +
System.out.println("Capturing piece at destination: " + pieceAtDestination.getType() + ", isWhite=" + pieceAtDestination.isWhite()); selectedX + "," + selectedY + ") to (" + x + "," + y + ")");
} 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 + ")");
selectedX = -1; selectedX = -1;
selectedY = -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; return selectedX == x && selectedY == y;
} }
public boolean isHighlighted(int x, int y) {
return Move.isHighlighted(x, y);
}
public void playMove(Move move) { public void playMove(Move move) {
System.out.println("playMove: Moving from (" + move.getFromX() + "," + move.getFromY() + ") to (" + System.out.println("playMove: Moving from (" + move.getFromX() + "," + move.getFromY() + ") to (" +
move.getToX() + "," + move.getToY() + ")"); move.getToX() + "," + move.getToY() + ")");
@ -215,10 +236,6 @@ public class Board {
// TODO // TODO
} }
public boolean isHighlighted(int x, int y) {
return false;
}
public void undoLastMove() { public void undoLastMove() {
// TODO // TODO
} }

View File

@ -7,6 +7,7 @@ public class Move {
private int toY; private int toY;
private Piece pieceMoved; private Piece pieceMoved;
private Piece pieceCaptured; 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) { public Move(int fromX, int fromY, int toX, int toY, Piece pieceMoved, Piece pieceCaptured) {
this.fromX = fromX; this.fromX = fromX;
@ -41,6 +42,31 @@ public class Move {
return pieceCaptured; 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 // Static method to calculate all valid moves for a piece
public static String[] calculateValidMoves(Board board, Piece piece) { public static String[] calculateValidMoves(Board board, Piece piece) {
String[] validMoves = new String[32]; // Allocate space for up to 32 moves String[] validMoves = new String[32]; // Allocate space for up to 32 moves