Skip to content

Instantly share code, notes, and snippets.

@ibnesayeed
Created February 3, 2015 18:17
Show Gist options
  • Select an option

  • Save ibnesayeed/13cc58bb1971e7a081de to your computer and use it in GitHub Desktop.

Select an option

Save ibnesayeed/13cc58bb1971e7a081de to your computer and use it in GitHub Desktop.
Aruuz StateMachine Implementation
module Aruuz
class StateMachine
TRANSITIONS = {
"hindi": {
"-": [2, 4, -1, 0, 5, -1, 7, -1],
"=": [1, 0, 3, -1, 6, 1, -1, 0]
},
"zamzama": {
"-": [1, 2, -1, -1],
"=": [3, -1, 0, 0]
},
"orig_hindi": {
"-": [-1, 2, 3, -1],
"=": [1, 0, -1, 1]
}
}
def self.next_state meter, input, cur_state
return -1 if cur_state < 0
TRANSITIONS[meter][input][cur_state] || -1
end
end
end
# This is how it will be accessed
Aruuz::StateMachine.next_state "hindi", "-", 1
# => 4
module Aruuz
class StateMachine
attr_reader :state
@@transitions = {
"hindi": {
"-": [2, 4, -1, 0, 5, -1, 7, -1],
"=": [1, 0, 3, -1, 6, 1, -1, 0]
},
"zamzama": {
"-": [1, 2, -1, -1],
"=": [3, -1, 0, 0]
},
"orig_hindi": {
"-": [-1, 2, 3, -1],
"=": [1, 0, -1, 1]
}
}
def initialize meter
@trans = @@transitions[meter]
@state = 0
end
def next_state input
@state = @trans[input][@state] || -1 unless @state < 0
end
end
end
# This is how it will be accessed
sm = Aruuz::StateMachine.new "hindi"
# => 0
sm.state
# => 0
sm.next_state "-"
# => 2
sm.state
# => 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment