Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Pilot screencast: 000: Conway's Game of Life

  • Loading branch information...
commit 335811a9c2b280aa025426f46aab88bcb56b36bb 0 parents
@radar authored
Showing with 125 additions and 0 deletions.
  1. +125 −0 000-game-of-life/gol_spec.rb
125 000-game-of-life/gol_spec.rb
@@ -0,0 +1,125 @@
+require 'rspec'
+
+class Cell
+ attr_accessor :world, :x, :y
+
+ def initialize(world, x=0, y=0)
+ @world = world
+ @x = x
+ @y = y
+ world.cells << self
+ end
+
+ def die!
+ world.cells -= [self]
+ end
+
+ def dead?
+ !world.cells.include?(self)
+ end
+
+ def alive?
+ world.cells.include?(self)
+ end
+
+ def neighbours
+ @neighbours = []
+ world.cells.each do |cell|
+ # Has a cell to the north
+ if self.x == cell.x && self.y == cell.y - 1
+ @neighbours << cell
+ end
+
+ # Has a cell to the north east
+ if self.x == cell.x - 1 && self.y == cell.y - 1
+ @neighbours << cell
+ end
+
+ # Has a cell to the west
+ if self.x == cell.x + 1 && self.y == cell.y
+ @neighbours << cell
+ end
+
+ if self.x == cell.x - 1 && self.y == cell.y
+ @neighbours << cell
+ end
+ end
+
+ @neighbours
+ end
+
+ def spawn_at(x, y)
+ Cell.new(world, x, y)
+ end
+end
+
+class World
+ attr_accessor :cells
+
+ def initialize
+ @cells = []
+ end
+
+ def tick!
+ cells.each do |cell|
+ if cell.neighbours.count < 2
+ cell.die!
+ end
+ end
+ end
+end
+
+
+describe 'game of life' do
+ let(:world) { World.new }
+ context "cell utility methods" do
+ subject { Cell.new(world) }
+ it "spawn relative to" do
+ cell = subject.spawn_at(3,5)
+ cell.is_a?(Cell).should be_true
+ cell.x.should == 3
+ cell.y.should == 5
+ cell.world.should == subject.world
+ end
+
+ it "detects a neighbour to the north" do
+ cell = subject.spawn_at(0, 1)
+ subject.neighbours.count.should == 1
+ end
+
+ it "detects a neighbour to the north east" do
+ cell = subject.spawn_at(1, 1)
+ subject.neighbours.count.should == 1
+ end
+
+ it "detects a neighbour to the left" do
+ cell = subject.spawn_at(-1, 0)
+ subject.neighbours.count.should == 1
+ end
+
+ it "detects a neighbour to the right" do
+ cell = subject.spawn_at(1, 0)
+ subject.neighbours.count.should == 1
+ end
+
+ it "dies" do
+ subject.die!
+ subject.world.cells.should_not include(subject)
+ end
+ end
+
+ it "Rule #1: Any live cell with fewer than two live neighbours dies, as if caused by under-population." do
+ cell = Cell.new(world)
+ new_cell = cell.spawn_at(2,0)
+ world.tick!
+ cell.should be_dead
+ end
+
+ it "Rule #2: Any live cell with two or three live neighbours lives on to the next generation." do
+ cell = Cell.new(world)
+ new_cell = cell.spawn_at(1,0)
+ other_new_cell = cell.spawn_at(-1, 0)
+ world.tick!
+ cell.should be_alive
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.