Compare commits

...

4 Commits

1 changed files with 33 additions and 6 deletions

View File

@ -174,19 +174,29 @@ public class Board {
if(selectedX == x && selectedY == y) { if(selectedX == x && selectedY == y) {
selectedPosition = null; selectedPosition = null;
highlightedPositions.clear(); //Unhighlight highlightedPositions.clear(); //Unhighlight
return;
} }
//To check if square valid
boolean valid = false;
for (int[] pos : highlightedPositions) {
if (pos[0]==x&&pos[1]==y) {
valid = true;
break;
}
// If a piece is selected and the user clicks a new position // If a piece is selected and the user clicks a new position
else { else {
// Check if the move is valid by checking if it exists in highlightedPositions // Check if the move is valid by checking if it exists in highlightedPositions
boolean isValidMove = false; boolean isValidMove = false;
for (int[] pos : highlightedPositions) { for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) { if (pos[0] == x && pos[1] == y) {
isValidMove = true; isValidMove = true;
break; break;
} }
} }
}
// Only move the piece if the destination is valid // Only move the piece if the destination is valid
if (isValidMove) { if (isValidMove) {
movePiece(selectedX, selectedY, x, y); movePiece(selectedX, selectedY, x, y);
@ -198,8 +208,25 @@ public class Board {
// Deselect the position after moving // Deselect the position after moving
this.selectedPosition = null; this.selectedPosition = null;
highlightedPositions.clear();//Clear after move highlightedPositions.clear();//Clear after move
} }
} // If a piece is selected and the user clicks a new position
if (valid) {
movePiece(selectedX, selectedY, x, y);
//update turn
this.turnNumber++;
this.turnWhite=!this.turnWhite;
}
else {
System.out.println("Blocked! This is not a valid move.");
}
// Deselect the position after moving
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
} }