new comments + valid piece moves only

This commit is contained in:
Valentine GIRAL 2025-05-09 08:56:26 +02:00
parent 2dfd232a84
commit 1796c22554
1 changed files with 24 additions and 8 deletions

View File

@ -9,12 +9,15 @@ public class Board {
private int [] selectedPosition = null; // No piece selected initially private int [] selectedPosition = null; // No piece selected initially
private int turnNumber=0; // Tracks the number of turns elapsed private int turnNumber=0; // Tracks the number of turns elapsed
private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn private boolean turnWhite=true; // True if it's White's turn, False if it's Black's turn
private ArrayList<int[]> highlightedPositions = new ArrayList<>();//this list contain all the board positions private ArrayList<int[]> highlightedPositions = new ArrayList<>();// List containing all board positions
private boolean inBounds(int x, int y) { private boolean inBounds(int x, int y) {
// Verify the bounds of the board
return x >= 0 && y >= 0 && x < col && y < line; return x >= 0 && y >= 0 && x < col && y < line;
} }
private boolean hasEnemy(int x, int y, boolean isWhite) { private boolean hasEnemy(int x, int y, boolean isWhite) {
// Detect an enemy piece, meaning from a different color
return board[y][x] != null && board[y][x].isWhite() != isWhite; return board[y][x] != null && board[y][x].isWhite() != isWhite;
} }
@ -84,7 +87,7 @@ public class Board {
PieceType.Knight, PieceType.Rook PieceType.Knight, PieceType.Rook
}; };
// place black pieces (false = black) // Place black pieces (false = black)
for (int x=0;x<8;x++) { for (int x=0;x<8;x++) {
// Row 0: black major pieces // Row 0: black major pieces
setPiece(false,backRow[x],x,0); setPiece(false,backRow[x],x,0);
@ -102,7 +105,7 @@ public class Board {
} }
public void cleanBoard() { public void cleanBoard() {
// removes all pieces from the board, readying it to add pieces for a new game // Removes all pieces from the board, readying it to add pieces for a new game
// Iterate over each row and column and set the board position to null // Iterate over each row and column and set the board position to null
for (int y = 0; y < line; y++) { for (int y = 0; y < line; y++) {
for (int x = 0; x < col; x++) { for (int x = 0; x < col; x++) {
@ -175,10 +178,22 @@ public class Board {
// 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 {
movePiece(selectedX,selectedY,x,y); // Check if the move is valid by checking if it exists in highlightedPositions
// Update turn boolean isValidMove = false;
this.turnNumber++; for (int[] pos : highlightedPositions) {
this.turnWhite=!this.turnWhite; if (pos[0] == x && pos[1] == y) {
isValidMove = true;
break;
}
}
// Only move the piece if the destination is valid
if (isValidMove) {
movePiece(selectedX, selectedY, x, y);
// Update turn
this.turnNumber++;
this.turnWhite = !this.turnWhite;
}
// Deselect the position after moving // Deselect the position after moving
this.selectedPosition = null; this.selectedPosition = null;
@ -280,6 +295,7 @@ public class Board {
/* The following methods require more work ! */ /* The following methods require more work ! */
public boolean isHighlighted(int x, int y) { public boolean isHighlighted(int x, int y) {
// Storing valid moves when piece is selected
for (int[] pos : highlightedPositions) { for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y) if (pos[0] == x && pos[1] == y)
return true; return true;