Skip to content

Instantly share code, notes, and snippets.

@Tomekku
Created October 21, 2017 17:50
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 Tomekku/87f08ffe05fbb4df0e3cf42b231570fd to your computer and use it in GitHub Desktop.
Save Tomekku/87f08ffe05fbb4df0e3cf42b231570fd to your computer and use it in GitHub Desktop.
const int sizeX = 10;
const int sizeY = 10;
void checkCells(bool map[sizeX][sizeY])
{
bool newMap[sizeX][sizeY];
for(int i=0; i<sizeX; i++)
{
for(int j=0; j<sizeY; j++)
{
bool cell = map[i][j];
int neighbours[] = {
((j-1 < 0) || (i-1 < 0)) ? 0 : (int)map[i-1][j-1],
(i-1 < 0) ? 0 : (int)map[i-1][j],
((j + 1 >= sizeY) || (i-1 < 0)) ? 0 : (int)map[i-1][j+1],
(j-1 < 0) ? 0 : (int)map[i][j-1],
(j + 1 >= sizeY) ? 0 : (int)map[i][j+1],
((j-1 < 0) || (i+1 >= sizeX)) ? 0 : (int)map[i+1][j-1],
((i+1 >= sizeX)) ? 0 : (int)map[i+1][j],
((j +1 >= sizeY) || (i+1 >= sizeX)) ? 0 : (int)map[i+1][j+1],
};
int neighboursCount = 0;
for (int iter = 0; iter < sizeof(neighbours)/sizeof(*neighbours); iter++)
neighboursCount += neighbours[iter];
newMap[i][j] = cell;
if(cell && neighboursCount != 3 && neighboursCount != 2)
newMap[i][j] = false;
if(!cell && neighboursCount == 3)
newMap[i][j] = true;
}
}
for(int i=0; i<sizeX*sizeY; i++)
map[i%sizeX][i/sizeY] = newMap[i%sizeX][i/sizeY];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment