Conway's Game of Life#

Simple rules, emergent behaviour

c++ icon 🏫 windows icon

Conway's Game of Life

Description

Game of Life is a cellular automation devised by John Conway in 1970. My application puts the game on screen as a Windows Form application. It was written in C++ for a first-year university assignment. The application uses the standard rules:

Development

GeeksforGeeks has a great tutorial which explains how you could code this application in a variety of languages. I based my code on this and added functions for loading from and saving to a CSV file. There are a few important things to consider:

int countNeighbours(bool boolArray[][gridSize], int x, int y) {
	int count = 0;
	for (int i(-1); i < 2; ++i) { //for every row before and after the passed position
		for (int j(-1); j < 2; ++j) { //for every column before and after the passed position
			int checkX = x + i;
			int checkY = y + j;

			//edge cases - wrap coordinates around the grid
			if (checkX < 0) {
				checkX = gridSize-1;
			}
			else if (checkX >= gridSize) {
				checkX = 0;
			}

			if (checkY < 0) {
				checkY = gridSize-1;
			}
			else if (checkY >= gridSize) {
				checkY = 0;
			}
			//add to neighbours if not checking self and checked cell is true (alive)
			if ( !(i == 0 && j == 0) && (boolArray[checkX][checkY] == true)) {
				++count;
			}
		}
	}
	return count;
}

When the project was first set there was no requirement for a GUI, so I output the simulation in the terminal.

Game of Life in a terminal
Terminal application
Loading a CSV file in the terminal
Loading a CSV
A glider pattern emerges in the terminal
Glider pattern

Features

This video has no sound

Animating the grid of cells
Animating the grid
Loading a CSV
Loading a CSV
Clicking on cells to change their state
Changing cell states
Changing grid size during compilation
Changing gridd size during compilation