Commit 861d75e

mo khan <mo@mokhan.ca>
2014-04-26 19:13:13
inject each operation with the difficulty.
1 parent 5ae8856
Changed files (5)
lib/mathy/difficulties/grade_two.rb
@@ -8,8 +8,8 @@ module Mathy
       def choose_operation(console)
         verifier = Verification.new
         console.operation?([
-          Operations::Addition.new(verifier),
-          Operations::Subtraction.new(verifier)
+          Operations::Addition.new(verifier, self),
+          Operations::Subtraction.new(verifier, self)
         ])
       end
     end
lib/mathy/operations/addition.rb
@@ -1,8 +1,8 @@
 module Mathy
   module Operations
     class Addition < Operation
-      def initialize(verification)
-        super(verification, "+")
+      def initialize(verification, difficulty)
+        super(verification, difficulty, "+")
       end
 
       def calculate(operands)
lib/mathy/operations/operation.rb
@@ -3,12 +3,14 @@ module Mathy
     class Operation
       attr_reader :key
 
-      def initialize(verification, key)
+      def initialize(verification, difficulty, key)
         @verifier = verification
+        @difficulty = difficulty
         @key = key
       end
 
-      def play_turn(operands)
+      def play_turn
+        operands = @difficulty.next_operands
         @verifier.check_answer("#{operands.join(" #{key} ")} = ", calculate(operands))
       end
 
lib/mathy/operations/subtraction.rb
@@ -1,8 +1,8 @@
 module Mathy
   module Operations
     class Subtraction < Operation
-      def initialize(verification)
-        super(verification, "-")
+      def initialize(verification, difficulty)
+        super(verification, difficulty, "-")
       end
 
       def calculate(operands)
lib/mathy/game.rb
@@ -10,7 +10,7 @@ module Mathy
       difficulty = console.difficulty?
       operation = difficulty.choose_operation(console)
       games_to_play.times do
-        @score += 1 if operation.play_turn(difficulty.next_operands)
+        @score += 1 if operation.play_turn
       end
 
       display_results(@score, games_to_play)