Skip to content

Instantly share code, notes, and snippets.

@ApprenticeGC
Last active March 21, 2020 08:20
Show Gist options
  • Save ApprenticeGC/10cda015cc9dc330b0fa4fa81ae01aaf to your computer and use it in GitHub Desktop.
Save ApprenticeGC/10cda015cc9dc330b0fa4fa81ae01aaf to your computer and use it in GitHub Desktop.
Game of Life main rule
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