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
This commit is contained in:
parent
48a3f7468d
commit
29935b84dc
|
|
@ -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<Piece> 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<Integer, ArrayList<Piece>> 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue