-
Notifications
You must be signed in to change notification settings - Fork 0
/
animal_finder.rb
82 lines (67 loc) · 2.2 KB
/
animal_finder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'forwardable'
class AnimalFinder
# Animal finder will attempt to use a classifier to determine the animal the
# user is thinking of, similar to 20 questions.
# it requires a ui to be passed in to communicate with the user.
# If the finder believes it has asked enough questions to try and guess the
# users animal it will then ask one additional question for future learning purposes.
extend Forwardable
#use delegator to delegate/alias calls to ui and classifier
def_delegators :@classifier,
:add_positive_constraint,
:empty_question,
:next_constraint,
:next_learning_question,
:potential_solutions,
:save_and_reset,
:skip,
:solved?
def_delegators :@ui,
:ask,
:ask_yes_no,
:tell
attr_reader :classifier, :ui
def initialize(classifier, ui)
@classifier = classifier
@ui = ui
end
def play
tell "Think of an animal and I will try to figure out what it is."
ask_clarifying_questions
get_answer(next_learning_question)
try_best_solution unless potential_solutions.empty?
ask_for_help unless solved?
end
def ask_clarifying_questions
get_answer(next_constraint)
ask_clarifying_questions if keep_playing?
end
def get_answer(constraint)
return if constraint.nil?
constraint.answer = ask_yes_no(constraint.question)
skip(constraint) if constraint.answer.nil?
end
def keep_playing?
next_constraint && (potential_solutions.length > 1)
end
def get_new_question
new_question = ask("Please enter a question to help me find your animal next game.")
add_positive_constraint(new_question)
end
def multiple_possibilities?
potential_solutions.length > 1
end
def show_potential_solutions
tell "I thought it might be one of these but could not decide:"
potential_solutions.each { |solution| tell solution.to_s }
end
def try_best_solution
result = ask_yes_no(potential_solutions.first.to_s + '?')
classifier.correct_solution = potential_solutions.first if result
end
def ask_for_help
show_potential_solutions if multiple_possibilities?
classifier.correct_solution = ask("What was the animal you were thinking of?").to_sym
get_new_question
end
end