Conway's Game of Life is a specific rule in cellular automation that makes a for a fun organic system that is both Turing complete and allows for 'evolution' over time, meaning that organized structures can and will grow out of chaos.
The system is pretty simple. We have a 2D grid (though other dimensions can be done) that contains binary values 1 or 0, alive or dead. Each step, we check a cell, and it's 8 neighbors.
 If the cell is currently alive, it dies unless there are exactly 2 or 3 neighbors, so no underpopulation or overpopulation allowed.
 If the cell is currently dead, it lives if there are exactly 3 neighbors, so reproduction.
This can be pretty short in C++, implemented like this for example:
 bool Life::checkLife(int x, int y) {
int n = neighbors(x,y); // neighbors
return n==3  (world[x][y] && n==2);
} 


We can seed the system with some initial pattern of alive and dead cells, or even just completely random. And then we just continue to step through the system. That's it!

 int seed = time(NULL); 
 srand(seed);
// Initialize world
for (int i = 1; i < size1; ++i)
for (int j = 1; j < size1; ++j)
world[i][j] = ((float)rand() / RAND_MAX > 0.5) ? true : false; 
I also wanted to learn some basic terminalgui based visualization, so I decided to do that instead of OpenGL or similar techniques, which can get really big really quickly. It has the added advantage of not requiring any nonstandard libraries, and looks retro to boot.
Comments
Post a Comment