#include #include using namespace std; const int size = 20; int oldmap[size][size]; int newmap[size][size]; void createmap(); void printmap(); void update(); void copy(); int main(){ createmap(); printmap(); update(); copy(); cout << endl; printmap(); } void update(){ int sum; for(int y = 0; y < size; y++){ for(int x = 0; x < size; x++){ // set newmap value based on oldmap values // newmap[x][y] = something about the sum of neighbors oldmap sum = 0; if(x>0 && y>0) sum = oldmap[x-1][y-1]; if(y>0) sum += oldmap[x][y-1]; if(x 0) sum += oldmap[x+1][y-1]; if(x > 0) sum += oldmap[x-1][y]; if(x < size -1) sum += oldmap[x+1][y]; if(x > 0 && y < size -1) sum += oldmap[x-1][y+1]; if(y < size -1) sum += oldmap[x][y+1]; if(x < size -1 && y < size -1) sum += oldmap[x+1][y+1]; newmap[x][y] = 0; if(sum == 3) newmap[x][y] = 1; if(sum == 2 && oldmap[x][y] == 1) newmap[x][y] = 1; } } } void copy(){ for(int y = 0; y < size; y++){ for(int x = 0; x < size; x++){ oldmap[x][y] = newmap[x][y]; } } } void printmap(){ for(int y = 0; y < size; y++){ for(int x = 0; x < size; x++){ cout << oldmap[x][y]; } cout << endl; } } void createmap(){ for(int y = 0; y < size; y++){ for(int x = 0; x < size; x++){ oldmap[x][y] = rand()%2; } } }