Implementing a finite-state machine
Another interesting, yet easy-to-implement, technique is the finite-state machine (FSM). Finite-state machines move us to change the train of thought from what it was in the previous recipe. FSMs are great when our train of thought is more event-oriented, and we think in terms of holding a behavior until a condition is met, changing to another.
Getting ready
This is a technique mostly based on the behavior of automata, and will lay the foundations for the next recipe, which is an improved version of the current one.
How to do it...
This recipe is broken down into implementing three classes from the ground up. Don't worry, everything will make sense by the final step:
- First, we'll implement the
Condition
class, as shown in the following code:
public class Condition { public virtual bool Test() { return false; } }
- Next, we're going to define the
Transition
class, as demonstrated in the following code:
public class Transition { ...