PIBD-14_Boyko_M_S_Cursach_DFS/Cursach/States/StatesStorage.cs

128 lines
2.4 KiB
C#
Raw Permalink Normal View History

2024-05-16 00:37:58 +04:00
using Cursach.Realisations;
using ProtoBuf;
namespace Cursach.States;
2024-05-22 20:29:11 +04:00
public class StatesStorage
2024-05-16 00:37:58 +04:00
{
2024-05-22 20:29:11 +04:00
List<State> states = [];
2024-05-16 00:37:58 +04:00
public int Count => states.Count;
2024-05-22 20:29:11 +04:00
public int CurrentStateIndex { get; private set; } = 0;
2024-05-16 00:37:58 +04:00
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;
}
2024-05-22 20:29:11 +04:00
public State? GoToFirstState()
{
if (states.Count != 0)
{
CurrentStateIndex = 0;
return states[0];
}
return null;
}
2024-05-16 00:37:58 +04:00
public State? GetFirstState()
{
2024-05-22 20:29:11 +04:00
if (states.Count != 0)
2024-05-16 00:37:58 +04:00
{
return states[0];
}
return null;
}
public State? GetLastState()
{
2024-05-22 20:29:11 +04:00
if (states.Count != 0)
2024-05-16 00:37:58 +04:00
{
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<StateList>(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];
}
}