Skip to content

Instantly share code, notes, and snippets.

@robrasmussen
Created March 8, 2011 02:46
Show Gist options
  • Save robrasmussen/859763 to your computer and use it in GitHub Desktop.
Save robrasmussen/859763 to your computer and use it in GitHub Desktop.
#! /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