require "erb"
puts "******************************************************"
puts "* *"
puts "* Animal Quiz *"
puts "* Author : volvet *"
puts "* *"
puts "******************************************************"
@qma = Hash.new
@qma["elephant"] = ""
@g_animals = ["elephant"]
@g_questions = [""]
@learn_template = ERB.new %q{Give me a question distinguish form
<%=correct_animal%> to <%=wrong_animal%>}
@guess_template = ERB.new %q{It is <%=animal%>(y/n)?}
def ask_if(answer)
answer =~ /^\s*[Yy]/
end
def get_answer()
answer = gets
answer ? answer.chomp : nil
end
def animal_loop
puts "Thinking of an animal..."
puts "Press Enter to start"
gets
animals = []
questions = []
@g_animals.each do |animal|
animals.push animal
end
@g_questions.each do |question|
questions.push question
end
while questions.size >= 1
question = questions.pop
animal = animals.pop
if( question == "" )
@guess_template.run(binding)
else
puts question
answer = get_answer
if ask_if(answer)
@guess_template.run(binding)
else
next
end
end
answer = get_answer
if ask_if(answer)
puts "Thanks"
break
else
wrong_animal = animal
puts "You win. Help me learn form my mistake before you go"
puts "What animal you thinking of?"
animal = get_answer
correct_animal = animal
@learn_template.run(binding)
puts ""
question = get_answer
@g_animals.push animal
@g_questions.push question
break;
end
end
end
loop do
animal_loop
puts "Play again(y/n)?"
answer = get_answer
break unless ask_if(answer)
end