Motivation

Motivated by being able to render almost anything in a terminal by my prior work with ASCII Surfaces I was thinking about other simple stuff that could use this.

One of the first ideas was implementing Conway’s Game of Life, which I had never implemented and find very interesting.

In short the Conway’s Game of Life happens in a two dimensional grid whose cells are either alive or dead. Depending on a given configuration of this grid one can apply these rules to get another configuration (generation):

  • Any live cell with two or three live neighbors survives.
  • Any dead cell with three live neighbors becomes a live cell.
  • All other live cells die in the next generation. Similarly, all other dead cells stay dead.

The neighbors of a cell are not only the horizontal and vertical but also diagonally adjacent cells, so eight at any given position that is not on the border of the grid. Calculating one generation after another one can observe interesting behavior.

Execution

Using the basic frame drawing approach used in ASCII Surfaces I use the terminal dimensions as the definition of the Game of Life grid. Implementing the rules is straightforward when just iterating over all the cells in each generation. To test the implementation I first added a few known patterns such as blocks, blinkers, gliders and a glider gun. Thereafter generation of a randomized grid at every program start was implemented.

Following one can see a run of the program with a random starting configuration:

Example

Actual code

The repository for the code is in my Gitlab. Feel free to use it in any way you like.