예제 #1
0
파일: Game.cs 프로젝트: nknigam/Life
        /// <summary>
        /// compute the next state of board on next generation
        /// </summary>
        /// <returns>Board</returns>
        public Board NextState()
        {
            Board output = new Board(_rowCount, _columnCount);

            Parallel.For(0, _rowCount - 1, x =>
            {
                Parallel.For(0, _columnCount - 1, y =>
                {
                    int numberOfNeighbors = NeighbourCell.CountNeighbours(_input, x, y);

                    bool shouldLive = false;
                    bool isAlive = _input[x, y];

                    if (isAlive && (numberOfNeighbors == 2 || numberOfNeighbors == 3))
                    {   //living cell with exactly 2 or 3 neighbour will be still alive
                        shouldLive = true;
                    }
                    else if (!isAlive && numberOfNeighbors == 3)
                    {   // dead cell with 3 neighbour will get life
                        shouldLive = true;
                    }

                    output[x, y] = shouldLive;

                });
            });

            return output;
        }
예제 #2
0
파일: Game.cs 프로젝트: nknigam/Life
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="rows"></param>
        /// <param name="columns"></param>
        /// <param name="timeInterval"></param>
        public Game(int rows, int columns)
        {
            _rowCount = rows;
            _columnCount = columns;

            _input = new Board(_rowCount, _columnCount);
        }
예제 #3
0
        public void IsCellExistWhenAdded()
        {
            Board b = new Board(2,2);

            b.GiveLife(1, 1);

            Assert.IsTrue(b.IsCellExist(1, 1));
        }
예제 #4
0
        public void ShouldBeActiveWhenLifeGivenTwoTimesToSameCell()
        {
            Board b = new Board(2,2);

            b.GiveLife(1, 1);
            b.GiveLife(1, 1);

            Assert.IsTrue(b.IsCellExist(1, 1));
        }
예제 #5
0
파일: Game.cs 프로젝트: nknigam/Life
 /// <summary>
 /// Update the game board with next generation output
 /// </summary>
 /// <param name="nextGenBoard">output of next generation</param>
 /// <returns>async task</returns>
 public Task UpdateBoard(Board nextGenBoard)
 {
     return Task.Factory.StartNew(() =>
     {
         // when a generation has completed
         // now assign next generation output to the game board.
         _input = nextGenBoard;
         _generation++;
     });
 }
예제 #6
0
        public void InActiveCellWhenLifeNotGiven()
        {
            Board b = new Board(2,2);

            Assert.IsFalse(b.IsCellExist(1, 1));
        }