From da9d8b1a8b938564b54e2eff192eeb5d5914ea1e Mon Sep 17 00:00:00 2001 From: VALENTINE GIRAL Date: Fri, 9 May 2025 09:57:33 +0200 Subject: [PATCH 1/2] pawn 2 squares forward starting move --- src/backend/Board.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 5ef55a7..c10ea22 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -212,12 +212,23 @@ public class Board { switch (p.getType()) { case Pawn: int dir = isWhite ? -1 : 1; - if (inBounds(x, y + dir) && board[y + dir][x] == null) + // Forward one square + if (inBounds(x, y + dir) && board[y + dir][x] == null) { moves.add(new int[]{x, y + dir}); - if (inBounds(x - 1, y + dir) && hasEnemy(x - 1, y + dir, isWhite)) + + // Initial two-square move + int startRow = isWhite ? 6 : 1; // Starting row for pawns + if (y == startRow && inBounds(x, y + 2*dir) && board[y + 2*dir][x] == null) { + moves.add(new int[]{x, y + 2*dir}); + } + } + // Captures (diagonal) + if (inBounds(x - 1, y + dir) && hasEnemy(x - 1, y + dir, isWhite)) { moves.add(new int[]{x - 1, y + dir}); - if (inBounds(x + 1, y + dir) && hasEnemy(x + 1, y + dir, isWhite)) + } + if (inBounds(x + 1, y + dir) && hasEnemy(x + 1, y + dir, isWhite)) { moves.add(new int[]{x + 1, y + dir}); + } break; case Rook: @@ -230,8 +241,7 @@ public class Board { case Queen: moves.addAll(lineMoves(x, y, isWhite, new int[][]{ - {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1} - })); + {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}, {-1, -1} })); break; case Knight: @@ -245,6 +255,7 @@ public class Board { break; case King: + // Regular king moves for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue; From 5af0f8487d2c99dc3375a5a5e617b229f0648fc1 Mon Sep 17 00:00:00 2001 From: VALENTINE GIRAL Date: Fri, 9 May 2025 10:38:59 +0200 Subject: [PATCH 2/2] undo method + new class recording moves --- src/backend/Board.java | 59 +++++++++++++++++++++++++++++-------- src/backend/MoveRecord.java | 22 ++++++++++++++ 2 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/backend/MoveRecord.java diff --git a/src/backend/Board.java b/src/backend/Board.java index c10ea22..1da9ee8 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -9,7 +9,8 @@ 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 highlightedPositions = new ArrayList<>();// List containing all board positions + private ArrayList highlightedPositions = new ArrayList<>(); // List containing all board positions + private ArrayList moveHistory = new ArrayList<>(); // Stack to store move history private boolean inBounds(int x, int y) { // Verify the bounds of the board @@ -272,26 +273,31 @@ public class Board { public void movePiece(int fromX, int fromY, int toX, int toY) { // Additional method: moves a piece from one position to another on the board + Piece movingPiece = board[fromY][fromX]; + Piece capturedPiece = board[toY][toX]; - // Get the piece at the source position - Piece piece = board[fromY][fromX]; + // Record the move before making it + moveHistory.add(new MoveRecord( + movingPiece, capturedPiece, + fromX, fromY, toX, toY, + turnNumber, turnWhite)); - if (piece != null) { - // Update the piece's internal position - piece.setX(toX); - piece.setY(toY); - // Move the piece on the board - board[toY][toX] = piece; // Place piece at the new position + board[toY][toX] = movingPiece; // Place piece at the new position board[fromY][fromX] = null; // Clear the old position - } - + + if (movingPiece != null) { + // Update the piece's internal position + movingPiece.setX(toX); + movingPiece.setY(toY); + } } public boolean isSelected(int x, int y) { return this.selectedPosition!=null && this.selectedPosition[0] == x && this.selectedPosition[1] == y; } + /* saving-loading feature :*/ public String[] toFileRep() { ArrayList fileLines = new ArrayList<>(); @@ -362,8 +368,35 @@ public class Board { } public void undoLastMove() { - //TODO - + // Check if there are moves to undo + if (moveHistory.isEmpty()) { + return; // Nothing to undo + } + + // Get the last move + MoveRecord lastMove = moveHistory.remove(moveHistory.size() - 1); + + // Restore the moved piece to its original position + board[lastMove.fromY][lastMove.fromX] = lastMove.movedPiece; + if (lastMove.movedPiece != null) { + lastMove.movedPiece.setX(lastMove.fromX); + lastMove.movedPiece.setY(lastMove.fromY); + } + + // Restore the captured piece if any + board[lastMove.toY][lastMove.toX] = lastMove.capturedPiece; + if (lastMove.capturedPiece != null) { + lastMove.capturedPiece.setX(lastMove.toX); + lastMove.capturedPiece.setY(lastMove.toY); + } + + // Restore game state + turnNumber = lastMove.turnNumberAtMove; + turnWhite = lastMove.turnWhiteAtMove; + + // Clear any selections or highlights + selectedPosition = null; + highlightedPositions.clear(); } public Board(Board board) { diff --git a/src/backend/MoveRecord.java b/src/backend/MoveRecord.java new file mode 100644 index 0000000..f3d706a --- /dev/null +++ b/src/backend/MoveRecord.java @@ -0,0 +1,22 @@ +package backend; + +public class MoveRecord { + Piece movedPiece; + Piece capturedPiece; + int fromX, fromY, toX, toY; + int turnNumberAtMove; + boolean turnWhiteAtMove; + + public MoveRecord(Piece moved, Piece captured, int fx, int fy, int tx, int ty, + int turnNum, boolean whiteTurn) { + this.movedPiece = moved; + this.capturedPiece = captured; + this.fromX = fx; + this.fromY = fy; + this.toX = tx; + this.toY = ty; + this.turnNumberAtMove = turnNum; + this.turnWhiteAtMove = whiteTurn; + } + +}