diff --git a/OOP_1A2_Project/bin/.gitignore b/OOP_1A2_Project/bin/.gitignore index 5c7ebe3..ac72981 100644 --- a/OOP_1A2_Project/bin/.gitignore +++ b/OOP_1A2_Project/bin/.gitignore @@ -1,2 +1,2 @@ -/backend/ /windowInterface/ +/backend/ diff --git a/OOP_1A2_Project/bin/backend/Board.class b/OOP_1A2_Project/bin/backend/Board.class index 92e7fab..8d5e10f 100644 Binary files a/OOP_1A2_Project/bin/backend/Board.class and b/OOP_1A2_Project/bin/backend/Board.class differ diff --git a/OOP_1A2_Project/src/backend/Board.java b/OOP_1A2_Project/src/backend/Board.java index 74c2f9d..8462fd9 100644 --- a/OOP_1A2_Project/src/backend/Board.java +++ b/OOP_1A2_Project/src/backend/Board.java @@ -2,6 +2,7 @@ package backend; import java.util.ArrayList; import java.util.List; +import java.util.Stack; public class Board { private int width; @@ -10,11 +11,13 @@ public class Board { private boolean isWhiteTurn; private ArrayList pieces; private List highlightedPositions = new ArrayList<>(); + + // Add this for undo functionality + private Stack boardHistory = new Stack<>(); private Integer selectedX = null; private Integer selectedY = null; - - + public Board(int colNum, int lineNum) { this.width = colNum; this.height = lineNum; @@ -22,37 +25,30 @@ public class Board { this.isWhiteTurn = true; this.pieces = new ArrayList<>(); this.highlightedPositions = new ArrayList<>(); + this.boardHistory = new Stack<>(); } - public int getWidth() { return width; } - public int getHeight() { return height; } - public int getTurnNumber() { return turnNumber; } - - public boolean isTurnWhite() { return isWhiteTurn; } - - public void advanceTurn() { turnNumber++; isWhiteTurn = !isWhiteTurn; } - public void setPiece(boolean isWhite, PieceType type, int x, int y) { for (int i = 0; i < pieces.size(); i++) { Piece p = pieces.get(i); @@ -65,7 +61,6 @@ public class Board { pieces.add(newPiece); } - public void removePiece(int x, int y) { for (int i = 0; i < pieces.size(); i++) { Piece p = pieces.get(i); @@ -76,7 +71,6 @@ public class Board { } } - public void populateBoard() { cleanBoard(); @@ -113,12 +107,10 @@ public class Board { pieces.add(new Piece(6, 7, knight, true)); pieces.add(new Piece(7, 7, rook, true)); } - public void cleanBoard() { pieces.clear(); } - public String toString() { String result = ""; @@ -141,7 +133,6 @@ public class Board { } return result; } - public ArrayList getPieces() { ArrayList result = new ArrayList<>(); @@ -150,7 +141,6 @@ public class Board { } return result; } - public Piece getPieceAt(int x, int y) { for (Piece p : pieces) { @@ -161,7 +151,6 @@ public class Board { return null; } - public void userTouch(int x, int y) { Piece clickedPiece = getPieceAt(x, y); @@ -180,6 +169,9 @@ public class Board { Piece selectedPiece = getPieceAt(selectedX, selectedY); if (selectedPiece != null) { if (isHighlighted(x, y)) { + // SAVE STATE BEFORE MOVE + saveCurrentState(); + Move move = new Move(selectedX, selectedY, x, y, this); if (move.isValid()) { move.execute(); @@ -217,7 +209,6 @@ public class Board { } return false; } - public String[] toFileRep() { String[] fileRep = new String[height + 1]; @@ -239,11 +230,11 @@ public class Board { return fileRep; } - public Board(String[] array) { this.width = 8; this.height = 8; this.pieces = new ArrayList<>(); + this.boardHistory = new Stack<>(); for (int y = 0; y < height; y++) { if (y >= array.length) { @@ -292,7 +283,6 @@ public class Board { this.isWhiteTurn = true; } } - public boolean isHighlighted(int x, int y) { for (Move.Position pos : highlightedPositions) { @@ -303,12 +293,6 @@ public class Board { return false; } - - public void undoLastMove() { - // TODO - } - - public Board(Board board) { this.width = board.width; this.height = board.height; @@ -328,14 +312,15 @@ public class Board { // Copy highlighted positions this.highlightedPositions = new ArrayList<>(); this.highlightedPositions.addAll(board.highlightedPositions); + + // Initialize board history for copy constructor + this.boardHistory = new Stack<>(); } - private void clearHighlights() { highlightedPositions.clear(); } - private void calculateHighlights() { if (selectedX == null || selectedY == null) { return; @@ -354,14 +339,41 @@ public class Board { highlightedPositions.clear(); highlightedPositions.addAll(validMoves); } - public void playMove(Move move) { if (move.isValid() && !move.putsOwnKingInCheck()) { + // SAVE STATE BEFORE MOVE + saveCurrentState(); + move.execute(); - // Play the move sound + // Play the move sound Sound.getInstance().playMoveSound(); } } + + private void saveCurrentState() { + boardHistory.push(new Board(this)); + } + + public void undoLastMove() { + if (!boardHistory.isEmpty()) { + Board previousBoard = boardHistory.pop(); + this.width = previousBoard.width; + this.height = previousBoard.height; + this.turnNumber = previousBoard.turnNumber; + this.isWhiteTurn = previousBoard.isWhiteTurn; + this.pieces.clear(); + for (Piece p : previousBoard.getPieces()) { + this.pieces.add(new Piece(p.getX(), p.getY(), p.getType(), p.isWhite())); + } + selectedX = null; + selectedY = null; + clearHighlights(); + } + } + + public boolean canUndo() { + return !boardHistory.isEmpty(); + } } \ No newline at end of file