Game of Life

Simple rules, emergent behaviour

C++ tagđŸĢwindows tag

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

Loading a CSV file in the terminal A glider pattern emerges in the terminal

Features

This video has no sound

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