diff --git a/src/backend/Board.java b/src/backend/Board.java index a44596d..0a1be0b 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -171,19 +171,31 @@ public class Board { if(selectedX == x && selectedY == y) { selectedPosition = null; highlightedPositions.clear(); //Unhighlight + return; } - - // If a piece is selected and the user clicks a new position - else { - movePiece(selectedX,selectedY,x,y); - // Update turn - this.turnNumber++; - this.turnWhite=!this.turnWhite; - - // Deselect the position after moving - this.selectedPosition = null; - highlightedPositions.clear();//Clear after move + //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 (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 + } }