Commit 1cc8f85

mo khan <mo@mokhan.ca>
2014-04-26 15:12:29
split out mathy to separate files.
1 parent b8a85dc
bin/mathy.rb
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+require 'mathy'
+
+console = Mathy::Console.new
+game = Mathy::Game.new(console)
+game.play(console.how_many_turns?, console.operation?)
lib/mathy/addition.rb
@@ -0,0 +1,11 @@
+module Mathy
+  class Addition
+    def initialize(verification)
+      @verifier = verification
+    end
+
+    def play_turn(x: rand(10), y: rand(20), correct_answer: x + y)
+      @verifier.check_answer("#{x} + #{y} = ", correct_answer)
+    end
+  end
+end
lib/mathy/console.rb
@@ -0,0 +1,34 @@
+module Mathy
+  class Console
+    def greet
+      clear_screen
+      prompt?('what is your name?')
+    end
+
+    def how_many_turns?
+      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
+    end
+
+    def prompt?(question)
+      puts question
+      gets.strip
+    end
+
+    private
+
+    def clear_screen
+      puts "\e[H\e[2J"
+    end
+  end
+end
lib/mathy/game.rb
@@ -0,0 +1,26 @@
+module Mathy
+  class Game
+    def initialize(console, score = 0)
+      @name = console.greet
+      @score = score
+    end
+
+    def play(games_to_play = 2, operation)
+      games_to_play.times do
+        @score += 1 if operation.play_turn(x: rand(10), y: rand(20))
+      end
+
+      display_results(@name, @score, games_to_play)
+    end
+
+    private
+
+    def display_results(name, score, games_to_play)
+      puts ""
+      puts "+++++++++++++++++++++++++++++++++++"
+      puts "You got #{score}/#{games_to_play}."
+      puts "Good bye #{name}"
+      puts "+++++++++++++++++++++++++++++++++++"
+    end
+  end
+end
lib/mathy/subtraction.rb
@@ -0,0 +1,11 @@
+module Mathy
+  class Subtraction
+    def initialize(verification)
+      @verifier = verification
+    end
+
+    def play_turn(x: rand(10), y: rand(20), correct_answer: x - y)
+      @verifier.check_answer("#{x} - #{y} = ", correct_answer)
+    end
+  end
+end
lib/mathy/verification.rb
@@ -0,0 +1,15 @@
+module Mathy
+  class Verification
+    def check_answer(question, correct_answer)
+      puts ""
+      print question
+      answer = gets
+      if answer.to_i == correct_answer
+        print "Correct!"
+        return true
+      end
+      print "The correct answer is #{correct_answer}."
+      false
+    end
+  end
+end
lib/mathy.rb
@@ -1,100 +1,9 @@
 require "mathy/version"
+require 'mathy/console'
+require 'mathy/verification'
+require 'mathy/addition'
+require 'mathy/subtraction'
+require 'mathy/game'
 
 module Mathy
-  class Console
-    def greet
-      clear_screen
-      prompt?('what is your name?')
-    end
-
-    def how_many_turns?
-      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
-    end
-
-    def prompt?(question)
-      puts question
-      gets.strip
-    end
-
-    private
-
-    def clear_screen
-      puts "\e[H\e[2J"
-    end
-  end
-
-  class Verification
-    def check_answer(question, correct_answer)
-      puts ""
-      print question
-      answer = gets
-      if answer.to_i == correct_answer
-        print "Correct!"
-        return true
-      end
-      print "The correct answer is #{correct_answer}."
-      false
-    end
-
-  end
-
-  class Addition
-    def initialize(verification)
-      @verifier = verification
-    end
-
-    def play_turn(x: rand(10), y: rand(20), correct_answer: x + y)
-      @verifier.check_answer("#{x} + #{y} = ", correct_answer)
-    end
-  end
-
-  class Subtraction
-    def initialize(verification)
-      @verifier = verification
-    end
-
-    def play_turn(x: rand(10), y: rand(20), correct_answer: x - y)
-      @verifier.check_answer("#{x} - #{y} = ", correct_answer)
-    end
-  end
-
-  class Game
-    def initialize(console, score = 0)
-      @name = console.greet
-      @score = score
-    end
-
-    def play(games_to_play = 2, operation)
-      games_to_play.times do
-        @score += 1 if operation.play_turn(x: rand(10), y: rand(20))
-      end
-
-      display_results(@name, @score, games_to_play)
-    end
-
-    private
-
-    def display_results(name, score, games_to_play)
-      puts ""
-      puts "+++++++++++++++++++++++++++++++++++"
-      puts "You got #{score}/#{games_to_play}."
-      puts "Good bye #{name}"
-      puts "+++++++++++++++++++++++++++++++++++"
-    end
-  end
-
-  console = Console.new
-  game = Game.new(console)
-  game.play(console.how_many_turns?, console.operation?)
 end
Gemfile.lock
@@ -0,0 +1,17 @@
+PATH
+  remote: .
+  specs:
+    mathy (0.0.1)
+
+GEM
+  remote: https://rubygems.org/
+  specs:
+    rake (10.1.0)
+
+PLATFORMS
+  ruby
+
+DEPENDENCIES
+  bundler (~> 1.6)
+  mathy!
+  rake