StateManager Klasse erstellt für Redo/ Undo Logik

currentStatus
Vickvick2002 2025-01-03 21:01:39 +01:00
parent 2205f1bf04
commit 1b64e31951
1 changed files with 37 additions and 0 deletions

View File

@ -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;
}
}