Game of Life program in C# | Game of Life Problem in C#

 Conway's Game of Life is a classic cellular automaton that simulates the evolution of a grid of cells over discrete time steps. The rules of the game are simple: each cell can be either alive or dead, and the state of a cell in the next generation depends on the states of its neighboring cells. The rules are as follows:

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

Here's a basic implementation of Conway's Game of Life in C#:

using System;

class Program
{
    static int GridWidth = 20;
    static int GridHeight = 20;
    static bool[,] currentGeneration = new bool[GridWidth, GridHeight];
    static bool[,] nextGeneration = new bool[GridWidth, GridHeight];
    static Random random = new Random();

    static void Main(string[] args)
    {
        InitializeGrid();
        Console.CursorVisible = false;

        while (true)
        {
            Console.Clear();
            DisplayGrid();
            UpdateGrid();
            System.Threading.Thread.Sleep(100); // Adjust the delay
//to control the speed.
        }
    }

    static void InitializeGrid()
    {
        for (int x = 0; x < GridWidth; x++)
        {
            for (int y = 0; y < GridHeight; y++)
            {
                currentGeneration[x, y] = random.Next(2) == 0;
            }
        }
    }

    static void DisplayGrid()
    {
        for (int y = 0; y < GridHeight; y++)
        {
            for (int x = 0; x < GridWidth; x++)
            {
                Console.Write(currentGeneration[x, y] ? "█" : " ");
            }
            Console.WriteLine();
        }
    }

    static void UpdateGrid()
    {
        for (int x = 0; x < GridWidth; x++)
        {
            for (int y = 0; y < GridHeight; y++)
            {
                int liveNeighbors = CountLiveNeighbors(x, y);

                if (currentGeneration[x, y])
                {
                    // Apply the rules for live cells.
                    nextGeneration[x, y] = liveNeighbors == 2 || liveNeighbors == 3;
                }
                else
                {
                    // Apply the rules for dead cells.
                    nextGeneration[x, y] = liveNeighbors == 3;
                }
            }
        }

        // Swap current and next generation grids.
        bool[,] temp = currentGeneration;
        currentGeneration = nextGeneration;
        nextGeneration = temp;
    }

    static int CountLiveNeighbors(int x, int y)
    {
        int count = 0;

        for (int dx = -1; dx <= 1; dx++)
        {
            for (int dy = -1; dy <= 1; dy++)
            {
                if (dx == 0 && dy == 0)
                    continue;

                int nx = x + dx;
                int ny = y + dy;

                if (nx >= 0 && nx < GridWidth && ny >= 0 && ny < GridHeight &&
currentGeneration[nx, ny])
                {
                    count++;
                }
            }
        }

        return count;
    }
}

This code creates a console application that simulates Conway's Game of Life. You can adjust the GridWidth and GridHeight variables to change the size of the grid, and the delay in the game loop to control the speed of the simulation.


The provided C# implementation of Conway's Game of Life is quite straightforward, and its time complexity is already quite efficient for a basic implementation. The time complexity for this implementation is O(N^2) in terms of the grid size, where N is GridWidth or GridHeight. This is because for each cell in the grid, you update its state based on the states of its neighbors, and you iterate through the entire grid.

To improve time complexity significantly, you could consider parallelizing the computation using multi-threading or taking advantage of GPU processing, but this would make the code more complex and is not suitable for a basic example.

If you need to handle very large grids or require even more efficiency, you might want to look into more advanced techniques, such as using sparse data structures like HashLife or other optimizations specific to Conway's Game of Life.

In summary, while the given implementation can be optimized in various ways, the time complexity for a basic implementation is already quite efficient for smaller grid sizes. For larger or more efficient implementations, you would need to explore advanced techniques beyond the scope of this basic example.




-----------------------------------------------

Share this

Related Posts

Previous
Next Post »