Created
November 15, 2014 20:44
-
-
Save nwjsmith/de81f7de8f4560fff26a to your computer and use it in GitHub Desktop.
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
def evolve_cell(cell, neighbours) | |
if cell == 1 | |
if neighbours.inject(&:+) == 2 || neighbours.inject(&:+) == 3 | |
1 | |
else | |
0 | |
end | |
else | |
if neighbours.inject(&:+) == 3 | |
1 | |
else | |
0 | |
end | |
end | |
end | |
def neighbours(grid, width, height, index) | |
[ | |
# north | |
index - width, | |
# northeast | |
# east | |
# southeast | |
# south | |
# southwest | |
# west | |
# northwest | |
# north | |
] | |
end | |
def evolve_grid(grid, width, height) | |
[ | |
0, 0, 0, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 0, 0, 0 | |
] | |
end | |
describe '#evolve_grid' do | |
it 'evolves oscillators' do | |
expect(evolve_grid([ | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, | |
0, 1, 1, 1, 0, | |
0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0 | |
], 5, 5)).to eq([ | |
0, 0, 0, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 1, 0, 0, | |
0, 0, 0, 0, 0 | |
]) | |
end | |
xit 'evolves still lifes' do | |
expect(evolve_grid([ | |
0, 0, 0, 0, | |
0, 1, 1, 0, | |
0, 1, 1, 0, | |
0, 0, 0, 0 | |
], 4, 4)).to eq([ | |
0, 0, 0, 0, | |
0, 1, 1, 0, | |
0, 1, 1, 0, | |
0, 0, 0, 0 | |
]) | |
end | |
end | |
describe '#evolve_cell' do | |
context 'when the cell is live' do | |
it 'dies with less than two live nieghbours' do | |
expect(evolve_cell(1, [1, 0, 0])).to eq 0 | |
expect(evolve_cell(1, [0, 0, 0])).to eq 0 | |
end | |
it 'lives with two live neighbours' do | |
expect(evolve_cell(1, [1, 1, 0])).to eq 1 | |
end | |
it 'lives with three live neighbours' do | |
expect(evolve_cell(1, [1, 1, 1])).to eq 1 | |
end | |
it 'dies with more than three live neighbours' do | |
expect(evolve_cell(1, [1, 1, 1, 1])).to eq 0 | |
end | |
end | |
context 'when the cell is dead' do | |
it 'becomes live with exactly three live neighbours' do | |
expect(evolve_cell(0, [1, 1, 1])).to eq 1 | |
end | |
it 'stays dead without exactly three live neighbours' do | |
expect(evolve_cell(0, [1, 1, 0])).to eq 0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment