Wednesday, 28 November 2012

Game of Life


Here is one small attempt to write a program that runs Conway's Game Of Life. 
It is a zero player game which does not require any user input once it has been started with 
an initial configuration. It is basically an infinite 2-D array of cells, each cell having 
state of either being alive (1) or being dead (0). At each step (tick of time), state of 
cell changes based on 4 simple rules (referred from aforementioned wikipedia article) -
  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overcrowding.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Below is a simple C# console application that runs Game of life. It is by no means
the best implementation but it is a good representation of an auto-updating state machine.

class Program
    {
        static void Main(string[] args)
        {
            int[,] state = GetInputArray();
 
            for (int i = 0; i < 5; i++)
            {
                state = NexStep(state, i, (int)Math.Sqrt((double)state.LongLength));
                Console.WriteLine("**************************************************");
            }
 
            Console.ReadLine();
        }
 
        private static int[,] GetInputArray()
        {
            //int[,] state = new int[3, 3];
            //state[0, 0] = 0;
            //state[0, 1] = 1;
            //state[0, 2] = 0;
 
            //state[1, 0] = 0;
            //state[1, 1] = 1;
            //state[1, 2] = 0;
 
            //state[2, 0] = 0;
            //state[2, 1] = 1;
            //state[2, 2] = 0;
 
            int[,] state = new int[5, 5];
            state[0, 0] = 0;
            state[0, 1] = 1;
            state[0, 2] = 1;
            state[0, 3] = 1;
            state[0, 4] = 0;
 
            state[1, 0] = 0;
            state[1, 1] = 1;
            state[1, 2] = 0;
            state[1, 3] = 1;
            state[1, 4] = 0;
 
            state[2, 0] = 0;
            state[2, 1] = 1;
            state[2, 2] = 0;
            state[2, 3] = 1;
            state[2, 4] = 0;
 
            state[3, 0] = 0;
            state[3, 1] = 1;
            state[3, 2] = 1;
            state[3, 3] = 1;
            state[3, 4] = 0;
 
            state[4, 0] = 1;
            state[4, 1] = 1;
            state[4, 2] = 0;
            state[4, 3] = 1;
            state[4, 4] = 0;
            
            return state;
        }
 
        private static int[,] NexStep(int[,] state, int stepCounter, int arrSize)
        {
            int[,] newState= null;
 
            if (stepCounter > 0)
                newState = GetNextState(state,arrSize);
            else
                newState = state;
 
            PrintCurrentState(newState, arrSize);
 
            return newState;
        }
 
        private static void PrintCurrentState(int[,] state, int size)
        {
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    Console.Write(state[i, j]);
                    Console.Write(" ");
                }
 
                Console.Write(Environment.NewLine);
            }
        }
 
        static int[,] GetNextState(int[,] oldState, int arrSize)
        {
            int[,] newState = new int[arrSize, arrSize];
            for (int i = 0; i < arrSize; i++)
            {
                for (int j = 0; j < arrSize; j++)
                {
                    int aliveCells = GetAliveNeighbourCount(oldState, i, j, arrSize);
                    if (aliveCells < 2 || aliveCells > 3)
                        newState[i, j] = 0;
                    if (aliveCells == 2)
                        newState[i, j] = oldState[i, j];
                    if (aliveCells == 3)
                        newState[i, j] = 1;
                }
            }
 
            return newState;
        }
 
        static int GetAliveNeighbourCount(int[,] currState, int x, int y, int arrSize)
        {
            int sumOfCells = 0;
            for(int i = x - 1; i <= x + 1; i ++)
            {
                for (int j = y - 1; j <= y + 1; j++)
                {
                    if (i < 0 || j < 0) continue;
                    if (i >= arrSize || j >= arrSize) continue;
                    if (i == x && j == y) continue;
                    sumOfCells += currState[i, j];
                }
            }
 
            return sumOfCells;
        }
    }

No comments:

Post a Comment