diff --git a/src/backend/Board.java b/src/backend/Board.java index 4d89e07..6058d8b 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -79,6 +79,10 @@ public class Board { public void cleanBoard() { pieces.clear(); + turnNumber = 0; + turnWhite = true; + selected = null; + highlighted.clear(); //TODO } @@ -121,6 +125,57 @@ public class Board { } public void userTouch(int x, int y) { + // If no position has been previously selected + if (selected == null) { + // Check if there is a piece at the selected coordinates + for (Piece piece : pieces) { + if (piece.getX() == x && piece.getY() == y) { + // If a piece is found, select the position + selected = new int[]{x, y}; + return; + } + } + } else { + // If a position is already selected + if (selected[0] == x && selected[1] == y) { + // If the selected position is the same as the current position, unselect it + selected = null; + } else { + // Otherwise, move the piece from the selected position to the new position + Piece selectedPiece = null; + Piece capturedPiece = null; + for (Piece piece : pieces) { + if (piece.getX() == selected[0] && piece.getY() == selected[1]) { + selectedPiece = piece; + break; + } + } + for (Piece piece : pieces) { + if (piece.getX() == x && piece.getY() == y && piece != selectedPiece) { + capturedPiece = piece; // Capture the piece + pieces.remove(piece); // Remove the captured piece from the board + break; + } + } + + + if (selectedPiece != null) { + // Move the piece to the new position + selectedPiece.setX(x); + selectedPiece.setY(y); + + // Increment the turn number and switch turns + turnNumber++; + turnWhite = !turnWhite; + + // Optionally, add the move to moveHistory if you'd like to track moves + moveHistory.add(new Move(selectedPiece, selected[0], selected[1], x, y, capturedPiece)); + } + + // After the move, unselect the piece + selected = null; + } + } //TODO diff --git a/src/backend/Piece.java b/src/backend/Piece.java index a955e7d..95b1105 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -14,10 +14,16 @@ public class Piece { public int getX() { return x; } + public void setX(int x) { + this.x = x; + } public int getY() { return y; } + public void setY(int y) { + this.y = y; + } public PieceType getType() { return type; @@ -31,4 +37,5 @@ public class Piece { this.x=x; this.y=y; } + }