private LifeGrid(LifeGrid source, byte[] newState)
 {
     m_width = source.m_width;
     m_bytesPerRow = source.m_bytesPerRow;
     m_height = source.m_height;
     m_state = Array.AsReadOnly(newState);
 }
Exemple #2
0
 private LifeGrid(LifeGrid previousState, CellState[,] nextState)
 {
     Columns    = previousState.Columns;
     Rows       = previousState.Rows;
     Generation = previousState.Generation + 1;
     Grid       = nextState;
 }
        public LifeGridControl(LifeGrid initialGrid)
        {
            m_grid = initialGrid;
            m_visual = new DrawingVisual();
            AddVisualChild(m_visual);

            // Author's note: by attaching the event handler here, we actually miss seeing the first couple of generations as the control
            //   is rendering. But the code is so much simpler this way, that I'm tentatively willing to make that compromise.
            CompositionTarget.Rendering += Rendering;
        }
Exemple #4
0
        static void Main(string[] args)
        {
            Random rand = new Random();

            Console.Write("Enter the height: ");
            int height = int.Parse(Console.ReadLine());

            Console.Write("Enter the width: ");
            int width = int.Parse(Console.ReadLine());

            int[,] randomGrid = new int[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    randomGrid[x, y] = rand.Next(0, 2);
                }
            }

            LifeGrid lifeGrid = new LifeGrid(randomGrid);

            Console.WriteLine("Generation 0");
            lifeGrid.DrawGen();

            while (lifeGrid.AliveCells() > 0)
            {
                string response;

                Console.WriteLine("\nGeneration {0}", lifeGrid.GenCount);

                lifeGrid.ProcessNextGen();

                lifeGrid.DrawGen();

                if (lifeGrid.AliveCells() == 0)
                {
                    Console.WriteLine("Game over!");
                }
                else
                {
                    Console.Write("Keep going? (y/n): ");

                    response = Console.ReadLine();

                    if (response.ToLower() == "n")
                    {
                        break;
                    }
                }
            }
        }
        /// <summary>
        /// Generates an ILifeGrid grid of ILifeCell cells of the given width and height.
        /// The IsAlive property of each of the cells should be set to false.
        /// Each of the cells should be registered to the events of the grid and its neighbor cells on it.
        /// </summary>
        /// <param name="width">The width (in cells) of the life grid</param>
        /// <param name="height">The height (in cells) of the life grid</param>
        /// <returns>A newly created ILifeGrid</returns>
        public static ILifeGrid CreateEmptyLifeGrid(int width, int height)
        {
            LifeGrid newGrid = new LifeGrid(height, width);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    newGrid[i, j].RegisterLifeGrid(newGrid);
                    newGrid[i, j].IsAlive = false;
                    newGrid[i, j].NextGenerationIsAlive = newGrid[i, j].IsAlive;
                }
            }
            return newGrid;
        }
Exemple #6
0
 public FormRules(LifeGrid grid, Timer tmr)
 {
     InitializeComponent();
     Grid = grid;
     Tmr  = tmr;
     foreach (int rule in grid.rulesCreate)
     {
         listBoxCreation.Items.Add(rule);
     }
     foreach (int rule in grid.rulesSurvive)
     {
         listBoxSurvival.Items.Add(rule);
     }
     numericUpDownSize.Value  = grid.getCellSize();
     numericUpDownTimer.Value = tmr.Interval;
 }
        /// <summary>
        /// Generates an ILifeGrid grid of ILifeCell cells corresponding to the input grid.
        /// i.e. the IsAlive property of the (x,y) cell is grid[x,y].
        /// Each of the cells should be registered to the events of the grid and its neighbor cells on it.
        /// </summary>
        /// <param name="grid">A boolean bidimensional array corresponding corresponing to the state of
        /// the life grid.</param>
        /// <returns>A newly created ILifeGrid</returns>
        public static ILifeGrid CreateLifeGrid(bool[,] grid)
        {
            int rowsNum     = grid.GetLength(0);
            int columnsNum  = grid.GetLength(1);

            LifeGrid newGrid = new LifeGrid(rowsNum, columnsNum);

            for(int i = 0; i < rowsNum; i++)
            {
                for(int j = 0; j < columnsNum; j++)
                {
                    newGrid[i, j].RegisterLifeGrid(newGrid);
                    newGrid[i, j].IsAlive = grid[i, j];
                    if (newGrid[i, j].IsAlive)
                    {
                        newGrid[i, j].OnChangeWrapper();
                    }
                }
            }
            return newGrid;
        }
 private void Rendering(object sender, EventArgs e)
 {
     m_grid = m_grid.Step();
     UpdateVisual();
 }