undoLastMove implemented
This commit is contained in:
parent
5a8de97bc2
commit
b86d77b61a
|
|
@ -1,6 +1,7 @@
|
||||||
package backend;
|
package backend;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
public int width;
|
public int width;
|
||||||
|
|
@ -12,6 +13,7 @@ public class Board {
|
||||||
public int turnNumber;
|
public int turnNumber;
|
||||||
public boolean turnColor;
|
public boolean turnColor;
|
||||||
public ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
|
public ArrayList<ArrayList<Boolean>> possibleMoves = new ArrayList<>();
|
||||||
|
public LinkedList<BoardHistory> boardHistory = new LinkedList<>();
|
||||||
public Board(int colNum, int lineNum) {
|
public Board(int colNum, int lineNum) {
|
||||||
this.width = colNum;
|
this.width = colNum;
|
||||||
this.height = lineNum;
|
this.height = lineNum;
|
||||||
|
|
@ -29,7 +31,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
this.turnNumber = 0;
|
this.turnNumber = 0;
|
||||||
this.turnColor = true;
|
this.turnColor = true;
|
||||||
|
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
|
|
@ -81,6 +83,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanBoard() {
|
public void cleanBoard() {
|
||||||
|
|
@ -91,6 +94,8 @@ public class Board {
|
||||||
this.board.get(y).set(x, null);
|
this.board.get(y).set(x, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.turnNumber = 0;
|
||||||
|
this.turnColor = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -109,16 +114,28 @@ public class Board {
|
||||||
}
|
}
|
||||||
return pieces;
|
return pieces;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Piece getPiece(int x, int y) {
|
public Piece getPiece(int x, int y) {
|
||||||
return board.get(y).get(x);
|
return board.get(y).get(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBoard(ArrayList<ArrayList<Piece>> boardToSet) {
|
||||||
|
for (int x = 0; x < 8; x++) {
|
||||||
|
for (int y = 0; y < 8; y++) {
|
||||||
|
if (boardToSet.get(y).get(x) != null) {
|
||||||
|
setPiece(x, y, boardToSet.get(y).get(x).getType(), boardToSet.get(y).get(x).isWhite());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void movePiece(int x, int y) {
|
public void movePiece(int x, int y) {
|
||||||
if (select) {
|
if (select) {
|
||||||
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
|
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
|
||||||
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
|
this.setPiece(x,y,pieceToMove.getType(),pieceToMove.isWhite());
|
||||||
board.get(this.ym).set(this.xm,null);
|
board.get(this.ym).set(this.xm,null);
|
||||||
|
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,8 +191,11 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void undoLastMove() {
|
public void undoLastMove() {
|
||||||
//TODO
|
ArrayList<ArrayList<Piece>> boardToSet = boardHistory.get(boardHistory.size() - 2).getBoard();
|
||||||
|
this.turnNumber = boardHistory.getLast().getTurnNumber();
|
||||||
|
this.turnColor = boardHistory.getLast().isTurnColor();
|
||||||
|
boardHistory.removeLast();
|
||||||
|
board = boardToSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Board(Board board) {
|
public Board(Board board) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class BoardHistory {
|
||||||
|
public int width;
|
||||||
|
public int height;
|
||||||
|
public ArrayList<ArrayList<Piece>> board = new ArrayList<>();
|
||||||
|
public int turnNumber;
|
||||||
|
public boolean turnColor;
|
||||||
|
public BoardHistory(ArrayList<ArrayList<Piece>> board, int turnNumber, boolean turnColor) {
|
||||||
|
this.board = deepCopyBoard(board);
|
||||||
|
this.turnNumber = turnNumber;
|
||||||
|
this.turnColor = turnColor;
|
||||||
|
}
|
||||||
|
private ArrayList<ArrayList<Piece>> deepCopyBoard(ArrayList<ArrayList<Piece>> original) {
|
||||||
|
ArrayList<ArrayList<Piece>> copy = new ArrayList<>();
|
||||||
|
for (ArrayList<Piece> row : original) {
|
||||||
|
ArrayList<Piece> newRow = new ArrayList<>();
|
||||||
|
for (Piece p : row) {
|
||||||
|
if (p != null) {
|
||||||
|
newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite()));
|
||||||
|
} else {
|
||||||
|
newRow.add(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copy.add(newRow);
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
public ArrayList<ArrayList<Piece>> getBoard() {
|
||||||
|
return board;
|
||||||
|
}
|
||||||
|
public int getTurnNumber() {
|
||||||
|
return turnNumber;
|
||||||
|
}
|
||||||
|
public boolean isTurnColor() {
|
||||||
|
return turnColor;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BoardHistory [board=" + board + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package backend;
|
||||||
|
|
||||||
|
public class History {
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue