From 5a5428639483f52a2066fbf0a8e861cad2517f38 Mon Sep 17 00:00:00 2001 From: Lucie Date: Tue, 13 May 2025 13:05:06 +0200 Subject: [PATCH] undo last move --- src/Main.java | 2 +- src/backend/Board.java | 82 +++++++++++++++++++++++++++------ src/backend/MoveCalculator.java | 46 +++++++++++------- src/backend/Piece.java | 10 ++-- 4 files changed, 103 insertions(+), 37 deletions(-) diff --git a/src/Main.java b/src/Main.java index 551c1c6..55790c5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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); diff --git a/src/backend/Board.java b/src/backend/Board.java index 9a6ce07..98a2bf3 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -24,6 +24,7 @@ public class Board { private boolean blackKing; private boolean castlingRight; private boolean castlingLeft; + ArrayList states; // CONSTRUCTOR public Board(int colNum, int lineNum) { @@ -48,7 +49,8 @@ public class Board { blackKing = false; castlingRight = false; castlingLeft = false; - } + states = new ArrayList<>(); + } // GETTERS public int getWidth() { @@ -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] != " ") { + + for (int j = 0; j < lineNum; 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()); } } diff --git a/src/backend/MoveCalculator.java b/src/backend/MoveCalculator.java index 9844fee..6a1e5fd 100644 --- a/src/backend/MoveCalculator.java +++ b/src/backend/MoveCalculator.java @@ -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}); } } diff --git a/src/backend/Piece.java b/src/backend/Piece.java index 4127cc9..cc7468f 100644 --- a/src/backend/Piece.java +++ b/src/backend/Piece.java @@ -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; } }