added undoLastMove
This commit is contained in:
parent
ae521aa569
commit
73f8ac5d35
|
|
@ -1,6 +1,7 @@
|
|||
package backend;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Board {
|
||||
|
||||
|
|
@ -22,6 +23,10 @@ private Integer selectedY = null;
|
|||
|
||||
private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
||||
|
||||
private LinkedList<ArrayList<Piece>> boardHistory = new LinkedList<>();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public Board(int colNum, int lineNum) {
|
||||
|
|
@ -162,12 +167,11 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
|||
if (pieceAtDestination == null ||
|
||||
pieceAtDestination.isWhite() != pieceToMove.isWhite()) {
|
||||
|
||||
|
||||
saveStateToHistory();
|
||||
if (pieceAtDestination != null) {
|
||||
pieces.remove(pieceAtDestination);
|
||||
}
|
||||
|
||||
|
||||
pieces.remove(pieceToMove);
|
||||
pieces.add(new Piece(pieceToMove.isWhite(), pieceToMove.getType(), x, y));
|
||||
|
||||
|
|
@ -187,7 +191,20 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
|||
|
||||
}
|
||||
|
||||
|
||||
private void saveStateToHistory() {
|
||||
ArrayList<Piece> clonedList = new ArrayList<>();
|
||||
for(int i =0; i<pieces.size();i++) {
|
||||
Piece pieceToCopy = pieces.get(i);
|
||||
Piece copy = new Piece(
|
||||
pieceToCopy.isWhite(),
|
||||
pieceToCopy.getType(),
|
||||
pieceToCopy.getX(),
|
||||
pieceToCopy.getY());
|
||||
|
||||
clonedList.add(copy);
|
||||
}
|
||||
boardHistory.add(clonedList);
|
||||
}
|
||||
|
||||
|
||||
private Piece getPieceAt(int x, int y) {
|
||||
|
|
@ -260,8 +277,9 @@ private ArrayList<int[]> highlightedPositions = new ArrayList<>();
|
|||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
|
||||
|
||||
pieces = boardHistory.getLast();
|
||||
boardHistory.removeLast();
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
WR,WN,WB,WQ,WK,WB,WN,WR
|
||||
,WN,WB,WQ,WK,WB,,WR
|
||||
WP,WP,WP,WP,WP,WP,WP,WP
|
||||
,,,,,,,
|
||||
,,BK,,,,,
|
||||
,,,,,,,
|
||||
,,,,,,,
|
||||
BP,BP,BP,BP,,BP,BP,BP
|
||||
BR,BN,BB,,BQ,BB,BN,BR
|
||||
,,WR,BP,,WN,,
|
||||
,,BN,,,,,
|
||||
BP,BP,BP,,BP,BP,BP,BP
|
||||
BR,,BB,BK,BQ,BB,BN,BR
|
||||
W
|
||||
|
|
|
|||
Loading…
Reference in New Issue