you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction
you might benefit from studying state machines. Here is a small introduction to the topic: Yet another Finite State Machine introduction