diff --git a/src/backend/Board.java b/src/backend/Board.java index a8f9dc7..a330998 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -1,6 +1,7 @@ package backend; import java.util.ArrayList; +import java.util.List; public class Board { private int width; @@ -11,12 +12,49 @@ public class Board { private boolean isTurnWhite=true; private ArrayList highlightedSquares= new ArrayList<>(); //stores highlighted board positions private Move lastMove; // Tracks the last move; needed for en-passant rule + private List previousStates = new ArrayList<>(); // i added this + + public Board(int colNum, int lineNum) { this.width=colNum; this.height=lineNum; this.board= new Piece[width][height]; } + + /** + * Copy-constructor: take a complete snapshot of another Board. + */ + public Board(Board other) { + // 1) copy dimensions + this.width = other.width; + this.height = other.height; + + // 2) deep-copy the board array + this.board = new Piece[width][height]; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + this.board[x][y] = other.board[x][y]; + } + } + + // 3) copy any “selected” piece + this.chosenPiece = other.chosenPiece; + + // 4) turn state + this.turnNumber = other.turnNumber; + this.isTurnWhite = other.isTurnWhite; + + // 5) highlighted squares + this.highlightedSquares = new ArrayList<>(other.highlightedSquares); + + // 6) lastMove (for en-passant) + this.lastMove = other.lastMove; + + // 7) start with an empty history + this.previousStates = new ArrayList<>(); + } + public int getWidth() { return width; @@ -149,6 +187,7 @@ public class Board { } public void movePiece(int oldX, int oldY, int newX, int newY) { + previousStates.add(new Board(this)); if (board[oldX][oldY] != null) { //checks if there is a piece at that position Piece piece = board[oldX][oldY]; // we get the piece from the board board[newX][newY] = piece; // we place the piece on the new position @@ -205,19 +244,80 @@ return false; } + + /** + * Reverts the board state back one move. + */ public void undoLastMove() { - //TODO + if (previousStates.isEmpty()) { + System.out.println("There are no moves to undo."); + return; + } + + // 1) Pop off the most recent snapshot + Board prev = previousStates.remove(previousStates.size() - 1); + + // 2) Restore the board array + this.board = new Piece[width][height]; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + this.board[x][y] = prev.board[x][y]; + } + } + + // 3) Restore all other game‐state fields + this.chosenPiece = prev.chosenPiece; + this.turnNumber = prev.turnNumber; + this.isTurnWhite = prev.isTurnWhite; + this.lastMove = prev.lastMove; + this.highlightedSquares = new ArrayList<>(prev.highlightedSquares); + + // 4) **Critical**: reset each Piece’s own coordinates to match its array slot + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + Piece p = this.board[x][y]; + if (p != null) { + p.setX(x); + p.setY(y); + } + } + } + + // 5) Clear any lingering selection or highlights + this.chosenPiece = null; + this.highlightedSquares.clear(); + } + + + + + + + private void updateTurnLabel() { + // TODO Auto-generated method stub } - public Board(Board board) { + private void repaint() { + // TODO Auto-generated method stub + + } + + /*public Board(Board board) { //TODO - } + }*/ public void playMove(Move move) { //TODO } + /** Expose the raw 2D array for testing or drawing. */ + public Piece[][] getBoardArray() { + return board; + } + + + } \ No newline at end of file diff --git a/src/backend/Game.java b/src/backend/Game.java index 4c64f70..ce5325d 100644 --- a/src/backend/Game.java +++ b/src/backend/Game.java @@ -106,5 +106,14 @@ public class Game extends Thread { public void toggleAI(boolean isWhite) { this.activationAIFlags[isWhite?1:0] = !this.activationAIFlags[isWhite?1:0]; } + /** In Game.java **/ + public boolean isTurnWhite() { + return board.isTurnWhite(); + } + /** So the UI can reach into the model’s Board for undo/turn info. */ + public Board getBoard() { + return board; // assuming your Game keeps its Board in a field named “board” + } + } diff --git a/src/windowInterface/MyInterface.java b/src/windowInterface/MyInterface.java index b7e2549..f0dce3d 100644 --- a/src/windowInterface/MyInterface.java +++ b/src/windowInterface/MyInterface.java @@ -1,5 +1,8 @@ package windowInterface; import java.awt.BorderLayout; + + + import java.awt.Dimension; import java.awt.GridLayout; @@ -174,10 +177,16 @@ public class MyInterface extends JFrame { if(game == null) { System.out.println("error : can't undo while no game present"); } else { - game.undoLastMove(); - } + game.getBoard().undoLastMove(); + panelDraw.repaint(); + turnLabel.setText("Turn : " + (game.getBoard().isTurnWhite() ? "White" : "Black")); - } + + } + } + + + public void clicAIToggle(boolean isWhite) { if(game == null) { System.out.println("error : can't activate AI while no game present");