From 29935b84dc8a541becda8f845d049e14e471d4b8 Mon Sep 17 00:00:00 2001 From: hugomanipoud2 Date: Fri, 23 May 2025 12:32:37 +0200 Subject: [PATCH] undolast move works unless we do a move, undo it, redo the same again, and undo it once again, it will give an incorrect state of the board, i try debugging it with hashmaps prints, the hasmaps updates correctly, the arraylist is emptied before adding the new pieces, idk what doesnt work, here is the code with the debugging prints --- OOP_3B5_Project/src/backend/Board.java | 22 ++++++++++++++++++++-- OOP_3B5_Project/src/backend/Move.java | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/OOP_3B5_Project/src/backend/Board.java b/OOP_3B5_Project/src/backend/Board.java index d21b43c..66b788e 100644 --- a/OOP_3B5_Project/src/backend/Board.java +++ b/OOP_3B5_Project/src/backend/Board.java @@ -2,6 +2,8 @@ package backend; import java.util.ArrayList; import java.util.HashMap; // for unddolastmove +import java.util.Map; + public class Board { private int column; // constructors for column and line @@ -268,21 +270,26 @@ public class Board { boardHistory.put(turnNb, boardCopy); // put the turnNb as the key, and the arraylist data as value } + + // if you dont do the same move nor undo it 2 times in a row it works, even with castling ( but you're not allowed to castle again lol), but idk why it bugs public void undoLastMove() { if (turnNb > 1) { // only if turnnb is positive - + turnNb--; ArrayList previousState = boardHistory.get(turnNb - 1);// create a temporary arraylist pieces.clear(); // removes all pieces from arraylist of pieces used pieces.addAll(previousState); // add all pieces from previous state of board to arraylist + boardHistory.remove(turnNb); - turnNb--; + + printBoardHistory(); }else if (turnNb == 1) { turnNb--; pieces.clear(); boardHistory.clear(); populateBoard(); + printBoardHistory(); } } @@ -295,5 +302,16 @@ public class Board { //TODO } + + public void printBoardHistory() { + System.out.println("Board History:"); + for (Map.Entry> entry : boardHistory.entrySet()) { + System.out.println("Turn " + entry.getKey() + ":"); + for (Piece piece : entry.getValue()) { + System.out.println(piece); // Assuming Piece has a toString method + } + System.out.println(); // Add a newline for better readability + } + } } diff --git a/OOP_3B5_Project/src/backend/Move.java b/OOP_3B5_Project/src/backend/Move.java index 2803f5d..5785c92 100644 --- a/OOP_3B5_Project/src/backend/Move.java +++ b/OOP_3B5_Project/src/backend/Move.java @@ -39,6 +39,7 @@ public class Move { 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 + board.printBoardHistory(); } break; }