Skip to content

Instantly share code, notes, and snippets.

@jtallant
Created December 13, 2016 22:37
Show Gist options
  • Save jtallant/bdc1efcb9cef1ae1ee507aa035c7ab7f to your computer and use it in GitHub Desktop.
Save jtallant/bdc1efcb9cef1ae1ee507aa035c7ab7f to your computer and use it in GitHub Desktop.
Choose your own adventure Ruby helper
class Choice
attr_reader :text
attr_reader :decision_a
attr_reader :decision_b
def initialize(text, decision_a, decision_b)
@text = text
@decision_a = decision_a
@decision_b = decision_b
end
def prompt
puts @text + " (#{selections.join('|')}) "
response = gets.chomp.downcase
if response == decision_a.selection
decision_a.outcome.prompt
elsif response == decision_b.selection
decision_b.outcome.prompt
else
puts "Response must be #{selections.join(' or ')}"
prompt
end
end
def selections
[decision_a.selection, decision_b.selection]
end
end
class Decision
attr_reader :selection
attr_reader :outcome
def initialize(selection, outcome)
@selection = selection
@outcome = outcome
end
end
class Ending
def initialize(text)
@text = text
end
def prompt
puts @text
exit
end
end
class Death < Ending
end
class Victory < Ending
end
Choice.new('Which direction?',
Decision.new('left', Death.new("You died!")),
Decision.new('right',
Choice.new('Which weapon?',
Decision.new('axe',
Choice.new('Up or down?',
Decision.new('up', Death.new('You died. Yay!')),
Decision.new('down', Death.new('You died no matter what! HAHAHA!'))
)),
Decision.new('chainsaw', Victory.new('You won by murdering people with a chainsaw!'))
)
)
).prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment