513
edits
No edit summary |
|||
Line 95: | Line 95: | ||
Translating our diagram into a functional program is a relatively simple task. | Translating our diagram into a functional program is a relatively simple task. | ||
Step 1: saving the current state into a variable | |||
'''Step 1:''' saving the current state into a variable | |||
First of all we need a variable that saves the current state. It represents our State Machines memory. | First of all we need a variable that saves the current state. It represents our State Machines memory. | ||
Line 106: | Line 107: | ||
Step 2: choosing alternative actions | '''Step 2:''' choosing alternative actions | ||
Now we need something that executes different code depending on the current state. | Now we need something that executes different code depending on the current state. | ||
Line 116: | Line 117: | ||
Step 3: defining the practical behavior of each state | '''Step 3:''' defining the practical behavior of each state | ||
From now on it makes sense to use functions (aggregation of code) in order to keep a clean overview of our program. | From now on it makes sense to use functions (aggregation of code) in order to keep a clean overview of our program. | ||
Line 133: | Line 134: | ||
Step 4: assembling everything | '''Step 4:''' assembling everything | ||
We can now call each particular function in the appropriate part of the switch/case statement. | We can now call each particular function in the appropriate part of the switch/case statement. | ||
Line 139: | Line 140: | ||
Step 5: extending the program | '''Step 5:''' extending the program | ||
This kind of program structure can easily be extended by additional states without disturbing the overall clarity. | This kind of program structure can easily be extended by additional states without disturbing the overall clarity. | ||
Therefore it makes sense to begin with a simple structure and to then add functions that are more complex. | Therefore it makes sense to begin with a simple structure and to then add functions that are more complex. |
edits