Commit e0f6c09

mo khan <mo@mokhan.ca>
2014-04-26 18:38:47
extract player class.
1 parent 137d84c
bin/mathy.rb
@@ -1,13 +1,17 @@
 #!/usr/bin/env ruby
 require 'mathy'
 
-verifier = Mathy::Verification.new
-operations = [
-  Mathy::Operations::Addition.new(verifier),
-  Mathy::Operations::Subtraction.new(verifier)
-]
+module Mathy
+  verifier = Mathy::Verification.new
+  operations = [
+    Mathy::Operations::Addition.new(verifier),
+    Mathy::Operations::Subtraction.new(verifier)
+  ]
 
-console = Mathy::Console.new
-game = Mathy::Game.new(console)
-difficulty = Mathy::Difficulties::GradeTwo.new
-game.play(console.how_many_turns?, console.operation?(operations), difficulty)
+  console = Mathy::Console.new
+
+  player = Player.new(console.greet)
+  game = Mathy::Game.new(player)
+  difficulty = Mathy::Difficulties::GradeTwo.new
+  game.play(console.how_many_turns?, console.operation?(operations), difficulty)
+end
lib/mathy/operations/addition.rb
@@ -6,7 +6,7 @@ module Mathy
       end
 
       def calculate(operands)
-        operands.inject(0) do |result, x|
+        operands.inject do |result, x|
           result + x
         end
       end
lib/mathy/operations/subtraction.rb
@@ -6,8 +6,8 @@ module Mathy
       end
 
       def calculate(operands)
-        operands.inject(0) do |result, x|
-          result + x
+        operands.inject do |result, x|
+          result - x
         end
       end
     end
lib/mathy/game.rb
@@ -1,7 +1,7 @@
 module Mathy
   class Game
-    def initialize(console, score = 0)
-      @name = console.greet
+    def initialize(player, score = 0)
+      @player = player
       @score = score
     end
 
@@ -10,16 +10,16 @@ module Mathy
         @score += 1 if operation.play_turn(difficulty.next_operands)
       end
 
-      display_results(@name, @score, games_to_play)
+      display_results(@score, games_to_play)
     end
 
     private
 
-    def display_results(name, score, games_to_play)
+    def display_results(score, games_to_play)
       puts ""
       puts "+++++++++++++++++++++++++++++++++++"
       puts "You got #{score}/#{games_to_play}."
-      puts "Good bye #{name}"
+      puts "Good bye #{@player.name}"
       puts "+++++++++++++++++++++++++++++++++++"
     end
   end
lib/mathy/player.rb
@@ -0,0 +1,9 @@
+module Mathy
+  class Player
+    attr_reader :name
+
+    def initialize(name)
+      @name = name
+    end
+  end
+end
lib/mathy.rb
@@ -1,6 +1,7 @@
 require "mathy/version"
 require 'mathy/console'
 require 'mathy/verification'
+require 'mathy/player'
 require 'mathy/game'
 require 'mathy/operations/operation'
 require 'mathy/operations/addition'