Created
June 6, 2013 18:07
-
-
Save marksim/5723610 to your computer and use it in GitHub Desktop.
A quick pairing exercise with @mattr- to learn a bit more about how to approach tests.
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
require 'rspec' | |
class Game | |
attr_reader :alive_cells | |
def initialize(living_cells) | |
@living_cells = {} | |
living_cells.each do |coords| | |
@living_cells[coords] = Cell.new(true) | |
end | |
end | |
def next_generation | |
end | |
def number_of_neighbors_for(x, y) | |
end | |
def alive_cells | |
@living_cells.keys | |
end | |
end | |
class Cell | |
attr_accessor :number_of_neighbors | |
def initialize(living) | |
@alive = living | |
end | |
def evolve | |
@alive = false if number_of_neighbors < 2 || number_of_neighbors > 3 | |
@alive = true if number_of_neighbors == 3 | |
end | |
def alive? | |
@alive | |
end | |
end | |
describe Game do | |
let(:block_cells) { [[0, 0], [0, 1], [1, 0], [1, 1]] } | |
let(:blinker_cells_a) { [[0, -1], [0, 0], [0, 1]] } | |
let(:blinker_cells_b) { [[-1, 0], [0, 0], [1, 0]] } | |
it "stays the same for the block game" do | |
game = Game.new(block_cells) | |
game.next_generation | |
game.alive_cells.should == block_cells | |
end | |
it "knows how many living neighbors a given cell has" do | |
game = Game.new(blinker_cells_a) | |
game.number_of_neighbors_for(0, 0).should == 2 | |
end | |
#it "occilates for the blinker game" do | |
#game = Game.new(blinker_cells_a) | |
#game.next_generation | |
#game.alive_cells.should == blinker_cells_b | |
#end | |
describe "live cells" do | |
let(:cell) { Cell.new(true) } | |
it "dies if fewer than 2 neighbors" do | |
cell.number_of_neighbors = 1 | |
cell.evolve | |
cell.should_not be_alive | |
end | |
it "lives with two neighbors" do | |
cell.number_of_neighbors = 2 | |
cell.evolve | |
cell.should be_alive | |
end | |
it "lives with three neighbors" do | |
cell.number_of_neighbors = 3 | |
cell.evolve | |
cell.should be_alive | |
end | |
it "dies with more than three neighbors" do | |
cell.number_of_neighbors = 4 | |
cell.evolve | |
cell.should_not be_alive | |
end | |
end | |
describe "dead cells" do | |
let(:cell) { Cell.new(false) } | |
it "becomes alive if exactly three neighbors" do | |
cell.number_of_neighbors = 3 | |
cell.evolve | |
cell.should be_alive | |
end | |
it "stays dead without exactly three neighbors" do | |
cell.number_of_neighbors = 2 | |
cell.evolve | |
cell.should_not be_alive | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment