Commit 287f826

mo khan <mo@mokhan.ca>
2014-06-23 01:55:54
add bin to run gol.
1 parent d68d82e
bin/gof
@@ -1,3 +1,6 @@
 #!/bin/env ruby
+$:.unshift('lib')
+require 'gof'
 
-game = Game.new
+game = Game.new($stdout)
+game.play(3, 3)
lib/cell.rb → lib/gof/cell.rb
@@ -24,6 +24,10 @@ class Cell
     false
   end
 
+  def print
+    populated? ? "X" : " "
+  end
+
   def populated?
     @populated
   end
lib/gof/game.rb
@@ -0,0 +1,36 @@
+class Game
+  def initialize(printer)
+    @printer = printer
+  end
+
+  def play(max_x, max_y)
+    world = World.new(create_cells(max_x, max_y))
+    10.times do
+      world.begin
+      print(world)
+    end
+  end
+
+  def create_cells(max_x, max_y)
+    items = []
+    (1..max_x).each do |x|
+      (1..max_y).each do |y|
+        items.push(Cell.new(x: x, y: y, populated: random_population))
+      end
+    end
+    items
+  end
+
+  private
+
+  def print(world)
+    world.each_with_index do |cell, index|
+      @printer.print("|#{cell.print}|")
+      @printer.puts if index % 3 == 0
+    end
+  end
+
+  def random_population
+    rand(10).even?
+  end
+end
lib/world.rb → lib/gof/world.rb
@@ -1,4 +1,6 @@
 class World
+  include Enumerable
+
   def initialize(cells)
     @cells = cells
   end
@@ -8,8 +10,14 @@ class World
   end
 
   def begin
-    @cells.each do |cell|
+    each do |cell|
       cell.spawn(self)
     end
   end
+
+  def each
+    @cells.each do |cell|
+      yield cell
+    end
+  end
 end
lib/game.rb
@@ -1,18 +0,0 @@
-class Game
-  def initialize(printer)
-  end
-
-  def play(max_x, max_y)
-    World.new
-  end
-
-  def create_cells(max_x, max_y)
-    items = []
-    (1..max_x).each do |x|
-      (1..max_y).each do |y|
-        items.push(Cell.new(x: x, y: y))
-      end
-    end
-    items
-  end
-end
lib/gof.rb
@@ -0,0 +1,3 @@
+require "gof/cell"
+require "gof/game"
+require "gof/world"
spec/lib/game_spec.rb
@@ -2,10 +2,9 @@ describe Game do
   subject { Game.new(printer) }
   let(:printer) { double }
 
-  xcontext "#start" do
+  context "#start" do
     it "prints the output of the world" do
-      subject.play(3, 3)
-      expect(world).to have_received(:print_each_using).with(printer)
+      expect(subject.play(3, 3)).to eql(new_world)
     end
   end
 
spec/spec_helper.rb
@@ -14,9 +14,8 @@
 # users commonly want.
 #
 # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
-require "world"
-require "cell"
-require 'game'
+require 'gof'
+
 RSpec.configure do |config|
 # The settings below are suggested to provide a good initial experience
 # with RSpec, but feel free to customize to your heart's content.