Skip to content

Instantly share code, notes, and snippets.

@geofflane
Created July 3, 2013 01:11
Show Gist options
  • Save geofflane/5914696 to your computer and use it in GitHub Desktop.
Save geofflane/5914696 to your computer and use it in GitHub Desktop.
# Life:
# Rule 1: Any live cell with fewer than 2 live neighbors dies
# Rule 2: Any live cell with more than 3 live neighbors
# Rule 3: Any live cell with two or three live neighbors lives on
# Rule 4: Any dead cell with exatctly three live cells becomes a live cell
require "rspec/given"
describe "grid" do
Given(:grid) { Grid.new(input) }
context "a dead 3 by 3 grid" do
Given(:input) { "...\n...\n..." }
When { grid.step }
Then { grid.to_s.should == input }
end
context "a 3 by 3 with only one cell" do
Given(:input) { "...\n.*.\n..." }
When { grid.step }
Then { grid.to_s.should == "...\n...\n..." }
end
context "a 3 by 3 with two live cells" do
Given {pending}
Given(:input) { "...\n***\n..." }
When { grid.step }
Then { grid.to_s.should == "...\n.*.\n..." }
end
context "a 3 by 3 with four live cells" do
Given(:input) { "...\n**.\n**." }
When { grid.step }
Then { grid.to_s.should == input }
end
end
class Grid
LIVE = '*'
DEAD = '.'
def initialize(grid)
@grid = grid.split("\n").map { |row_str| row_str.split(//) }
end
def step
end
def to_s
@grid.map { |row| row.join }.join("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment