You may have heard of Conway’s Game of Life, an infinite grid of dying and living cells. It’s normally a zero-player game and the only parameter is the initial state of the grid. The game then evolves according to a set of rules. The amount of neighbours determines the fate of a cell in the next round. A cell with 3 neighbours will live, a cell with 2 neighbours will stay the same and every other cell will die.

green cell with 3 neighbours, yellow with 2 and red with 8

So, I started implementing this simulation in Unity and C#. First, I tried spawning real 3D objects alias cubes to create the grid which comes with great freedom for spectacular visual effects like custom models or an animated camera. The performance impact was pretty noticeable though, especially on larger grids, and it didn’t fit the simple but elegant theme I was going for. For those reasons I went with a tile map.

Then I chose interesting patterns from Wikipedia and this site, and brought them into the game. The most famous one is the glider, a pattern that moves across the grid, but I also added a few more like the blinker, the beacon and the glider gun. Based on their behaviour I organized them into categories, and you can place each one with a simple click,

glider, blinker, beacon and glider gun

Internally, the entire field is stored as a 2D array of a custom struct that contains data like the state of the cell. In FixedUpdate the game checks the state of each cell and updates it according to the rules. Nothing fancy, and there’s probably room for improvement. One could cache patterns and their evolution which saves calculating this again. That’s what HashLife does. Another possible improvement could be a different way of rendering the cells, maybe with a shader directly into a RawTexture. Especially when zooming the tile map causes a noticeable hiccup.

While this may be interesting to mathematicians and computer scientists, we need more for a game, especially a multiplayer one. So I added a few features to make it more interesting.
First of all, the game now has a competitive aspect. Each cell belongs to a player and shows that with a color.
Secondly, the game does not just run all the time, it stops every x generations and waits for player input. Each player can change a configurable amount of cells individually or by placing figures. The game then evolves according to the rules (also configurable) for a set amount of steps. If all of a player’s cells die, he’s out. The last player standing wins.

player name input, color selection menu, ready button, list of players

You can match up with friends on the same network and play together. One hosts a game and the others can join. While you’re in the lobby, choose a color and set your name. The host can also change options for the game like the amount of cells a players can edit. If you disable “Out at 0 cells”, you can play forever, even in single player, because the game will never end.

options screen, editable cells per player, board size, evolution rules

I also added zooming and a screenshot feature. You can even share screenshots using the native Android/iOS UI thanks to this plugin.

multiple screenshots of game states, each with delete and share button

I’ve made a quick WebGL port of the game, so you can test it right here in your browser. Unfortunately, certain features are only available when actually installing the game, due to the security measures modern browsers implement. For example discovering other players on the local network doesn’t work, so this demo is single player only.
The entire project is available on GitHub, but there’s no binaries for now. I added binaries for Windows, Mac, Linux and Android, check the releases.

Leave a comment