Last active
March 21, 2020 08:20
-
-
Save ApprenticeGC/10cda015cc9dc330b0fa4fa81ae01aaf to your computer and use it in GitHub Desktop.
Game of Life main rule
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
int GetNextGenerationStatus(int statusOfCurrentCell, int[] statusOfCurrentNeighbors) | |
{ | |
var count = statusOfCurrentNeighbors.Length; | |
var aliveNeighborCount = 0; | |
for (var i = 0; i < count; ++i) | |
{ | |
var statusOfCurrentNeighbor = statusOfCurrentNeighbors[i]; | |
if (statusOfCurrentNeighbor == 1) | |
{ | |
aliveNeighborCount += 1; | |
} | |
} | |
// Just default to 0 | |
var statusOfNextGeneration = 0; | |
// At this point, alive count is calculated | |
if (statusOfCurrentCell == 1) | |
{ | |
if (aliveNeighborCount < 2) | |
{ | |
statusOfNextGeneration = 0; | |
} | |
else if (aliveNeighborCount == 2 || aliveNeighborCount == 3) | |
{ | |
statusOfNextGeneration = 1; | |
} | |
else if (aliveNeighborCount > 3) | |
{ | |
statusOfNextGeneration = 0; | |
} | |
} | |
else if (statusOfCurrentCell == 0) | |
{ | |
if (aliveNeighborCount == 3) | |
{ | |
statusOfNextGeneration = 1; | |
} | |
} | |
return statusOfNextGeneration; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment