StateManager Klasse erstellt für Redo/ Undo Logik
parent
2205f1bf04
commit
1b64e31951
|
@ -0,0 +1,37 @@
|
||||||
|
package PR2.HitoriSpiel.Domain;
|
||||||
|
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
public class StateManager {
|
||||||
|
private final Stack<int[][]> undoStack = new Stack<>();
|
||||||
|
private final Stack<int[][]> redoStack = new Stack<>();
|
||||||
|
|
||||||
|
public void saveState(int[][] state) {
|
||||||
|
undoStack.push(copyArray(state));
|
||||||
|
redoStack.clear(); // Redo-Stack leeren, da ein neuer Zustand hinzugefügt wurde
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] undo(int[][] currentState) {
|
||||||
|
if (!undoStack.isEmpty()) {
|
||||||
|
redoStack.push(copyArray(currentState)); // Aktuellen Zustand im Redo-Stack speichern
|
||||||
|
return undoStack.pop();
|
||||||
|
}
|
||||||
|
return null; // Kein Zustand zum Zurücksetzen
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[][] redo(int[][] currentState) {
|
||||||
|
if (!redoStack.isEmpty()) {
|
||||||
|
undoStack.push(copyArray(currentState)); // Aktuellen Zustand im Undo-Stack speichern
|
||||||
|
return redoStack.pop();
|
||||||
|
}
|
||||||
|
return null; // Kein Zustand zum Wiederherstellen
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[][] copyArray(int[][] original) {
|
||||||
|
int[][] copy = new int[original.length][];
|
||||||
|
for (int i = 0; i < original.length; i++) {
|
||||||
|
copy[i] = original[i].clone();
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue