Created
November 16, 2014 09:52
-
-
Save KyleMit/7af990b2fbd6adbf1f73 to your computer and use it in GitHub Desktop.
Conway's Game of Life
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
module Life | |
def self.next_grid live_cells_in_grid | |
new_grid = Set.new | |
potential_cells(live_cells_in_grid).each do |cell| | |
alive = live_cells_in_grid.include?(cell) | |
neighbors = live_neighbors_count(live_cells_in_grid, cell) | |
if next_state_alive(alive, neighbors) | |
new_grid << cell | |
end | |
end | |
new_grid | |
end | |
def self.potential_cells live_cells_in_grid | |
border_cells = [] | |
live_cells_in_grid.each do |live_cell| | |
border_cells.concat border_cells(live_cell) | |
end | |
border_cells.concat live_cells_in_grid.to_a() | |
border_cells.uniq | |
end | |
def self.border_cells cell | |
border_cells = [] | |
(-1..1).each do |x| | |
(-1..1).each do |y| | |
next if [x, y] == [0, 0] | |
border_cells << [cell[0] + x, cell[1] + y] | |
end | |
end | |
border_cells | |
end | |
def self.live_neighbors_count live_cells_in_grid, cell | |
c = 0 | |
border_cells(cell).each do |border_cell| | |
c += 1 if live_cells_in_grid.include?(border_cell) | |
end | |
c | |
end | |
def self.next_state_alive initially_alive, num_neighbors | |
(initially_alive and num_neighbors == 2) or | |
(num_neighbors == 3) | |
end | |
end |
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_relative 'life' | |
module Life | |
describe "an empty grid" do | |
subject { Set.new } | |
it "turns into another empty grid" do | |
next_grid = Life.next_grid(subject) | |
expect(next_grid.size).to eq(0) | |
end | |
end | |
describe "a grid with one live cell" do | |
subject do | |
grid = Set.new | |
grid << [42, 37] | |
end | |
it "turns into en empty grid" do | |
next_grid = Life.next_grid(subject) | |
expect(next_grid).to be_empty | |
end | |
end | |
describe "a grid with 3 live neighbors" do | |
subject do | |
grid = Set.new | |
grid << [1,1] | |
grid << [2,1] | |
grid << [2,2] | |
end | |
it "brings a cell to life" do | |
next_grid = Life.next_grid(subject) | |
expect(next_grid).to include([1,2]) | |
end | |
it "preserves the live cells with 2 or 3 neighbors" do | |
next_grid = Life.next_grid(subject) | |
expect(next_grid).to include([1,1]) | |
expect(next_grid).to include([2,1]) | |
expect(next_grid).to include([2,2]) | |
end | |
it "counts a cell's neighbors" do | |
expect(Life.live_neighbors_count(subject, [1,1])).to eq(2) | |
expect(Life.live_neighbors_count(subject, [0,0])).to eq(1) | |
expect(Life.live_neighbors_count(subject, [0,1])).to eq(1) | |
end | |
end | |
describe "a particular cell" do | |
subject { [5,5] } | |
it "should gather all the coordinates around it" do | |
expect(Life.border_cells(subject)).to match_array( | |
[[4,4],[4,5],[4,6], | |
[5,4], [5,6], | |
[6,4],[6,5],[6,6]]) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment