Skip to content

Instantly share code, notes, and snippets.

@adkron
Created February 6, 2014 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adkron/8838273 to your computer and use it in GitHub Desktop.
Save adkron/8838273 to your computer and use it in GitHub Desktop.
require "data_bank"
class Node < Struct.new(:question, :yes, :no)
def call(*)
puts question
answer = gets
case answer.chomp
when ?y
yes.call(self)
when ?n
no.call(self)
else
throw :quit
end
end
def replace(yes_or_no)
puts "What is it?"
answer = Thing.new(gets.chomp)
puts "Give me a question that would be yes for #{yes_or_no}, and no for #{answer}"
question = gets.chomp
node = Node.new(question, yes_or_no, answer)
self.yes = node if yes_or_no == yes
self.no = node if yes_or_no == no
end
end
Thing = Struct.new(:name) do
def call(parent)
puts "Is it a #{name}?"
answer = gets
case answer.chomp
when ?y
puts "Got it!"
when ?n
parent.replace(self)
end
end
def to_s
name
end
end
fish = Thing.new("fish")
tree = Thing.new("tree")
db = DataBank.new(".", "juliet.data")
head = db.withdraw || Node.new("Is it an animal?", fish, tree)
until false do
head.call
db.deposit head
puts "="*100
end
catch :quit do
puts "Thanks for playing"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment