Skip to content

Instantly share code, notes, and snippets.

@samklr
Created September 29, 2012 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samklr/3803598 to your computer and use it in GitHub Desktop.
Save samklr/3803598 to your computer and use it in GitHub Desktop.
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