undo
This commit is contained in:
parent
da7f9739fc
commit
cf8a0f4d80
|
|
@ -1,2 +1,2 @@
|
|||
/backend/
|
||||
/windowInterface/
|
||||
/backend/
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -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<Piece> pieces;
|
||||
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
||||
|
||||
// Add this for undo functionality
|
||||
private Stack<Board> 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<Piece> getPieces() {
|
||||
ArrayList<Piece> 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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue