Just in case you haven’t seen these:
But both of these just use if else statements to represent the state machine.
I can think of a couple of ways to solve this.
Approach 1: MAP transform
Put the data structure into a .map file with the current state + the input as the key and the new state as the value.
running-pausepressed = paused
paused-pausedpressed = running
...
Then in the code you would have:
val newState = transform("MAP", "fsm.map", StateItem.state.toString + "-" + InputItem.state.toString)
Personally I think this is the easiest to work with approach.
Approach 2: Map
This approach builds the same thing using a Map instead of the .map file.
import java.util.Map
val fsm = createHashMap( "running-pausepressed" -> "paused",
"paused-pausedpressed" -> "running", ...
)
...
val newState = fsm.get(StateItem.state.toString + "-" + InputItem.state.toString)
The key to both of these approaches is realizing that you don’t need a 2D array to represent this data. If you treat the two-dimensional address just as a key then you can just use a Map.