undo last move

This commit is contained in:
Lucie 2025-05-13 13:05:06 +02:00
parent e7a64f69a7
commit 5a54286394
4 changed files with 103 additions and 37 deletions

View File

@ -12,7 +12,7 @@ public class Main {
// testing : // testing :
Board testBoard = new Board(8, 8); Board testBoard = new Board(8, 8);
testBoard.populateBoard(); testBoard.populateBoard();
System.out.println(testBoard.toString()); //System.out.println(testBoard.toString());
// launches graphical interface : // launches graphical interface :
MyInterface mjf = new MyInterface(); MyInterface mjf = new MyInterface();
mjf.setVisible(true); mjf.setVisible(true);

View File

@ -24,6 +24,7 @@ public class Board {
private boolean blackKing; private boolean blackKing;
private boolean castlingRight; private boolean castlingRight;
private boolean castlingLeft; private boolean castlingLeft;
ArrayList<String> states;
// CONSTRUCTOR // CONSTRUCTOR
public Board(int colNum, int lineNum) { public Board(int colNum, int lineNum) {
@ -48,7 +49,8 @@ public class Board {
blackKing = false; blackKing = false;
castlingRight = false; castlingRight = false;
castlingLeft = false; castlingLeft = false;
} states = new ArrayList<>();
}
// GETTERS // GETTERS
public int getWidth() { public int getWidth() {
@ -85,6 +87,7 @@ public class Board {
blackRookRight = false; blackRookRight = false;
whiteKing = false; whiteKing = false;
blackKing = false; blackKing = false;
states = new ArrayList<>();
} }
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
@ -110,6 +113,7 @@ public class Board {
setPiece(white, PieceType.King, 4, i); setPiece(white, PieceType.King, 4, i);
} }
} }
states.add(toString());
} }
public void cleanBoard() { public void cleanBoard() {
@ -144,6 +148,11 @@ public class Board {
} }
str += "\n"; str += "\n";
} }
String turn = "B";
if (isTurnWhite()) {
turn = "W";
}
str += turn;
return str; return str;
} }
@ -290,11 +299,7 @@ public class Board {
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
String turn = "B"; String myFile = toString();
if (isTurnWhite()) {
turn = "W";
}
String myFile = toString() + turn;
String[] myArray = myFile.split("\n"); String[] myArray = myFile.split("\n");
return myArray; return myArray;
} }
@ -303,24 +308,32 @@ public class Board {
this.colNum = 8; this.colNum = 8;
this.lineNum = 8; this.lineNum = 8;
this.board = new Piece[lineNum][colNum]; this.board = new Piece[lineNum][colNum];
this.x = -1;
this.y = -1;
//this.turns = turns;
String[] line; String[] line;
boolean white; boolean white;
for (int i = 0; i < colNum; i++) {
line = array[i].split(","); for (int j = 0; j < lineNum; j++) {
for (int j = 0; j < lineNum; j++) { line = array[j].split(",");
if (line[j] != " ") { for (int i = 0; i < colNum; i++) {
if (line[i] != " ") {
white = false; white = false;
if (line[j].charAt(0) == 'W') { if (line[i].charAt(0) == 'W') {
white = true; 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") { if (array[lineNum] == "B") {
whiteTurn = false; whiteTurn = false;
} }
else {
whiteTurn = true;
}
} }
@ -358,12 +371,50 @@ public class Board {
} }
public void undoLastMove() { 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) { 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) { public void playMove(Move move) {
@ -371,6 +422,7 @@ public class Board {
board[move.getFromY()][move.getFromX()].setY(move.getToY()); board[move.getFromY()][move.getFromX()].setY(move.getToY());
board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()]; board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()];
board[move.getFromY()][move.getFromX()] = null; board[move.getFromY()][move.getFromX()] = null;
this.states.add(toString());
} }
} }

View File

@ -141,8 +141,9 @@ public class MoveCalculator {
Piece[][] temporaryBoard = new Piece[lineNum][colNum]; Piece[][] temporaryBoard = new Piece[lineNum][colNum];
for (int i = 0; i < colNum; i++) { for (int i = 0; i < colNum; i++) {
for (int j = 0; j < lineNum; j++) { for (int j = 0; j < lineNum; j++) {
if (board1[j][i] != null) { Piece piece = board1[j][i];
temporaryBoard[j][i] = new 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) // the piece selected is the king
if (type == PieceType.Knight || type == PieceType.King) { if (type == PieceType.King) {
if (type == PieceType.Knight) {
pieceMoves = knightMoves;
}
else {
pieceMoves = kingMoves;
}
// iterates for the different possible moves // iterates for the different possible moves
for (int n = 0; n < pieceMoves.length; n++) { for (int n = 0; n < kingMoves.length; n++) {
i = x + pieceMoves[n][0]; i = x + kingMoves[n][0];
j = y + pieceMoves[n][1]; j = y + kingMoves[n][1];
Move move = new Move(x,y,i,j); Move move = new Move(x,y,i,j);
// verify if the coordinates are still in the board // verify if the coordinates are still in the board
if (inBoard(i,j)) { 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 // 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}); moves.add(new int[] {i, j});
} }
// if the location is empty, verify that it won't let the king in check // 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}); moves.add(new int[] {i, j});
} }
} }

View File

@ -38,11 +38,11 @@ public class Piece {
return this.isWhite; return this.isWhite;
} }
public Piece(Piece other) { public Piece(Piece piece) {
this.isWhite = other.isWhite; this.isWhite = piece.isWhite;
this.type = other.type; this.type = piece.type;
this.x = other.x; this.x = piece.x;
this.y = other.y; this.y = piece.y;
} }
} }