Commit 3fa92b2

mo khan <mo@mokhan.ca>
2014-06-23 03:02:11
loop until the world is uninhabited.
1 parent 2886aba
lib/gol/cell.rb
@@ -9,9 +9,9 @@ class Cell
   def spawn(world)
     populated_neighbors = world.neighbors_for(self).find_all { |x| x.populated? }
     if populated?
-      Cell.new(populated: (2...4).include?(populated_neighbors.count))
+      Cell.new(populated: (2...4).include?(populated_neighbors.count), x: x, y: y)
     else
-      Cell.new(populated: populated_neighbors.count == 3)
+      Cell.new(populated: populated_neighbors.count == 3, x: x, y: y)
     end
   end
 
lib/gol/game.rb
@@ -5,9 +5,9 @@ class Game
 
   def play(max_x, max_y)
     world = World.new(create_cells(max_x, max_y))
-    10.times do
-      world.begin
-      print(world)
+    until world.empty? do
+      print(world, max_x)
+      world.next_generation
     end
   end
 
@@ -23,14 +23,14 @@ class Game
 
   private
 
-  def print(world)
+  def print(world, max_x)
     world.each_with_index do |cell, index|
-      @printer.print("|#{cell.print}|")
-      @printer.puts if index % 3 == 0
+      @printer.print("#{cell.print}")
+      @printer.puts if (index+1) % max_x == 0
     end
   end
 
   def random_population
-    rand(10).even?
+    rand(100).even?
   end
 end
lib/gol/world.rb
@@ -9,10 +9,8 @@ class World
     @cells.find_all { |x| cell.neighbor?(x) }
   end
 
-  def begin
-    each do |cell|
-      cell.spawn(self)
-    end
+  def next_generation
+    @cells = map { |cell| cell.spawn(self) }
   end
 
   def each
@@ -20,4 +18,8 @@ class World
       yield cell
     end
   end
+
+  def empty?
+    !any? { |x| x.populated? }
+  end
 end
spec/lib/world_spec.rb
@@ -5,7 +5,7 @@ describe World do
   let(:other_cell) { double }
 
   context "#neighbors_for" do
-    let(:cell) { Object.new }
+    let(:cell) { Cell.new }
 
     before :each do
       cell.stub(:neighbor?).with(neighbor).and_return(true)
@@ -21,13 +21,31 @@ describe World do
     end
   end
 
-  context "begin" do
+  context "#next_generation" do
     subject { World.new([cell]) }
     let(:cell) { double(spawn: true) }
 
     it "visits each cell" do
-      subject.begin
+      subject.next_generation
       expect(cell).to have_received(:spawn).with(subject)
     end
   end
+
+  context "#empty?" do
+    context "when there are no living cells" do
+      it "returns true" do
+        neighbor.stub(:populated?).and_return(false)
+        other_cell.stub(:populated?).and_return(false)
+        expect(subject).to be_empty
+      end
+    end
+
+    context "when there is at least one living cell" do
+      it "returns false" do
+        neighbor.stub(:populated?).and_return(false)
+        other_cell.stub(:populated?).and_return(true)
+        expect(subject).to_not be_empty
+      end
+    end
+  end
 end
spec/spec_helper.rb
@@ -17,9 +17,8 @@
 require 'gol'
 
 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.
-=begin
+  # The settings below are suggested to provide a good initial experience
+  # with RSpec, but feel free to customize to your heart's content.
   # These two settings work together to allow you to limit a spec run
   # to individual examples or groups you care about by tagging them with
   # `:focus` metadata. When nothing is tagged with `:focus`, all examples
@@ -61,7 +60,7 @@ RSpec.configure do |config|
     # Enable only the newer, non-monkey-patching expect syntax.
     # For more details, see:
     #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
-    expectations.syntax = :expect
+    expectations.syntax = [:should, :expect]
   end
 
   # rspec-mocks config goes here. You can use an alternate test double
@@ -70,11 +69,10 @@ RSpec.configure do |config|
     # Enable only the newer, non-monkey-patching expect syntax.
     # For more details, see:
     #   - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
-    mocks.syntax = :expect
+    mocks.syntax = [:should, :expect]
 
     # Prevents you from mocking or stubbing a method that does not exist on
     # a real object. This is generally recommended.
     mocks.verify_partial_doubles = true
   end
-=end
 end