Seeking a bit of for-fun programming for the weekend and an excuse to refresh myself on the basics of
ncurses, I decided to write my own small implementation of Conway's
Game of Life. If you've never had a chance to become acquainted with this "game," I highly recommend taking the time to read about it or to play with one of the
many implementations (including mine below if you'd like). It is a great study in how a small number of simple rules can lead to some amazing emergent phenomena when applied at a larger scale.
In a nutshell, the "game" (I use quotes because it is not a game in the traditional, competitive sense, although some people have created variants that allow two players to "compete") is staged in a 2-D world that one can think of as a grid of "cells." The life of each cell depends upon its neighbors; cells like some company, but not too much! For each "step" in the game, each cell's life is evaluated based on the number of living neighbors it has (above and below, left and right, and the four diagonals). The life of the cell following the step is determined by three rules:
- If the number of living neighbors is exactly 2, the cell remains alive or dead (whichever it was previously).
- If the number of living neighbors is exactly 3, the cell is "born" (or remains alive).
- Otherwise, the cell "dies" (or remains dead).
Pretty simple, right? So simple that an entire implementation
with an ncurses interface takes less than 200 lines of code (as
sloccount reports; minus whitespace and comments). So, let's get to it!