undo last move
This commit is contained in:
parent
e7a64f69a7
commit
5a54286394
|
|
@ -12,7 +12,7 @@ public class Main {
|
|||
// testing :
|
||||
Board testBoard = new Board(8, 8);
|
||||
testBoard.populateBoard();
|
||||
System.out.println(testBoard.toString());
|
||||
//System.out.println(testBoard.toString());
|
||||
// launches graphical interface :
|
||||
MyInterface mjf = new MyInterface();
|
||||
mjf.setVisible(true);
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public class Board {
|
|||
private boolean blackKing;
|
||||
private boolean castlingRight;
|
||||
private boolean castlingLeft;
|
||||
ArrayList<String> states;
|
||||
|
||||
// CONSTRUCTOR
|
||||
public Board(int colNum, int lineNum) {
|
||||
|
|
@ -48,6 +49,7 @@ public class Board {
|
|||
blackKing = false;
|
||||
castlingRight = false;
|
||||
castlingLeft = false;
|
||||
states = new ArrayList<>();
|
||||
}
|
||||
|
||||
// GETTERS
|
||||
|
|
@ -85,6 +87,7 @@ public class Board {
|
|||
blackRookRight = false;
|
||||
whiteKing = false;
|
||||
blackKing = false;
|
||||
states = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void setPiece(boolean isWhite, PieceType type, int x, int y) {
|
||||
|
|
@ -110,6 +113,7 @@ public class Board {
|
|||
setPiece(white, PieceType.King, 4, i);
|
||||
}
|
||||
}
|
||||
states.add(toString());
|
||||
}
|
||||
|
||||
public void cleanBoard() {
|
||||
|
|
@ -144,6 +148,11 @@ public class Board {
|
|||
}
|
||||
str += "\n";
|
||||
}
|
||||
String turn = "B";
|
||||
if (isTurnWhite()) {
|
||||
turn = "W";
|
||||
}
|
||||
str += turn;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
|
@ -290,11 +299,7 @@ public class Board {
|
|||
/* saving-loading feature :*/
|
||||
|
||||
public String[] toFileRep() {
|
||||
String turn = "B";
|
||||
if (isTurnWhite()) {
|
||||
turn = "W";
|
||||
}
|
||||
String myFile = toString() + turn;
|
||||
String myFile = toString();
|
||||
String[] myArray = myFile.split("\n");
|
||||
return myArray;
|
||||
}
|
||||
|
|
@ -303,24 +308,32 @@ public class Board {
|
|||
this.colNum = 8;
|
||||
this.lineNum = 8;
|
||||
this.board = new Piece[lineNum][colNum];
|
||||
this.x = -1;
|
||||
this.y = -1;
|
||||
//this.turns = turns;
|
||||
|
||||
String[] line;
|
||||
boolean white;
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
line = array[i].split(",");
|
||||
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
if (line[j] != " ") {
|
||||
line = array[j].split(",");
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
if (line[i] != " ") {
|
||||
white = false;
|
||||
if (line[j].charAt(0) == 'W') {
|
||||
if (line[i].charAt(0) == 'W') {
|
||||
white = true;
|
||||
}
|
||||
setPiece(white, PieceType.fromSummary(line[j].charAt(1)), j, i);
|
||||
Piece piece = new Piece(white, PieceType.fromSummary(line[i].charAt(1)), i, j);
|
||||
board[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (array[lineNum] == "B") {
|
||||
whiteTurn = false;
|
||||
}
|
||||
else {
|
||||
whiteTurn = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -358,12 +371,50 @@ public class Board {
|
|||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
//TODO
|
||||
if (this.turns > 0) {
|
||||
states.remove(this.turns);
|
||||
this.turns = turns - 1;
|
||||
this.board = new Piece[lineNum][colNum];
|
||||
this.x = -1;
|
||||
this.y = -1;
|
||||
|
||||
String[] undo = states.get(turns).split("\n");
|
||||
String[] line;
|
||||
boolean white;
|
||||
|
||||
for (int j = 0; j < colNum; j++) {
|
||||
line = undo[j].split(",");
|
||||
for (int i = 0; i < lineNum; i++) {
|
||||
if (line[i].charAt(0) != ' ') {
|
||||
white = false;
|
||||
if (line[i].charAt(0) == 'W') {
|
||||
white = true;
|
||||
}
|
||||
Piece piece = new Piece(white, PieceType.fromSummary(line[i].charAt(1)), i, j);
|
||||
board[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (undo[lineNum].charAt(0) == 'B' || turns == 0) {
|
||||
this.whiteTurn = true;
|
||||
}
|
||||
else {
|
||||
this.whiteTurn = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Board(Board board) {
|
||||
//TODO
|
||||
Piece[][] newBoard = new Piece[lineNum][colNum];
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
Piece piece = board.getPieceAt(i,j);
|
||||
if (piece != null) {
|
||||
newBoard[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
|
|
@ -371,6 +422,7 @@ public class Board {
|
|||
board[move.getFromY()][move.getFromX()].setY(move.getToY());
|
||||
board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()];
|
||||
board[move.getFromY()][move.getFromX()] = null;
|
||||
this.states.add(toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,8 +141,9 @@ public class MoveCalculator {
|
|||
Piece[][] temporaryBoard = new Piece[lineNum][colNum];
|
||||
for (int i = 0; i < colNum; i++) {
|
||||
for (int j = 0; j < lineNum; j++) {
|
||||
if (board1[j][i] != null) {
|
||||
temporaryBoard[j][i] = new Piece(board1[j][i]);
|
||||
Piece piece = board1[j][i];
|
||||
if (piece != null) {
|
||||
temporaryBoard[j][i] = new Piece(piece);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -231,20 +232,13 @@ public class MoveCalculator {
|
|||
}
|
||||
}
|
||||
|
||||
// the piece selected is a knight or the king (it has a precise number of possible moves)
|
||||
if (type == PieceType.Knight || type == PieceType.King) {
|
||||
|
||||
if (type == PieceType.Knight) {
|
||||
pieceMoves = knightMoves;
|
||||
}
|
||||
else {
|
||||
pieceMoves = kingMoves;
|
||||
}
|
||||
// the piece selected is the king
|
||||
if (type == PieceType.King) {
|
||||
|
||||
// iterates for the different possible moves
|
||||
for (int n = 0; n < pieceMoves.length; n++) {
|
||||
i = x + pieceMoves[n][0];
|
||||
j = y + pieceMoves[n][1];
|
||||
for (int n = 0; n < kingMoves.length; n++) {
|
||||
i = x + kingMoves[n][0];
|
||||
j = y + kingMoves[n][1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
|
|
@ -264,11 +258,31 @@ public class MoveCalculator {
|
|||
}
|
||||
}
|
||||
// if the location is not empty, verify if the piece met is from the opponent and that it won't let the king in check
|
||||
else if (n < 8 && board1[j][i] != null && board1[j][i].isWhite() != isWhite && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
else if (n < 8 && board1[j][i] != null && board1[j][i].isWhite() != isWhite && check(board1,i,j,isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
// if the location is empty, verify that it won't let the king in check
|
||||
else if (n < 8 && board1[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false){
|
||||
else if (n < 8 && board1[j][i] == null && check(board1,i,j,isWhite) == false){
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (type == PieceType.Knight) {
|
||||
// iterates for the different possible moves
|
||||
for (int n = 0; n < knightMoves.length; n++) {
|
||||
i = x + knightMoves[n][0];
|
||||
j = y + knightMoves[n][1];
|
||||
Move move = new Move(x,y,i,j);
|
||||
// verify if the coordinates are still in the board
|
||||
if (inBoard(i,j)) {
|
||||
// if the location is not empty, verify if the piece met is from the opponent and that it won't let the king in check
|
||||
if (board1[j][i] != null && board1[j][i].isWhite() != isWhite && checkIf(move,coordinates[0],coordinates[1],isWhite) == false) {
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
// if the location is empty, verify that it won't let the king in check
|
||||
else if (board1[j][i] == null && checkIf(move,coordinates[0],coordinates[1],isWhite) == false){
|
||||
moves.add(new int[] {i, j});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,11 +38,11 @@ public class Piece {
|
|||
return this.isWhite;
|
||||
}
|
||||
|
||||
public Piece(Piece other) {
|
||||
this.isWhite = other.isWhite;
|
||||
this.type = other.type;
|
||||
this.x = other.x;
|
||||
this.y = other.y;
|
||||
public Piece(Piece piece) {
|
||||
this.isWhite = piece.isWhite;
|
||||
this.type = piece.type;
|
||||
this.x = piece.x;
|
||||
this.y = piece.y;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue