------ extPart_000_01B5_01C4FC09.5579B000
Content-Type: text/plain;
charset S-ASCII"
Content-Transfer-Encoding: 7bit
Here's my attempt. It's longer than I would have liked, too many if/else's, but I'm still catching on to Ruby.
-Lee
------ extPart_000_01B5_01C4FC09.5579B000
Content-Type: application/octet-stream;
name uiz15.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename uiz15.rb"
$stdout.sync rue
class Question
attr_accessor :parent
attr_reader :q
attr_accessor :yes
attr_accessor :no
attr_reader :answer
def initialize(question, answer il)
@q uestion
@answer nswer
end
end
class Quiz
def initialize(quiz_type, first_guess)
@type uiz_type
@root uestion.new("Is it #{first_guess}?", first_guess)
end
def a_an(word)
/^[^aeiou]/i word ? "a" : "an"
end
def ask(prompt)
puts prompt
gets.chomp
end
def ask?(prompt)
/^y/i ask("#{prompt} (y or n)")
end
def add_question(failed_question)
new_ans sk("What #{@type} were you thinking of?")
new_ques sk("Give me a question to distinguish #{new_ans} from #{failed_question.answer}.")
differentiator uestion.new(new_ques)
if ask?("For #{new_ans}, what is the answer to your question?")
differentiator.yes uestion.new("Is it #{new_ans}?", new_ans)
differentiator.no ailed_question
else
differentiator.no uestion.new("Is it #{new_ans}?", new_ans)
differentiator.yes ailed_question
end
parent_question ailed_question.parent
if parent_question
differentiator.parent arent_question
if parent_question.yes failed_question
parent_question.yes ifferentiator
else
parent_question.no ifferentiator
end
else
@root ifferentiator
end
differentiator.yes.parent ifferentiator.no.parent ifferentiator
puts "Thanks"
end
def play
playing rue
while playing
puts "Think of #{a_an(@type)} #{@type}..."
question root
win alse
continue rue
while continue
if ask?(question.q)
if question.answer
win rue
continue alse
else
question uestion.yes
end
else
if question.no
question uestion.no
else
continue alse
end
end
end
if win
puts "I win. Pretty smart, ain't I?"
else
puts "You win. Help me learn from my mistake before you go..."
add_question(question)
end
playing sk?("Play again?")
end
end
end
quiz uiz.new("animal", "an elephant")
#quiz uiz.new("food", "Is it an apple?")
quiz.play
------ extPart_000_01B5_01C4FC09.5579B000--