Skip to content

Instantly share code, notes, and snippets.

@florabtw
Last active December 21, 2018 22:01
Show Gist options
  • Save florabtw/cad0564ef5e7f7aca95ca051dd54793b to your computer and use it in GitHub Desktop.
Save florabtw/cad0564ef5e7f7aca95ca051dd54793b to your computer and use it in GitHub Desktop.
Counter Game
module Actions
class Increment
def self.call(state)
{ count: state[:count] + 1 }
end
end
class Decrement
def self.call(state)
{ count: state[:count] - 1 }
end
end
end
class Graphics
def self.drawAction(action)
puts "Aha! An #{action} just happened."
end
def self.drawState(state)
puts "The count is now #{state[:count]}"
end
end
ActionTypes = [:increment, :decrement]
ActionMap = {
increment: Actions::Increment,
decrement: Actions::Decrement
}
class Game
def self.apply(action, state)
handler = ActionMap[action]
handler.call(state)
end
end
state = { count: 0 }
Graphics.drawState(state)
(1..10).each do |i|
randomIndex = rand(2)
action = ActionTypes[randomIndex]
state = Game.apply(action, state)
Graphics.drawAction(action)
Graphics.drawState(state)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment