My State Machine
- I want to have a puzzle where the user must select items AB+C
- This is a regular expression that describes the solution to
the puzzle.
- They must tap item A, followed by 1 or more taps to item B, followd by a tap to item C.
- A state machine describes a set of states that the solution to the puzzle could be in
- A start state where I have not seen valid input.
- State 1, where I have seen an A.
- State 2, where I have seen at least AB, but perhaps more B inputs
- State 3, where I have seen AB+C.
-
- State 0 is called the start state and is indicated by an arrow pointing to it.
- We always start in state 0.
- On a total fail or reset we will go back to state 0.
- State 3 is called the accepting state or final state and is indicated by the double circle.
- If we reach state 3, the puzzle has been solved.
- We could go back to the start state.
- Or we could remove or disable the puzzle.
- The state machine has a set of inputs, in this case A,B,C,D
- We generally define a set of transitions that occur
- It is a tuple (state, input) -> new state
- We draw this as an arrow from one state to another with a label
- So if we encounter an A in state 0, we transistion to state 1.
-
- We also define a default transition.
-
- So if we are in state 0, we can progress to state 1 by input A, otherwise we remain in state 0.
- So the final state table is
-
State/Input | A | B | C | D |
0 | 1 | 0 | 0 | 0 |
1 | 1 | 2 | 0 | 0 |
2 | 1 | 2 | 3 | 0 |
- We need the following
- Transition logic that interprets the state table.
- Possible logic that is triggered on entering a state.
- Possible logic that is triggered on exiting a state.
- My implementation.
- I have a set of items, that will provide "input" on collision.
- Detecting it is somewhat of a pain
-
- Note, I am using a variable input to record the input
- I then built state transition logic
-
- This takes the input and the state and computes the new state.
- You could have pre-state change or post-state change logic built in.
- In my case, I chose to have post state change logic.
-