possible moves highlighted
This commit is contained in:
parent
68d428058a
commit
19fd5a92c8
|
|
@ -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,29 +132,48 @@ 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
|
||||||
|
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)) {
|
if (!selectedPiece.canMoveTo(x, y, this)) {
|
||||||
System.out.println("Invalid move for " + selectedPiece.getType() + " from (" +
|
System.out.println("Invalid move for " + selectedPiece.getType() + " from (" +
|
||||||
selectedX + "," + selectedY + ") to (" + x + "," + y + ")");
|
selectedX + "," + selectedY + ") to (" + x + "," + y + ")");
|
||||||
selectedX = -1;
|
selectedX = -1;
|
||||||
selectedY = -1;
|
selectedY = -1;
|
||||||
|
Move.clearHighlightedPositions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
System.out.println("Moving piece from (" + selectedX + "," + selectedY + ") to (" + x + "," + y + ")");
|
System.out.println("Moving piece from (" + selectedX + "," + selectedY + ") to (" + x + "," + y + ")");
|
||||||
|
|
@ -166,11 +187,7 @@ public class Board {
|
||||||
playMove(move);
|
playMove(move);
|
||||||
selectedX = -1;
|
selectedX = -1;
|
||||||
selectedY = -1;
|
selectedY = -1;
|
||||||
} else {
|
Move.clearHighlightedPositions();
|
||||||
System.out.println("No piece at (" + selectedX + "," + selectedY + ")");
|
|
||||||
selectedX = -1;
|
|
||||||
selectedY = -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue