Last active
December 21, 2018 22:01
-
-
Save florabtw/cad0564ef5e7f7aca95ca051dd54793b to your computer and use it in GitHub Desktop.
Counter Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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