From ceaad980f661871936da608fde9fe9ba5e4255bd Mon Sep 17 00:00:00 2001 From: Yash Shah Date: Wed, 7 May 2025 16:21:07 +0200 Subject: [PATCH] final good one --- src/backend/Board.java | 65 ++---------------------------------------- 1 file changed, 2 insertions(+), 63 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 79e3975..8bddd66 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -79,10 +79,6 @@ public class Board { public void cleanBoard() { pieces.clear(); - turnNumber = 0; - turnWhite = true; - selected = null; - highlighted.clear(); //TODO } @@ -122,62 +118,6 @@ 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 - - Piece clickedPiece = getPieceAt(x, y); if (selected == null) { @@ -205,7 +145,6 @@ public class Board { } } } - } @@ -351,7 +290,8 @@ public class Board { return false; } - public void undoLastMove() { +public void undoLastMove() { + if (moveHistory.isEmpty()) return; Move lastMove = moveHistory.remove(moveHistory.size() - 1); @@ -374,7 +314,6 @@ public class Board { highlighted.clear(); } - public Board(Board board) { if (moveHistory.isEmpty()) return;