-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimedMathGame.rb
More file actions
148 lines (137 loc) · 4.45 KB
/
TimedMathGame.rb
File metadata and controls
148 lines (137 loc) · 4.45 KB
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
#Reqire date is using code already written for us to help us with the time and date.
require 'date'
#when the timer is put on for a certain amount of time it keeps track of that time and when it goes off it is reset
class Timer
def initialize
@start_time = DateTime.now
end
def reset
@start_time = DateTime.now
end
def passed
(DateTime.now - @start_time).to_f * 24 * 60 * 60
end
end
#Operators "plus", "minus", "divided by", and "times" are chosen at random
class Problem
def initialize(max_value)
@type = rand(4)
@num_1 = rand(max_value)
@num_2 = rand(max_value)
case @type
when 0
@operator = "plus"
when 1
@operator = "minus"
when 2
@operator = "divided by"
when 3
@operator = "times"
else
abort("Illegal problem type.")
end
end
#random numbers are used with the operator to form a problem for the player to solve
def prompt
unless @type == 2
puts "What is #{@num_1} #{@operator} #{@num_2}?"
else
puts "What is #{@num_1 * (@num_2 + 1)} divided by #{@num_2 + 1}?"
end
end
def answer
case @type
when 0
return @num_1 + @num_2
when 1
return @num_1 - @num_2
when 2
return @num_1
when 3
return @num_1 * @num_2
else
abort("Illegal problem type.")
end
end
end
if __FILE__ == $0
# Set abort to true so that errors in the thread do not fail silently
Thread.abort_on_exception = true
puts "Greetings traveler, what is your name?"
player_name = gets.chomp
right_now = DateTime.now
@redos = []
this_week = right_now.strftime(fmt='%U')
player_file = "./#{player_name.downcase}-#{this_week}.txt"
#If the person exists just add their work in their file, if it's a different person make a new file
if File.exists?(player_file)
file = File.open(player_file, "a+")
else
file = File.new(player_file, "w+")
end
puts "You've practiced #{file.readlines.length} times this week."
#All players start out with 0 incorrect and correct, number limit 11, a new timer, and no interrupts until timer goes off and player hits enter.the reason the limit is 11 is because the 0 will make it so if I put 10 there would only be 9 options.
puts "How long in seconds are you going to practice math today?"
time_limit = gets.chomp.to_f
correct = 0
incorrect = 0
number_limit = 11
timer = Timer.new
interrupt = false
#until the players time is up it will use sleep so our computer does not over work and become slow
Thread.new {
while true
if timer.passed >= time_limit
interrupt = true
sleep 1
else
sleep 1
end
end
}
#Until the players time is up the player will be told how much time he/she has left. Under different circumstances different messages will be used. "You have____seconds left...___is incorect...ran out of time before you answered...you got __correct and __incorrect...Successfully completed redos...whatever else"
until interrupt
puts "\n\n\nYou have #{(time_limit - timer.passed).round} seconds left."
problem = Problem.new(number_limit)
problem.prompt
input = gets.chomp.to_i
unless interrupt
if input == problem.answer
correct += 1
puts "#{input} is correct!"
number_limit += 2
else
incorrect += 1
puts "I'm sorry, #{input} is incorrect. Don't worry, you will get a chance to try again."
@redos.push problem
end
else
puts "I'm sorry, you ran out of time before you answered. I'll just add this to the list of redos!"
@redos.push problem
end
end
puts "You got #{correct} correct, and #{incorrect} incorrect."
unless (correct + incorrect) > 0
final_grade = 0
else
final_grade = ((correct/(correct+incorrect).to_f)*100).round
end
puts "Your final grade is #{final_grade}%"
if (correct + incorrect) > 14
file.puts("#{right_now.strftime(fmt='%A %T')} In #{time_limit} seconds, #{player_name.capitalize} got #{correct} out of #{correct + incorrect}, for a grade of #{final_grade}%.")
end
file.close
puts "Now for the redos!"
while @redos.length > 0
problem = @redos.pop
problem.prompt
input = gets.chomp.to_i
if input == problem.answer
puts "#{input} is correct!"
else
puts "I'm sorry, #{input} is incorrect. Don't worry, you will get a chance to try again."
@redos.push problem
end
end
puts "Good job, you've successfully completed your #{incorrect} redos!"
end