Merge branch 'master' of

https://gitarero.ecam.fr/valentine.giral/OOP_3B3_Project.git
This commit is contained in:
Lymeng LY 2025-05-09 09:23:44 +02:00
commit 14f88abd3d
1 changed files with 33 additions and 3 deletions

View File

@ -9,12 +9,15 @@ public class Board {
private int [] selectedPosition = null; // No piece selected initially
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 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) {
// Verify the bounds of the board
return x >= 0 && y >= 0 && x < col && y < line;
}
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;
}
@ -84,7 +87,7 @@ public class Board {
PieceType.Knight, PieceType.Rook
};
// place black pieces (false = black)
// Place black pieces (false = black)
for (int x=0;x<8;x++) {
// Row 0: black major pieces
setPiece(false,backRow[x],x,0);
@ -102,7 +105,7 @@ public class Board {
}
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
for (int y = 0; y < line; y++) {
for (int x = 0; x < col; x++) {
@ -173,6 +176,7 @@ public class Board {
highlightedPositions.clear(); //Unhighlight
return;
}
//To check if square valid
boolean valid = false;
for (int[] pos : highlightedPositions) {
@ -180,6 +184,31 @@ public class Board {
valid = true;
break;
}
// If a piece is selected and the user clicks a new position
else {
// Check if the move is valid by checking if it exists in highlightedPositions
boolean isValidMove = false;
for (int[] pos : highlightedPositions) {
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
this.selectedPosition = null;
highlightedPositions.clear();//Clear after move
}
// If a piece is selected and the user clicks a new position
if (valid) {
@ -292,6 +321,7 @@ public class Board {
/* The following methods require more work ! */
public boolean isHighlighted(int x, int y) {
// Storing valid moves when piece is selected
for (int[] pos : highlightedPositions) {
if (pos[0] == x && pos[1] == y)
return true;