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.
Implementing a finite-state machine
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.