The answers here seem very complicated (but accurate, nonetheless.) So, here are my thoughts.
First, I like the dmckee (online) definition of FSM and how they apply to programming.
A state machine consists of a finite number of discrete states (I know pedantic, but still), which are usually represented as integer values. In c or C ++ using enumeration is very common.
The machine responds to a finite number of inputs, which can often be represented by another integer meaningful variable. In more complex cases, you can use a structure to represent the input state.
Each combination of internal state and external input will call in:
- possible transition to another state
- possibly generating some output
So you have a program. It has states, and their finite number. (“light bulb is bright” or “light bulb dims” or “light bulb is off.” 3 states are final.) Your program can only be in one state at a time.
So, say you want your program to change states. Usually you need something to happen to cause a state change. In this example, how about entering user input to determine the state — say, a keystroke.
You may need this logic. When the user presses a key:
- If the light is off, turn the light off.
- If the light “dims”, make the light “bright”.
- If the lamp is bright, turn the lamp off.
Obviously, instead of "replacing the light bulb", you can "change the color of the text" or whatever you do in the program. Before you begin, you want to define your states.
So, let's look at some pseudo-code C:
#define BULB_OFF 0 #define BULB_DIM 1 #define BULB_BRIGHT 2 int currentState = BULB_OFF; while(1) { waitForUserKeystroke(); switch(currentState) { case BULB_OFF: currentState = BULB_DIM; break; case BULB_DIM: currentState = BULB_BRIGHT; doCoolBulbStuff(); break; case BULB_BRIGHT: currentState = BULB_OFF; break; } }
And voila. A simple program that changes state.
This code executes only a small part of the switch , depending on the current state. He then updates this state. How FSM works.
Now here are a few things you can do:
Obviously, this program simply modifies the currentState variable. You want your code to do something more interesting when the state changes. The doCoolBulbStuff() function could, I don’t know, actually draw a light bulb on the screen. Or something.
This code only searches for keystrokes. But your FSM (and therefore your switch statement) can choose a state based on what the user enters (for example, "O" means "go off", and not just go to what's in the sequence.)
Part of your question asked the data structure.
One person suggested using enum to track conditions. This is a good #defines alternative that I used in my example. People also offered arrays - and these arrays track transitions between states. It is also a thin structure to use.
Given the above, you can use any structure (something tree-like, an array, whatever) to track individual states and determine what to do in each state (therefore, some of the suggestions for using "function pointers" - have a state map for a function pointer that indicates what to do in this state.)
Hope this helps!