-
-
Save coreyhaines/665346 to your computer and use it in GitHub Desktop.
def tick(cell, number_of_neighbors) | |
if number_of_neighbors < 2 || number_of_neighbors > 3 | |
cell.setAlive false | |
end | |
if number_of_neighbors == 3 | |
cell.setAlive true | |
end | |
end |
I'm a little lost on this - maybe a little more context is needed for me to understand what is going on.
My initial thought would be to change it into:
def tick(cell, number_of_neighbors)
cell.setAlive number_of_neighbors == 3 #pull out 3 into some meaningful name
end
But I don't understand what the tick method is for ... tick and setAlive don't go well for me. I might also pull the boolean expression out into its own method so it would read something like:
cell.setAlive ifCloseNeighbors
But in doing this, setAlive and ifCloseNeighbors don't meld well for me either.
I would like to hear other peoples interpretation.
Sorry about that. This is a method for handling the ruleset for conway's game of life (GoL): http://en.wikipedia.org/wiki/Conway's_game_of_life
context? shoot, that makes it too easy .
Essentially, this code is invoked for each "tick" of the clock to see what the next "generation" looks like for each cell.
You can see my first (well, second) Game of Life code -- and you can run it locally to watch each tick: https://github.com/JonKernPA/gol-2
regarding SRP... well, this is an "Arrogant" type method, why ask an object (who is just a stupid worker anyway) to do work it should be responsible for when you can just "do it all" right here?
regarding OCP... not sure if you mean the mere fact that the cell object's live/die behavior is completely controlled externally? That is, I can randomly change life to be when there are 4 neighbors instead of 3.
On a side note, couldn't they at least have collapsed the "==3" bit into an "else?"