This commit is contained in:
2025-02-01 20:45:56 +01:00
parent 52ca8b208c
commit 9e2421accf
3 changed files with 84 additions and 10 deletions

View File

@@ -15,27 +15,35 @@ public class StateManager {
this.doku = doku;
}
public void pushState() {
states.add(new HashMap<>());
saveState();
public Map<Cell, Integer> pushState() {
states.add(saveState());
return states.getLast();
}
public void popState() {
assert (states.size() > 0);
restoreState();
states.pop();
restoreState(states.pop());
}
private void restoreState() {
for (var entry : this.states.getLast().entrySet()) {
public Map<Cell, Integer> popAndGetState() {
assert (states.size() > 0);
var currentState = saveState();
restoreState(states.pop());
return currentState;
}
private void restoreState(Map<Cell, Integer> state) {
for (var entry : state.entrySet()) {
entry.getKey().setSymbolIndex(entry.getValue());
}
}
private void saveState() {
private Map<Cell, Integer> saveState() {
Map<Cell, Integer> state = new HashMap<>();
for (Cell cell : this.doku.getCells()) {
states.getLast().put(cell, cell.getSymbolIndex());
state.put(cell, cell.getSymbolIndex());
}
return state;
}
}