132 lines
2.5 KiB
C#
132 lines
2.5 KiB
C#
|
using Cursach.Realisations;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel.Design;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using ProtoBuf;
|
|||
|
|
|||
|
namespace Cursach.States;
|
|||
|
|
|||
|
public class StatesManager
|
|||
|
{
|
|||
|
List<State> states;
|
|||
|
public int Count => states.Count;
|
|||
|
public int CurrentStateIndex { get; private set; }
|
|||
|
|
|||
|
public StatesManager()
|
|||
|
{
|
|||
|
states = [];
|
|||
|
CurrentStateIndex = 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? 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<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];
|
|||
|
}
|
|||
|
}
|