Commit e0bd8ed
Changed files (7)
lib
spec
lib/gol/cell.rb
@@ -1,37 +1,37 @@
class Cell
attr_reader :x, :y
- def initialize(populated: false, x: 1, y: 1)
- @populated = populated
+ def initialize(alive: false, x: 1, y: 1)
+ @alive = alive
@x, @y = x, y
end
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), x: x, y: y)
+ living_neighbors = world.neighbors_for(self).find_all { |x| x.alive? }
+ if alive?
+ Cell.new(alive: (2...4).include?(living_neighbors.count), x: x, y: y)
else
- Cell.new(populated: populated_neighbors.count == 3, x: x, y: y)
+ Cell.new(alive: living_neighbors.count == 3, x: x, y: y)
end
end
def neighbor?(other_cell)
- return true if matches_x?(other_cell) && one_cell_away(other_cell.y - self.y)
- return true if matches_y?(other_cell) && one_cell_away(other_cell.x - self.x)
+ return true if x?(other_cell) && one_cell_away(other_cell.y - self.y)
+ return true if y?(other_cell) && one_cell_away(other_cell.x - self.x)
false
end
- def populated?
- @populated
+ def alive?
+ @alive
end
private
- def matches_x?(other_cell)
+ def x?(other_cell)
other_cell.x == x
end
- def matches_y?(other_cell)
+ def y?(other_cell)
other_cell.y == y
end
lib/gol/game.rb
@@ -15,7 +15,7 @@ class Game
items = []
(1..max_x).each do |x|
(1..max_y).each do |y|
- items.push(Cell.new(x: x, y: y, populated: random_population))
+ items.push(Cell.new(x: x, y: y, alive: random_life))
end
end
items
@@ -23,7 +23,7 @@ class Game
private
- def random_population
+ def random_life
rand(100).even?
end
end
lib/gol/printer.rb
@@ -5,7 +5,7 @@ class Printer
def display(world)
world.each_with_index do |cell, index|
- print cell.populated? ? "X" : " "
+ print cell.alive? ? "X" : " "
puts if (index+1) % @max_x == 0
end
end
lib/gol/world.rb
@@ -20,6 +20,6 @@ class World
end
def empty?
- !any? { |x| x.populated? }
+ !any? { |x| x.alive? }
end
end
spec/lib/cell_spec.rb
@@ -1,61 +1,61 @@
describe Cell do
- let(:populated_neighbor) { Cell.new(populated: true) }
- let(:unpopulated_neighbor) { Cell.new(populated: false) }
+ let(:living_neighbor) { Cell.new(alive: true) }
+ let(:dead_neighbor) { Cell.new(alive: false) }
let(:world) { double }
- context "when populated" do
- subject { Cell.new(populated: true) }
+ context "when alive" do
+ subject { Cell.new(alive: true) }
- it "is populated at creation" do
- expect(subject.populated?).to be_truthy
+ it "is alive at creation" do
+ expect(subject.alive?).to be_truthy
end
- context "when it has a single populated neighbor" do
- let(:neighbors) { [populated_neighbor, unpopulated_neighbor] }
+ context "when it has a single living neighbor" do
+ let(:neighbors) { [living_neighbor, dead_neighbor] }
it "dies of isolation" do
world.stub(:neighbors_for).with(subject).and_return(neighbors)
- expect(subject.spawn(world)).to_not be_populated
+ expect(subject.spawn(world)).to_not be_alive
end
end
- context 'when it has two populated neighbors' do
- let(:neighbors) { [populated_neighbor] * 2 }
+ context 'when it has two living neighbors' do
+ let(:neighbors) { [living_neighbor] * 2 }
- it "remains populated" do
+ it "remains living" do
world.stub(:neighbors_for).with(subject).and_return(neighbors)
- expect(subject.spawn(world)).to be_populated
+ expect(subject.spawn(world)).to be_alive
end
end
- context 'when it has three populated neighbors' do
- let(:neighbors) { [populated_neighbor] * 3 }
+ context 'when it has three living neighbors' do
+ let(:neighbors) { [living_neighbor] * 3 }
- it "remains populated" do
+ it "remains living" do
world.stub(:neighbors_for).with(subject).and_return(neighbors)
- expect(subject.spawn(world)).to be_populated
+ expect(subject.spawn(world)).to be_alive
end
end
context 'when it has more than three neighbors' do
- let(:neighbors) { [populated_neighbor] * 4 }
+ let(:neighbors) { [living_neighbor] * 4 }
- it "becomes unpopulated" do
+ it "dies" do
world.stub(:neighbors_for).with(subject).and_return(neighbors)
- expect(subject.spawn(world)).to_not be_populated
+ expect(subject.spawn(world)).to_not be_alive
end
end
end
- context "when unpopulated" do
- subject { Cell.new(populated: false) }
- let(:populated_neighbors) { [populated_neighbor] * 2 }
- let(:neighbors) { populated_neighbors + [unpopulated_neighbor] }
+ context "when dead" do
+ subject { Cell.new(alive: false) }
+ let(:living_neighbors) { [living_neighbor] * 2 }
+ let(:neighbors) { living_neighbors + [dead_neighbor] }
- context "when it has two populated neighbors" do
- it "remains unpopulated" do
+ context "when it has two living neighbors" do
+ it "remains dead" do
world.stub(:neighbors_for).with(subject).and_return(neighbors)
- expect(subject.spawn(world)).to_not be_populated
+ expect(subject.spawn(world)).to_not be_alive
end
end
end
spec/lib/world_spec.rb
@@ -34,16 +34,16 @@ describe World do
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)
+ neighbor.stub(:alive?).and_return(false)
+ other_cell.stub(:alive?).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)
+ neighbor.stub(:alive?).and_return(false)
+ other_cell.stub(:alive?).and_return(true)
expect(subject).to_not be_empty
end
end
spec/spec_helper.rb
@@ -39,7 +39,7 @@ RSpec.configure do |config|
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
- config.profile_examples = 10
+ #config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing