Created
September 15, 2014 13:24
-
-
Save pawel2105/1c6539d78fed67f725d4 to your computer and use it in GitHub Desktop.
Game of life implementation
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 World | |
attr_accessor :cells | |
def initialize | |
@cells = [] | |
end | |
def push_cell cell | |
@cells = @cells << cell | |
end | |
end | |
class Cell | |
attr_accessor :state, :x, :y, :world, :neighbour_count | |
def initialize(x,y,state,world) | |
@x = x | |
@y = y | |
@neighbour_count = 0 | |
@state = state | |
@world = world | |
@world.push_cell self | |
end | |
def all_minus_self | |
cells_minus_self = world.cells - [self] | |
end | |
def coords | |
[@x,@y] | |
end | |
def more_than_three(neighbour_count) | |
neighbour_count > 3 | |
end | |
def less_than_two(neighbour_count) | |
neighbour_count < 2 | |
end | |
def count_neighbors | |
check_loop | |
@neighbour_count | |
end | |
def check_for_neighbours | |
the_neighbour_count = count_neighbors | |
while (more_than_three the_neighbour_count) || (less_than_two neighbour_count) | |
return @state = false | |
end | |
@state = true | |
end | |
def self.is_neighbour(cell, target, x_range, y_range) | |
(cell.x + x_range == target.x) && (cell.y + y_range == target.y) | |
end | |
def check_loop | |
all_minus_self.each do |cell| | |
(-1..1).each do |x_range| | |
(-1..1).each do |y_range| | |
(Cell.is_neighbour(cell, self, x_range, y_range)) ? (@neighbour_count += 1) : nil | |
end | |
end | |
end | |
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 'rspec' | |
require_relative '../cell.rb' | |
describe Cell do | |
it 'initializes with coords and a state' do | |
cell = Cell.new(0,0,true,World.new) | |
expect(cell.state).to eq true | |
expect(cell.coords).to eq [0,0] | |
end | |
it 'dies when there are greater than 3 cells adjacent to it' do | |
world = World.new | |
cell = Cell.new(1,2,true,world) | |
Cell.new(2,3,true,world) | |
Cell.new(1,3,true,world) | |
Cell.new(0,1,true,world) | |
Cell.new(2,2,true,world) | |
cell.check_for_neighbours | |
expect(cell.state).to eq false | |
end | |
it 'dies when there are less than 2 cells adjacent to it' do | |
world = World.new | |
cell = Cell.new(1,2,true,world) | |
Cell.new(2,3,true,world) | |
cell.check_for_neighbours | |
expect(cell.state).to eq false | |
end | |
it "carries on if there are 2 or 3 neighbours adjacent to it" do | |
world = World.new | |
cell = Cell.new(1,2,true,world) | |
Cell.new(2,3,true,world) | |
Cell.new(1,3,true,world) | |
Cell.new(0,1,true,world) | |
cell.check_for_neighbours | |
expect(cell.state).to eq true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment