-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiplication.rb
executable file
·88 lines (75 loc) · 1.99 KB
/
multiplication.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
@@right = 0
@@counter = 0
@@tables = Array.new
def create_question(max = 10)
operators = %w(+ -)
integers = (1 .. 20).to_a
operand1 = (integers.sort_by { rand }).first
operand2 = (integers.sort_by { rand }).first
operator = (operators.sort_by {rand}).first
question = "#{operand1} #{operator} #{operand2}"
if (eval(question) > max || eval(question) <= 0)
return create_question(max)
else
question
end
end
def display_question
question = create_question(100)
answer = process_question(question)
#raise "answer = #{answer} and answer.class = #{answer.class}"
if answer.chomp == 's' || answer.chomp == 'S'
puts "Goed: #{@@right}; fout: #{@@counter - @@right}. Totaal aantal sommen: #{@@counter}"
return
end
if answer.to_i == eval(question)
@@right += 1
puts "Goed!"
else
puts "Fout: #{question} = #{eval(question)}"
end
@@counter += 1
#sleep 1 # wait 1 second
gets
display_question
end
def process_question(question)
print question + " = "
answer = gets
if /\S/ !~ answer
return process_question(question)
end
return answer
end
def start
puts "Welke tafels wil je oefenen?"
@@tables = gets.split(",")
display_multiplication
end
def create_multiplication
integers = (1 .. 10).to_a
operand1 = (integers.sort_by { rand }).first
operand2 = (@@tables.sort_by { rand }).first
question = "#{operand1} x #{operand2}"
end
def display_multiplication
question = create_multiplication()
answer = process_question(question)
#raise "answer = #{answer} and answer.class = #{answer.class}"
if answer.chomp == 's' || answer.chomp == 'S'
puts "Goed: #{@@right}; fout: #{@@counter - @@right}. Totaal aantal sommen: #{@@counter}"
return
end
parsed_question = question.gsub('x', '*');
if answer.to_i == eval(parsed_question)
@@right += 1
puts "Goed!"
else
puts "Fout: #{question} = #{eval(parsed_question)}"
end
@@counter += 1
#sleep 1 # wait 1 second
gets
display_multiplication
end
start