Skip to content

Instantly share code, notes, and snippets.

@samklr samklr/gol.rb
Created Sep 29, 2012

Embed
What would you like to do?
GOL
class GOL
def initialize
@cells = Array.new(10,Array.new(10))
for i in 0..9
for j in 0..9
@cells[i][j] = 0
end
end
end
def cells
@cells
end
def neighbors (i,j)
##TODO check for edge cases
neighborhood = Array.new
neighborhood[0] = @cells[i-1][j]
neighborhood[1] = @cells[i-1][j+1]
neighborhood[2] = @cells[i-1][j-1]
neighborhood[0] = @cells[i+1][j]
neighborhood[0] = @cells[i+1][j-1]
neighborhood[0] = @cells[i+1][j+1]
neighborhood[0] = @cells[i][j-1]
neighborhood[0] = @cells[i][j+1]
neighborhood
end
def nextState (i,j)
alive_neighbors =neighbors(i,j).count {|x| x ==1}
return 0 if (@cells[i][j] === 1)&&((alive_neighbors <2) || (alive_neighbors > 3)) #rule1
return 1 if (@cells[i][j] === 1)&&((alive_neighbors == 2)|| (alive_neighbors == 3))
return 1 if (@cells[i][j] === 0)&&(alive_neighbors == 3)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.