Created
March 8, 2011 02:46
-
-
Save robrasmussen/859763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
class AdditionProblemGenerator | |
attr_accessor :max, :number_of_problems | |
def initialize(max = 20, number_of_problems = 20) | |
self.max = max | |
self.number_of_problems = number_of_problems | |
end | |
def play | |
number_of_problems.times do | |
first, second = create_problem | |
puts "#{first} + #{second} = ?" | |
guess = gets | |
unless guess.to_i == first + second | |
puts "Try again" | |
guess = gets | |
else | |
puts "Great job!" | |
end | |
end | |
puts "That's it! You answered #{number_of_problems} problems!" | |
end | |
private | |
def create_problem | |
sum = max + 1 | |
first = 0 | |
second = 0 | |
# Because "adding 0 or 1 is for babies" | |
while sum > max || first < 2 || second < 2 | |
first = rand(max) | |
second = rand(max) | |
sum = first + second | |
end | |
[first, second] | |
end | |
end | |
if __FILE__ == $0 | |
generator = AdditionProblemGenerator.new(20, 10) | |
generator.play | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment