Game Of Life by fontan10 - 1

Games & Projects

Conway's Game of Life in Unity

Unity 2019.3.13f1Unknown LicenseUpdated 43 days agoCreated on May 19th, 2020
Go to source

Game of Life

John Conway's cellular automation Game of Life written in C# with Unity.

Tags: C#, Unity

Program logic is in GameOfLife/Assets/Scripts


Background

John Conway first throught of the idea in 1970. The cells follow four simple rules:

  1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.


From these simple instructions, very complex behaviour arises.

Credits to https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life for the above table.


My Approach

I use three 2D arrays to hold the current the current state of the cells, the next state of the cells, and the GameObjects to display on screen. By looping through the current states and updating the GameObjects and next states, the animation is achieved.

I chose to represent the living cells in two ways - either with a solid colour or a colour gradient depending on how long the cell has been alive. You can toggle between the two using the 'Colour' button at the bottom.

More implementations can be added by using the provided interface.

Single Colour Dynamic Colour

How to Play

  • Use this link to run the simulation on your web browser or request a pull and run the game in Unity.

  • Play around with the buttons and sliders at the bottom of the screen to adjust the simulation


Extending the Code

The simulation currently represents the living and dead cells in two ways.

If you want to add another respresentation of the cells, create a class that implements interface IGridManagerable. Then add an instance of your class to _gridManagers[] in Main.Start():

private void Start()
    {
        // some code

        _gridManagers = new IGridManagerable[]
        {
            new PolyColourGridManager { TileSprite = Resources.Load<Sprite>("Sprites/Square") },
            new MonoColourGridManager { TileSprite = Resources.Load<Sprite>("Sprites/Square") }
            
            // add your instance here
        };

        // some code
    }

More button functions can also be added by creating a function in the Main class and adding the function to your button.

Show all projects by fontan10