diff --git a/OOP_2B1_Project/src/backend/Board.java b/OOP_2B1_Project/src/backend/Board.java index 683c8ed..41ad376 100644 --- a/OOP_2B1_Project/src/backend/Board.java +++ b/OOP_2B1_Project/src/backend/Board.java @@ -7,6 +7,7 @@ import javax.sound.sampled.Clip; import java.util.ArrayList; import java.util.LinkedList; +import java.util.List; public class Board implements Cloneable { @@ -167,46 +168,53 @@ public class Board implements Cloneable { } public void userTouch(int x, int y) { - if (selectedX == null && selectedY == null) { - Piece pieceAtPos = getPieceAt(x, y); - if (pieceAtPos != null) { - selectedX = x; - selectedY = y; - } - } else { - if (selectedX == x && selectedY == y) { - selectedX = null; - selectedY = null; - } else { - Piece pieceToMove = getPieceAt(selectedX, selectedY); - if (pieceToMove != null) { - Piece pieceAtDestination = getPieceAt(x, y); - if (pieceAtDestination == null || - pieceAtDestination.isWhite() != pieceToMove.isWhite()) { + if (selectedX == null && selectedY == null) { + Piece pieceAtPos = getPieceAt(x, y); + if (pieceAtPos != null) { + selectedX = x; + selectedY = y; - saveStateToHistory(); - if (pieceAtDestination != null) { - pieces.remove(pieceAtDestination); - } - - pieces.remove(pieceToMove); + // Calculate and highlight valid moves for the selected piece + highlightedPositions.clear(); + List legalMoves = pieceAtPos.getLegalMoves(this, x, y); + for (Move move : legalMoves) { + highlightedPositions.add(new int[]{move.getToCol(), move.getToRow()}); + } + } + } else { + if (selectedX == x && selectedY == y) { + // Unselect the piece and clear highlighted positions + selectedX = null; + selectedY = null; + highlightedPositions.clear(); + } else { + Piece pieceToMove = getPieceAt(selectedX, selectedY); + if (pieceToMove != null) { + Piece pieceAtDestination = getPieceAt(x, y); + if (pieceAtDestination == null || pieceAtDestination.isWhite() != pieceToMove.isWhite()) { + saveStateToHistory(); + if (pieceAtDestination != null) { + pieces.remove(pieceAtDestination); + } + pieces.remove(pieceToMove); - // Create a new instance of the specific piece type - Piece newPiece = makeNewPiece(pieceToMove.getType(), - pieceToMove.isWhite, x, y); - pieces.add(newPiece); + // Create a new instance of the specific piece type + Piece newPiece = makeNewPiece(pieceToMove.getType(), pieceToMove.isWhite(), x, y); + pieces.add(newPiece); - playMoveSound(); + playMoveSound(); - turnNumber++; - turnIsWhite = !turnIsWhite; - } - } - selectedX = null; - selectedY = null; - } - } + turnNumber++; + turnIsWhite = !turnIsWhite; + // Clear highlighted positions after moving the piece + highlightedPositions.clear(); + } + } + selectedX = null; + selectedY = null; + } + } } private Piece makeNewPiece(PieceType type, boolean isWhite, int x, int y) { @@ -317,8 +325,12 @@ public class Board implements Cloneable { /* The following methods require more work ! */ public boolean isHighlighted(int x, int y) { - //TODO - return false; + for (int[] position : highlightedPositions) { + if (position[0] == x && position[1] == y) { + return true; + } + } + return false; } public void undoLastMove() { @@ -328,11 +340,16 @@ public class Board implements Cloneable { } public Board(Board board) { - //TODO creer copy du board : utiliser constructeur du board - // dans l' IA / board past = new board (previous) - - - + this.Width = board.Width; + this.Height = board.Height; + this.pieces = new ArrayList<>(); + for (Piece piece : board.pieces) { + this.pieces.add(piece.clone()); + } + this.selectedX = board.selectedX; + this.selectedY = board.selectedY; + this.turnNumber = board.turnNumber; + this.turnIsWhite = board.turnIsWhite; } public void playMove(Move move) {