undo
This commit is contained in:
parent
da7f9739fc
commit
cf8a0f4d80
|
|
@ -1,2 +1,2 @@
|
||||||
/backend/
|
|
||||||
/windowInterface/
|
/windowInterface/
|
||||||
|
/backend/
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -2,6 +2,7 @@ package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
private int width;
|
private int width;
|
||||||
|
|
@ -10,11 +11,13 @@ public class Board {
|
||||||
private boolean isWhiteTurn;
|
private boolean isWhiteTurn;
|
||||||
private ArrayList<Piece> pieces;
|
private ArrayList<Piece> pieces;
|
||||||
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
private List<Move.Position> highlightedPositions = new ArrayList<>();
|
||||||
|
|
||||||
|
// Add this for undo functionality
|
||||||
|
private Stack<Board> boardHistory = new Stack<>();
|
||||||
|
|
||||||
private Integer selectedX = null;
|
private Integer selectedX = null;
|
||||||
private Integer selectedY = null;
|
private Integer selectedY = null;
|
||||||
|
|
||||||
|
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum;
|
this.width = colNum;
|
||||||
this.height = lineNum;
|
this.height = lineNum;
|
||||||
|
|
@ -22,37 +25,30 @@ public class Board {
|
||||||
this.isWhiteTurn = true;
|
this.isWhiteTurn = true;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
this.highlightedPositions = new ArrayList<>();
|
this.highlightedPositions = new ArrayList<>();
|
||||||
|
this.boardHistory = new Stack<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int getTurnNumber() {
|
public int getTurnNumber() {
|
||||||
return turnNumber;
|
return turnNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public boolean isTurnWhite() {
|
public boolean isTurnWhite() {
|
||||||
return isWhiteTurn;
|
return isWhiteTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void advanceTurn() {
|
public void advanceTurn() {
|
||||||
turnNumber++;
|
turnNumber++;
|
||||||
isWhiteTurn = !isWhiteTurn;
|
isWhiteTurn = !isWhiteTurn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||||
for (int i = 0; i < pieces.size(); i++) {
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
Piece p = pieces.get(i);
|
Piece p = pieces.get(i);
|
||||||
|
|
@ -65,7 +61,6 @@ public class Board {
|
||||||
pieces.add(newPiece);
|
pieces.add(newPiece);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void removePiece(int x, int y) {
|
public void removePiece(int x, int y) {
|
||||||
for (int i = 0; i < pieces.size(); i++) {
|
for (int i = 0; i < pieces.size(); i++) {
|
||||||
Piece p = pieces.get(i);
|
Piece p = pieces.get(i);
|
||||||
|
|
@ -76,7 +71,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void populateBoard() {
|
public void populateBoard() {
|
||||||
cleanBoard();
|
cleanBoard();
|
||||||
|
|
||||||
|
|
@ -113,12 +107,10 @@ public class Board {
|
||||||
pieces.add(new Piece(6, 7, knight, true));
|
pieces.add(new Piece(6, 7, knight, true));
|
||||||
pieces.add(new Piece(7, 7, rook, true));
|
pieces.add(new Piece(7, 7, rook, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void cleanBoard() {
|
public void cleanBoard() {
|
||||||
pieces.clear();
|
pieces.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String result = "";
|
String result = "";
|
||||||
|
|
@ -141,7 +133,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ArrayList<Piece> getPieces() {
|
public ArrayList<Piece> getPieces() {
|
||||||
ArrayList<Piece> result = new ArrayList<>();
|
ArrayList<Piece> result = new ArrayList<>();
|
||||||
|
|
@ -150,7 +141,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Piece getPieceAt(int x, int y) {
|
public Piece getPieceAt(int x, int y) {
|
||||||
for (Piece p : pieces) {
|
for (Piece p : pieces) {
|
||||||
|
|
@ -161,7 +151,6 @@ public class Board {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void userTouch(int x, int y) {
|
public void userTouch(int x, int y) {
|
||||||
Piece clickedPiece = getPieceAt(x, y);
|
Piece clickedPiece = getPieceAt(x, y);
|
||||||
|
|
||||||
|
|
@ -180,6 +169,9 @@ public class Board {
|
||||||
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
Piece selectedPiece = getPieceAt(selectedX, selectedY);
|
||||||
if (selectedPiece != null) {
|
if (selectedPiece != null) {
|
||||||
if (isHighlighted(x, y)) {
|
if (isHighlighted(x, y)) {
|
||||||
|
// SAVE STATE BEFORE MOVE
|
||||||
|
saveCurrentState();
|
||||||
|
|
||||||
Move move = new Move(selectedX, selectedY, x, y, this);
|
Move move = new Move(selectedX, selectedY, x, y, this);
|
||||||
if (move.isValid()) {
|
if (move.isValid()) {
|
||||||
move.execute();
|
move.execute();
|
||||||
|
|
@ -217,7 +209,6 @@ public class Board {
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String[] toFileRep() {
|
public String[] toFileRep() {
|
||||||
String[] fileRep = new String[height + 1];
|
String[] fileRep = new String[height + 1];
|
||||||
|
|
@ -239,11 +230,11 @@ public class Board {
|
||||||
return fileRep;
|
return fileRep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Board(String[] array) {
|
public Board(String[] array) {
|
||||||
this.width = 8;
|
this.width = 8;
|
||||||
this.height = 8;
|
this.height = 8;
|
||||||
this.pieces = new ArrayList<>();
|
this.pieces = new ArrayList<>();
|
||||||
|
this.boardHistory = new Stack<>();
|
||||||
|
|
||||||
for (int y = 0; y < height; y++) {
|
for (int y = 0; y < height; y++) {
|
||||||
if (y >= array.length) {
|
if (y >= array.length) {
|
||||||
|
|
@ -292,7 +283,6 @@ public class Board {
|
||||||
this.isWhiteTurn = true;
|
this.isWhiteTurn = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public boolean isHighlighted(int x, int y) {
|
public boolean isHighlighted(int x, int y) {
|
||||||
for (Move.Position pos : highlightedPositions) {
|
for (Move.Position pos : highlightedPositions) {
|
||||||
|
|
@ -303,12 +293,6 @@ public class Board {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void undoLastMove() {
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Board(Board board) {
|
public Board(Board board) {
|
||||||
this.width = board.width;
|
this.width = board.width;
|
||||||
this.height = board.height;
|
this.height = board.height;
|
||||||
|
|
@ -328,14 +312,15 @@ public class Board {
|
||||||
// Copy highlighted positions
|
// Copy highlighted positions
|
||||||
this.highlightedPositions = new ArrayList<>();
|
this.highlightedPositions = new ArrayList<>();
|
||||||
this.highlightedPositions.addAll(board.highlightedPositions);
|
this.highlightedPositions.addAll(board.highlightedPositions);
|
||||||
|
|
||||||
|
// Initialize board history for copy constructor
|
||||||
|
this.boardHistory = new Stack<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void clearHighlights() {
|
private void clearHighlights() {
|
||||||
highlightedPositions.clear();
|
highlightedPositions.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void calculateHighlights() {
|
private void calculateHighlights() {
|
||||||
if (selectedX == null || selectedY == null) {
|
if (selectedX == null || selectedY == null) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -354,14 +339,41 @@ public class Board {
|
||||||
highlightedPositions.clear();
|
highlightedPositions.clear();
|
||||||
highlightedPositions.addAll(validMoves);
|
highlightedPositions.addAll(validMoves);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void playMove(Move move) {
|
public void playMove(Move move) {
|
||||||
if (move.isValid() && !move.putsOwnKingInCheck()) {
|
if (move.isValid() && !move.putsOwnKingInCheck()) {
|
||||||
|
// SAVE STATE BEFORE MOVE
|
||||||
|
saveCurrentState();
|
||||||
|
|
||||||
move.execute();
|
move.execute();
|
||||||
|
|
||||||
// Play the move sound
|
// Play the move sound
|
||||||
Sound.getInstance().playMoveSound();
|
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