-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_card2_classes.rb
152 lines (132 loc) · 4.07 KB
/
flash_card2_classes.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Fact
attr_accessor :question, :answer, :metric, :repeat_count, :last_displayed
def initialize(question_answer)
@question = question_answer[0]
@answer = question_answer[1]
# learning metric
@metric = 1.0
@last_displayed = false
end
end
# card class
class Cards
attr_accessor :facts, :userAnswer
def initialize()
@facts = []
@userAnswer = nil
@user_score = 0
end
def random_question_mode()
for i in [email protected]
facts[i].metric = 1
end
prompt = TTY::Prompt.new
@user_score = 0
puts
questionLimit = prompt.ask(" How many questions would you like to answer? ")
# checks if input is an integer
isNumber = /\A[+-]?\d+(\.[\d]+)?\z/
questionLimit = questionLimit.to_s unless questionLimit.is_a? String
if isNumber.match "questionLimit"
puts " Invalid input; limit set to 15"
questionLimit = 15
else
questionLimit = questionLimit.to_i
end
question_count = 0
puts `clear`
while question_count < questionLimit
# sort facts by learning metric, checking to see if they were last displayed
i = 0
@facts = sort_by_metric(@facts)
if @facts[i].last_displayed
i = rand([email protected])
end
userAnswer = prompt.ask(" #{@facts[i].question} ")
puts check_answer(userAnswer, i)
@facts[i].last_displayed = !@facts[i].last_displayed
question_count += 1
end
font = TTY::Font.new(:doom)
print TTY::Box.frame font.write("You got #{@user_score}/#{questionLimit}").colorize(:red)
sleep(2)
@user_score = 0
menu_choice()
end
def check_answer(userAnswer, i)
if @facts[i].answer.downcase == userAnswer.to_s.downcase
@facts[i].metric *= 1.2
@user_score += 1
return " Nice Work! \n ------- "
else
@facts[i].metric *= 0.8
wrongAnswer = " Nup, the answer is: " +"#{@facts[i].answer}".colorize(:red) + " \n -------"
return wrongAnswer
end
end
def sort_by_metric(array)
#bubble sort
return array if array.size <= 1
swap = true
while swap
swap = false
(array.length - 1).times do |x|
if array[x].metric > array[x+1].metric
array[x], array[x+1] = array[x+1], array[x]
swap = true
end
end
end
return array
end
def choose_q()
# list facts and allow you to choose using tty-prompt
prompt = TTY::Prompt.new
questions = []
@facts.each do |fact|
questions.push(fact.question)
end
choice = prompt.select(" Choose a question", questions)
for i in [email protected]
if choice == @facts[i].question
userAnswer = prompt.ask(" #{choice}")
puts check_answer(userAnswer, i)
end
end
sleep(2)
menu_choice()
end
def add_fact
prompt = TTY::Prompt.new
q_prompt = prompt.ask(" What is your question?")
q = " " + q_prompt if q_prompt != nil
a_prompt = prompt.ask(" What is the answer?")
a = " " + a_prompt if a_prompt != nil
@facts.push(Fact.new([q, a]))
puts " Question successfully added to deck."
sleep(2)
menu_choice()
end
end
def menu_choice
puts`clear`
choices = [
" Choose Questions Mode",
" Random Question Mode",
" Add new question",
" Exit"
]
prompt = TTY::Prompt.new
choice = prompt.select(" What do you you want to do?\n", choices)
case choice
when choices[0]
$deck.choose_q()
when choices[1]
$deck.random_question_mode()
when choices[2]
$deck.add_fact()
else
puts
exit
end
end