using Cursach.Realisations; using ProtoBuf; namespace Cursach.States; public class StatesStorage { List states = []; public int Count => states.Count; public int CurrentStateIndex { get; private set; } = 0; public bool NextState() { CurrentStateIndex += 1; if (CurrentStateIndex >= Count) { CurrentStateIndex = Count - 1; return false; } return true; } public bool PrevState() { CurrentStateIndex -= 1; if (CurrentStateIndex < 0) { CurrentStateIndex = 0; return false; } return true; } public State? GetCurrentState() { return GetState(CurrentStateIndex); } public bool AddState(State state) { states.Add(state); return true; } public State? GetState(int index) { if (index >= 0 && index < states.Count) { return states[index]; } return null; } public State? GoToFirstState() { if (states.Count != 0) { CurrentStateIndex = 0; return states[0]; } return null; } public State? GetFirstState() { if (states.Count != 0) { return states[0]; } return null; } public State? GetLastState() { if (states.Count != 0) { return states[Count - 1]; } return null; } public bool SaveStateList(string filename) { try { using var file = File.Create(filename); Serializer.Serialize(file, new StateList(states)); return true; } catch (Exception) { return false; } } public bool LoadStateList(string filename) { try { StateList tmpStates; using (var file = File.OpenRead(filename)) { tmpStates = Serializer.Deserialize(file); states = tmpStates.States; CurrentStateIndex = 0; return true; } } catch (Exception) { return false; } } public Node GetStartNode() { if (GetCurrentState().visited.Count == 0) { return GetCurrentState().queue.Last(); } return GetCurrentState().visited[0]; } }