diff --git a/OOP_3B5_Project/src/backend/Board.java b/OOP_3B5_Project/src/backend/Board.java index 9ce9c2b..3742d3d 100644 --- a/OOP_3B5_Project/src/backend/Board.java +++ b/OOP_3B5_Project/src/backend/Board.java @@ -85,7 +85,8 @@ public class Board { for (int i = 0; i <= 7; i++) { pieces.add(new Piece(i, 6, PieceType.Pawn, true)); } - + + saveBoardState(); // to save first state of the board } public void cleanBoard() { @@ -258,6 +259,15 @@ public class Board { return concernedPiece; // returns the piece } + public void saveBoardState() { + ArrayList boardCopy = new ArrayList<>(); // create arraylist to store in hashmap + for (Piece piece : pieces) { + + boardCopy.add(new Piece(piece)); // works thank to copy constructor in piece class + } + boardHistory.put(turnNb, boardCopy); // put the turnNb as the key, and the arraylist data as value + } + public void undoLastMove() { //TODO diff --git a/OOP_3B5_Project/src/backend/Move.java b/OOP_3B5_Project/src/backend/Move.java index 7224b3a..2803f5d 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -31,13 +31,14 @@ public class Move { if (board.isAPieceThere(toX, toY)){//checking if a piece is at the arrival coordinates needPieceDeletion(toX, toY); //if a piece is at arrival coord, remove the piece } - specialMoves.pawnPromotion(movingPiece, toY); - specialMoves.castlingShort(movingPiece, toX, toY); - specialMoves.castlingLong(movingPiece, toX, toY); + specialMoves.pawnPromotion(movingPiece, toY); // check if there is a case of pawn promotion + specialMoves.castlingShort(movingPiece, toX, toY); // check if there is a case of short castling + specialMoves.castlingLong(movingPiece, toX, toY); // check if there is a case of long castling movingPiece.setX(toX); // change coordinates to the new coordinate movingPiece.setY(toY); - board.setTurnNb(turnNb + 1); - System.out.println(board.toString()); + board.setTurnNb(turnNb + 1); // adds + 1 to turn nb + board.saveBoardState(); // after updating the pieces on the board and the turnNb, saves the current state of the board in a hashmap + System.out.println(board.toString()); // prints the current state of board } break; } diff --git a/OOP_3B5_Project/src/backend/Piece.java b/OOP_3B5_Project/src/backend/Piece.java index 9078d07..320ce7a 100644 --- a/OOP_3B5_Project/src/backend/Piece.java +++ b/OOP_3B5_Project/src/backend/Piece.java @@ -16,6 +16,14 @@ public class Piece { //this.index = indexPiece; } + // copy constructor to create pieces without precision of all specifications, just copies whatever piece you input + public Piece(Piece other) { + this.x = other.x; + this.y = other.y; + this.type = other.type; + this.color = other.color; + } + // xcoord of the piece public int getX() { return x;