-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStackFiniteStateMachine.cs
More file actions
33 lines (27 loc) · 874 Bytes
/
StackFiniteStateMachine.cs
File metadata and controls
33 lines (27 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
namespace AlgorithmsAndDataStructures.DataStructures.StateMachine;
// This type of FSM is usefull for tracking previous state if you can return to different state from the current one,
// so backward transition is not straightforward.
// Important note: It's state who is reposnsible for poping itself from the stack and push next state onto the stack
public class StackFiniteStateMachine
{
private readonly Stack<Action> stack = new();
public void Turn()
{
GetCurrentState()?.Invoke();
}
public void PushState(Action state)
{
stack.Push(state);
}
private Action GetCurrentState()
{
return stack.Count > 0 ? stack.Peek() : null;
}
public Action PopState()
{
var state = stack.Count > 0 ? stack.Pop() : null;
return state;
}
}