Created
February 14, 2014 21:46
-
-
Save benneuman/9010012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
#Need something like spots/adjacency | |
class Board | |
attr_reader :cell_count | |
def initialize | |
@cells = [] | |
end | |
def add_cell(cell = Cell.new) | |
@cells << cell | |
end | |
def evolve | |
@cells.each(&:evolve) | |
end | |
def cell_count | |
@cells.count { |cell| cell.alive? } | |
end | |
end | |
class Cell | |
attr_reader :neighbors | |
attr_accessor :alive | |
def initialize(neighbors = 0, alive = true) | |
@neighbors = neighbors | |
@alive = alive | |
end | |
def alive? | |
@alive | |
end | |
def evolve | |
@alive = (alive?) ? staying_alive? : coming_alive? | |
end | |
def staying_alive? | |
@neighbors == 2 || @neighbors == 3 | |
end | |
def coming_alive? | |
@neighbors == 3 | |
end | |
end | |
describe Board do | |
context "at creation" do | |
it "has no cells" do | |
board = Board.new | |
board.cell_count.should == 0 | |
end | |
it "has one cell after adding a cell" do | |
board = Board.new | |
board.add_cell | |
board.cell_count.should == 1 | |
end | |
end | |
context "with one cell" do | |
context "with no neighbors" | |
it "has no cells after evolution" do | |
board = Board.new | |
board.add_cell | |
board.evolve | |
board.cell_count.should == 0 | |
end | |
context "with one neighbor" do | |
it "has no cells after evolution" do | |
board = Board.new | |
board.add_cell(Cell.new(1)) | |
board.evolve | |
board.cell_count.should == 0 | |
end | |
end | |
context "with two neighbors" do | |
it "still has that cell after evolution" do | |
board = Board.new | |
board.add_cell(Cell.new(2)) | |
board.evolve | |
board.cell_count.should == 1 | |
end | |
end | |
context "with three neighbor" do | |
it "still has that cell after evolution" do | |
board = Board.new | |
board.add_cell(Cell.new(3)) | |
board.evolve | |
board.cell_count.should == 1 | |
end | |
end | |
end | |
context "with a dead spot" do | |
context "with exactly three neighbors" do | |
it "spot is not alive before evolution" do | |
board = Board.new | |
dead_cell = Cell.new(neighbors = 3, alive = false) | |
board.add_cell(dead_cell) | |
board.cell_count.should == 0 | |
end | |
it "has a new cell after evolution" do | |
board = Board.new | |
dead_cell = Cell.new(neighbors = 3, alive = false) | |
board.add_cell(dead_cell) | |
board.evolve | |
board.cell_count.should == 1 | |
end | |
end | |
context "with exactly two neighbors" do | |
it "should not be alive after evolution" do | |
board = Board.new | |
dead_cell = Cell.new(neighbors = 2, alive = false) | |
board.add_cell(dead_cell) | |
board.evolve | |
board.cell_count.should == 0 | |
end | |
end | |
end | |
end | |
# class Board | |
# attr_reader :cell_count | |
# def initialize | |
# @cell_count = 1 | |
# end | |
# def evolve | |
# @cell_count -= 1 | |
# end | |
# end | |
# describe Board do | |
# context "starting with one cell" do | |
# it "has one cell at creation" do | |
# board = Board.new | |
# board.cell_count.should == 1 | |
# end | |
# it "has zero cells after evolution" do | |
# board = Board.new | |
# board.evolve | |
# board.cell_count.should == 0 | |
# end | |
# end | |
# context "starting with a cell with two neighbors" do | |
# it "cell has two neighbors at creation" do | |
# board = Board.new | |
# cell = Cell.new(neighbors = 2) | |
# board.add_cell(cell) | |
# board.evolve | |
# board.cell_count.should == 0 | |
# end | |
# end | |
# end | |
# def evolve(board) | |
# board.map do |point| | |
# if point.neighbors 993q5grq80 | |
# if board == ["X"] | |
# ["O"] | |
# else | |
# [%w(X X), %(X)] | |
# end | |
# end | |
# describe "Board" do | |
# context "beginning with one live cell" do | |
# it "has one dead cell after evolution" do | |
# board = ["X"] | |
# evolve(board).should == ["O"] | |
# end | |
# end | |
# context "beginning with three live cells each with two neighbors" do | |
# it "has three live cells after evolution" do | |
# board = [%w(X X), %(X)] | |
# evolve(board).should == [%w(X X), %(X)] | |
# end | |
# context "with a different shape" do | |
# it "still has three live cells after evolution" do | |
# board = [%w(X), %w(X X)] | |
# evolve(board).should = [%w(X), %w(X X)] | |
# end | |
# end | |
# end | |
# end | |
# # # class Spot | |
# # # attr_accessor :neighbors | |
# # # def evolve | |
# # end | |
# # def has_cell? | |
# # true | |
# # end | |
# # end | |
# # describe "Cell" do | |
# # context "with two neighbors" do | |
# # it "is still alive after evolution" do | |
# # cell = Cell.new | |
# # cell.neighbors = 2 | |
# # cell.evolve | |
# # cell.should be_alive | |
# # end | |
# # end | |
# # context "with three neighbors" do | |
# # it "is still live after evolution" do | |
# # cell = Cell.new | |
# # cell.neighbors = 3 | |
# # cell.evolve | |
# # cell.should be_alive | |
# # end | |
# end | |
# end | |
# describe "New board" do | |
# context "beginning with one live cell" do | |
# it "has one live cell at creation" do | |
# world = World.new | |
# world.cell_count.should == 1 | |
# end | |
# it "has no live cells after evolution" do | |
# world = World.new | |
# world.evolve | |
# world.cell_count.should == 0 | |
# end | |
# end | |
# end | |
# describe "Cell" do | |
# it "has two neighbors at creation" do | |
# cell = Cell.new | |
# cell.neighbors.should == 2 | |
# end | |
# it "has three neighbors at creation" do | |
# cell = Cell.new | |
# cell.neighbor.should == 3 | |
# end | |
# end | |
# describe "Dead cell with no neighbors" do | |
# it "dies" do | |
# cell = Cell.new | |
# cell.evolve | |
# cell.should be_dead | |
# end | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment