Conway's Game Of Life

Background

Gavin Whitson, 6/12/2024

Conway's game of life has always been something that has piqued my interest. Since hearing about it and how simple the rules themselves are, I have wanted to make my own implementation of this famed cellular automaton.

Implementation

The Game of Life is a simple program with 4 simple rules:

  1. Any live cell with fewer than two live neighbors dies, as if by underpopulation.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction..

Rules of Conway's Game of Life

This makes the program an excellent jumping off point for learning a new language. The implemenation for this uses an array and accessing the items in it, using branching logic, and printing some formatted output. Those are some of the basic parts of any language and makes you implement some features that will likely be used in many other projects. Although I used languages I was already familiar with for these implementations, any time that I feel like experimenting with a new language this will likely be my first test program.

There is not much different that I can do for making this program. To make it more easily understandable and translatable, I made it use three different functions with the main function calling them. Code shown is in python.

def countNbors(x: int, y: int, c: dict) -> int: # Count of neighbors around cell return count def update(c: dict) -> dict: # Update all cells using countNbors function return loc def display(c: dict): # Clear terminal and display def main(): # declare cells and set starting structure while True: cells = update(cells) display(cells)

The update function loops through all cells. I used a 1 dimensional array for this and calculated positions using the global 'row' and 'col' variables that defined the size of the window. The update function calls to 'countNbors' with the calculated x and y values and depending on the result updates the cell based on the rules defined. This does potentially allow us to define our own rules.

With this basic structure setup, it is quite simple to implement this program with any language. The main thing to remember is that you go slow to go fast. It is important to fully understand the rules as you implement them. Getting one of the rules wrong will result in the output being underwhelming. If anything, get the base program working and then work on adjusting the rules to use more streamlined logic. Working, even poorly, is better than not at all.

Future Work

I have already implemented this in python as well as Golang. I will likely use this to familiarize myself with languages that I may use or even as a method for learning UI frameworks within a known language. It would have been preferable to start with this project when using the bubbletea library when I used it to design a framework for another project. Although I can normally get by experimenting and toying with a language, making a basic project first will help learn the fundamentals of whatever I am working with. If you have some time and haven't done it before. I recommend doing your own implementation of Conway's Game of Life.

Code is available on my Github