ASCII-based Conway's Game of Life in C++

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.
From wiki - A system creating 'gliders'
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.
  1. If the cell is currently alive, it dies unless there are exactly 2 or 3 neighbors, so no under-population or over-population allowed.
  2. 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 < size-1; ++i) for (int j = 1; j < size-1; ++j) world[i][j] = ((float)rand() / RAND_MAX > 0.5) ? true : false;

I also wanted to learn some basic terminal-gui 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.
Simple C++ ASCII implementation of Conway's Game of Life

Source code on Github



Popular posts from this blog

Building a PID hover controller for Kerbal Space Program with kOS and IPython Notebook

Learning TensorFlow #1 - Using Computer Vision to turn a Chessboard image into chess tiles

Visualizing TLE Orbital Elements with Python and matplotlib