Created
October 21, 2017 17:50
-
-
Save Tomekku/87f08ffe05fbb4df0e3cf42b231570fd to your computer and use it in GitHub Desktop.
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
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