Commit ce22aa3
Changed files (4)
bin
lib
bin/mathy.rb
@@ -1,6 +1,12 @@
#!/usr/bin/env ruby
require 'mathy'
+verifier = Mathy::Verification.new
+operations = [
+ Mathy::Addition.new(verifier),
+ Mathy::Subtraction.new(verifier)
+]
+
console = Mathy::Console.new
game = Mathy::Game.new(console)
-game.play(console.how_many_turns?, console.operation?)
+game.play(console.how_many_turns?, console.operation?(operations))
lib/mathy/addition.rb
@@ -1,11 +1,18 @@
module Mathy
class Addition
+ attr_reader :key
+
def initialize(verification)
@verifier = verification
+ @key = "+"
end
def play_turn(x: rand(10), y: rand(20), correct_answer: x + y)
@verifier.check_answer("#{x} + #{y} = ", correct_answer)
end
+
+ def matches?(other_key)
+ key == other_key
+ end
end
end
lib/mathy/console.rb
@@ -9,15 +9,9 @@ module Mathy
prompt?("how many questions do you want?").to_i
end
- def operation?(verifier = Verification.new)
- case prompt?("choose operation? [+-]")
- when "+"
- Addition.new(verifier)
- when "-"
- Subtraction.new(verifier)
- else
- Addition.new(verifier)
- end
+ def operation?(operations)
+ selection = prompt?("choose operation? #{operations.map(&:key).flatten}")
+ operations.find { |operation| operation.matches?(selection) }
end
def prompt?(question)
lib/mathy/subtraction.rb
@@ -1,11 +1,18 @@
module Mathy
class Subtraction
+ attr_reader :key
+
def initialize(verification)
@verifier = verification
+ @key = "-"
end
def play_turn(x: rand(10), y: rand(20), correct_answer: x - y)
@verifier.check_answer("#{x} - #{y} = ", correct_answer)
end
+
+ def matches?(other_key)
+ key == other_key
+ end
end
end