top of page
conways game of life 1.png

Conway's Game of Life

Conway's Game of Life made in Unity

Videos

https://github.com/JoAMD/Conways-Game-of-Life

Github Repo given above

​

The MainGameControl.cs script has a coroutine which invokes two C# actions at equal intervals of gameTick / 2.

All the cells (cellBehaviour scripts) in the scene subscribe to these actions. In the first action/gameTick only the live cells broadcast to the 8 neighbouring cells that they have one extra neighbour alive by incrementing the counter neighbourAliveCtr.

In the second gameTick, all the cells update their live or dead status using the rules and the current updated neighbor count

​

Here, we convert the list of cells, which are children of a holder transform, into a 2d array. The children transforms should be in order for this to work. In the future, I could try a feature of arranging jumbled cells into the array by using the positions and number of cells

Here's the CellsHolder class which contains the 2d array of all cells, the DIRS array of Vector2Int to make it easier to find the 8 neighbours, and a function to give alive count to neighbours. Note that the DIRS array is connected with the array index and not the actual position of the cells.

CellBehaviour.cs is given below. Each cell has one of this component. It subscribes to the 2 C# actions OnEnable and OnDisable. On the first C# action invocation, GiveAliveCountToNeighbours gets calls (explained above). On the second C# action call, Game2() gets calls and this has the major rules of the game of life. 

​

Instead of C# actions, we could also use normal for loops to loop through the cells in the same manner. So two for loops, one for neighbour count and the second for the rules and alive and dead updation. We could optimise this as well, by running the second for loop only among or near previously alive cells since only their neighbours have a chance of living again. But of course this would depend on the rules provided.

Project Gallery

< Previous Project
Next Project >
bottom of page