final good one

This commit is contained in:
Yash Shah 2025-05-07 16:21:07 +02:00
parent a6dab9e366
commit ceaad980f6
1 changed files with 2 additions and 63 deletions

View File

@ -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;