Exemple #1
0
        /// <summary>
        /// returns the amount of living neighbor cells
        /// </summary>
        public short countLiveNeighbors(cCell c)
        {
            short lives = 0;

            for (int col = c.iX - 1; col <= c.iX + 1; col++)
            {
                for (int row = c.iY - 1; row <= c.iY + 1; row++)
                {
                    //don't count self
                    if (col == 1 && row == 1)
                    {
                        continue;
                    }

                    //don't check borders
                    if (col < 0 || col >= _ulX || row < 0 || row >= _ulY)
                    {
                        continue;
                    }

                    lives += (_aBoard[col][row].bIsAlive) ? (short)1 : (short)0;
                }
            }
            return(lives);
        }
Exemple #2
0
        /// <summary>
        /// the game rules are in here
        /// </summary>
        private void evalNextCellState(cCell cell)
        {
            short nb = countLiveNeighbors(cell);

            cell.bNextState = cell.bIsAlive;

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

            // rule 1
            if (nb < 2)
            {
                cell.bNextState = false;
            }

            // rule 2
            //unchanged

            // rule 3
            if (nb > 3)
            {
                cell.bNextState = false;
            }

            // rule 4
            if (!cell.bIsAlive && nb == 3)
            {
                cell.bNextState = true;
            }
        }