summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2014-06-22 21:11:28 -0600
committermo khan <mo@mokhan.ca>2014-06-22 21:11:28 -0600
commit847b0a20a60f77f957026c512e61e2274a03e661 (patch)
tree00f8021510a8909710f53a24acaaaa88f70f3a01
parent3fa92b2bb9eb0e746da58e3e8afe16f45316338d (diff)
extract printer class.
-rw-r--r--bin/gol5
-rw-r--r--lib/gol.rb1
-rw-r--r--lib/gol/cell.rb4
-rw-r--r--lib/gol/game.rb11
-rw-r--r--lib/gol/printer.rb12
-rw-r--r--lib/gol/world.rb2
-rw-r--r--spec/lib/game_spec.rb6
-rw-r--r--spec/lib/world_spec.rb4
8 files changed, 21 insertions, 24 deletions
diff --git a/bin/gol b/bin/gol
index c920141..481f02e 100644
--- a/bin/gol
+++ b/bin/gol
@@ -2,5 +2,6 @@
$:.unshift('lib')
require 'gol'
-game = Game.new($stdout)
-game.play(3, 3)
+max_x = 3
+max_y = 3
+Game.new(Printer.new(max_x)).play(max_x, max_y)
diff --git a/lib/gol.rb b/lib/gol.rb
index c0b417b..c3ff6f9 100644
--- a/lib/gol.rb
+++ b/lib/gol.rb
@@ -1,3 +1,4 @@
require "gol/cell"
require "gol/game"
require "gol/world"
+require "gol/printer"
diff --git a/lib/gol/cell.rb b/lib/gol/cell.rb
index dd1717b..b3070ca 100644
--- a/lib/gol/cell.rb
+++ b/lib/gol/cell.rb
@@ -24,10 +24,6 @@ class Cell
false
end
- def print
- populated? ? "X" : " "
- end
-
def populated?
@populated
end
diff --git a/lib/gol/game.rb b/lib/gol/game.rb
index 197750a..ea33a28 100644
--- a/lib/gol/game.rb
+++ b/lib/gol/game.rb
@@ -6,8 +6,8 @@ class Game
def play(max_x, max_y)
world = World.new(create_cells(max_x, max_y))
until world.empty? do
- print(world, max_x)
- world.next_generation
+ @printer.print(world)
+ world.next_generation!
end
end
@@ -23,13 +23,6 @@ class Game
private
- def print(world, max_x)
- world.each_with_index do |cell, index|
- @printer.print("#{cell.print}")
- @printer.puts if (index+1) % max_x == 0
- end
- end
-
def random_population
rand(100).even?
end
diff --git a/lib/gol/printer.rb b/lib/gol/printer.rb
new file mode 100644
index 0000000..d3fa3b5
--- /dev/null
+++ b/lib/gol/printer.rb
@@ -0,0 +1,12 @@
+class Printer
+ def initialize(max_x)
+ @max_x = max_x
+ end
+
+ def print(world)
+ world.each_with_index do |cell, index|
+ print cell.populated? ? "X" : " "
+ puts if (index+1) % @max_x == 0
+ end
+ end
+end
diff --git a/lib/gol/world.rb b/lib/gol/world.rb
index df14ffe..1eef21c 100644
--- a/lib/gol/world.rb
+++ b/lib/gol/world.rb
@@ -9,7 +9,7 @@ class World
@cells.find_all { |x| cell.neighbor?(x) }
end
- def next_generation
+ def next_generation!
@cells = map { |cell| cell.spawn(self) }
end
diff --git a/spec/lib/game_spec.rb b/spec/lib/game_spec.rb
index 1e6324f..aa67956 100644
--- a/spec/lib/game_spec.rb
+++ b/spec/lib/game_spec.rb
@@ -2,12 +2,6 @@ describe Game do
subject { Game.new(printer) }
let(:printer) { double }
- context "#start" do
- it "prints the output of the world" do
- expect(subject.play(3, 3)).to eql(new_world)
- end
- end
-
context "#create_cells" do
context "for a 2x2 grid" do
let(:results) { subject.create_cells(2, 2) }
diff --git a/spec/lib/world_spec.rb b/spec/lib/world_spec.rb
index 51a41d3..f6556ed 100644
--- a/spec/lib/world_spec.rb
+++ b/spec/lib/world_spec.rb
@@ -21,12 +21,12 @@ describe World do
end
end
- context "#next_generation" do
+ context "#next_generation!" do
subject { World.new([cell]) }
let(:cell) { double(spawn: true) }
it "visits each cell" do
- subject.next_generation
+ subject.next_generation!
expect(cell).to have_received(:spawn).with(subject)
end
end