Conway's Game of Life

Conway's Game of Life

2020, Jun 22    
View on GitHub

About

The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. It is Turing complete and can simulate a universal constructor or any other Turing machine. – Conway’s Game of Life, Wikipedia

Motivation

During my first computer science build week at Lambda School, I built Conway’s Game of Life as an iOS app. The app is complete with a library of presents, the ability to play, pause, skip forward, and wipe the board. It also allows the user to tap the cells, toggling them as alive or dead. I was extremely happy with how the app turned out and I am specifically proud of the UI.

Key-Value Observing

In order to update the current generation number and live cell population as they fluctuated, I needed to use key-value observing. I had previously used KVO in an Objective-C project, so it was interesting to see how it works in Swift.

I first conformed the GameBoard class to NSObject. Then I added the @objc dynamic attributes to the generation and population variables.

class GameBoard: NSObject {
    // MARK: - Properties
    @objc dynamic var generation: Int
    @objc dynamic var population: Int
    ...

Back in the GameOfLifeViewController, I set up the observers to configure the labels.

private var generationObserver: NSKeyValueObservation?
private var populationObserver: NSKeyValueObservation?

generationObserver = golView.gameBoard.observe(\.generation) { [weak self] object, _  in
	self?.generationLabel.text = "Generation\n\(object.generation)"
}

populationObserver = golView.gameBoard.observe(\.population) { [weak self] object, _ in
	self?.populationLabel.text = "Population\n\(object.population)"
}

Game of Life