Created
January 31, 2012 06:36
-
-
Save abyx/1709252 to your computer and use it in GitHub Desktop.
Final exercise at Corey Haines' Improving Your TDD workshop
This file contains 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
class Board | |
def initialize | |
@living = [] | |
end | |
def evolve_dead_cells | |
end | |
def add_living_cell(location) | |
@living << location | |
end | |
def evolve_living_cells | |
@living = @living.select {|location| location.neighbor_count == 2} | |
end | |
def alive_at?(location) | |
location.neighbor_count == 3 || @living.include?(location) | |
end | |
end | |
class Location | |
def update_living_cells(living_cells) | |
end | |
def neighbor_count | |
return 0 | |
end | |
end | |
describe "Location" do | |
describe "neighbor_count" do | |
it "should be zero when there no living cells to choose from" do | |
location = Location.new | |
location.update_living_cells [] | |
location.neighbor_count.should == 0 | |
end | |
end | |
end | |
describe "Board" do | |
describe "evolve_dead_cells" do | |
it "does not create in a location that doesnt have neighbors" do | |
location_with_0_neighbors = stub :neighbor_count => 0 | |
board = Board.new | |
board.evolve_dead_cells | |
board.alive_at?(location_with_0_neighbors).should be_false | |
end | |
it "creates in a location that has 3 living neighbors" do | |
location_with_3_neighbors = stub :neighbor_count => 3 | |
board = Board.new | |
board.evolve_dead_cells | |
board.alive_at?(location_with_3_neighbors).should be_true | |
end | |
end | |
describe "evolve_living_cells" do | |
it "kills a cell with 0 neighbors" do | |
location_with_0_neighbors = stub :neighbor_count => 0 | |
board = Board.new | |
board.add_living_cell location_with_0_neighbors | |
board.evolve_living_cells | |
board.alive_at?(location_with_0_neighbors).should be_false | |
end | |
it "keeps a cell with 2 neighbors alive" do | |
location_with_2_neighbors = stub :neighbor_count => 2 | |
board = Board.new | |
board.add_living_cell location_with_2_neighbors | |
board.evolve_living_cells | |
board.alive_at?(location_with_2_neighbors).should be_true | |
end | |
it "kills a cell with 1 neighbor and keeps one with 2 alive" do | |
location_with_2_neighbors = stub :neighbor_count => 2 | |
location_with_1_neighbor = stub :neighbor_count => 1 | |
board = Board.new | |
board.add_living_cell location_with_2_neighbors | |
board.add_living_cell location_with_1_neighbor | |
board.evolve_living_cells | |
board.alive_at?(location_with_2_neighbors).should be_true | |
board.alive_at?(location_with_1_neighbor).should be_false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment