예제 #1
0
        public static StateOfCell GetNewCellState(StateOfCell CurrentCellState, int NumOfLiveNeighbors)
        {
            //if (CurrentCellState == StateOfCell.Alive && NumOfLiveNeighbors < 2)
            //{
            //    return StateOfCell.Dead;
            //}
            //if (CurrentCellState == StateOfCell.Alive && NumOfLiveNeighbors > 3)
            //{
            //    return StateOfCell.Dead;
            //}
            //if (CurrentCellState == StateOfCell.Dead && NumOfLiveNeighbors == 3)
            //{
            //    return StateOfCell.Alive;
            //}

            // A switch statement is better
            switch (CurrentCellState)
            {
            case StateOfCell.Alive:
                if (NumOfLiveNeighbors < 2 || NumOfLiveNeighbors > 3)
                {
                    return(StateOfCell.Dead);
                }
                break;

            case StateOfCell.Dead:
                if (NumOfLiveNeighbors == 3)
                {
                    return(StateOfCell.Alive);
                }
                break;
            }
            return(CurrentCellState);
        }
예제 #2
0
        private static StateOfCell[,] NextIteration(StateOfCell[,] currentStateOfLife)
        {
            var dimension = currentStateOfLife.GetLength(0);

            var result = new StateOfCell[dimension, dimension];

            for (int row = 0; row < dimension; row++)
            {
                for (int column = 0; column < dimension; column++)
                {
                    var prevRow    = row == 0 ? dimension - 1 : row - 1;
                    var nextRow    = row == dimension - 1 ? 0 : row + 1;
                    var prevColumn = column == 0 ? dimension - 1 : column - 1;
                    var nextColumn = column == dimension - 1 ? 0 : column + 1;

                    var neighbours = new List <StateOfCell>
                    {
                        currentStateOfLife[prevRow, prevColumn],
                        currentStateOfLife[prevRow, column],
                        currentStateOfLife[prevRow, nextColumn],
                        currentStateOfLife[row, prevColumn],
                        currentStateOfLife[row, nextColumn],
                        currentStateOfLife[nextRow, prevColumn],
                        currentStateOfLife[nextRow, column],
                        currentStateOfLife[nextRow, nextColumn]
                    };

                    result[row, column] = GetNewState(currentStateOfLife[row, column], neighbours);
                }
            }
            return(result);
        }
예제 #3
0
        public void SelfIsDeadTest(int amoutOfAliveNeighbours, StateOfCell expectState)
        {
            var cellOfConway = new CellOfConway();
            var expected     = expectState;
            var actual       = cellOfConway.CheckCheckState(StateOfCell.Dead, amoutOfAliveNeighbours);

            Assert.AreEqual(expected, actual);
        }
예제 #4
0
        public StateOfCell CheckCheckState(StateOfCell state, int amoutOfAliveNeighbours)
        {
            if (state == StateOfCell.Dead)
            {
                return((amoutOfAliveNeighbours == 3) ? StateOfCell.Alive : StateOfCell.Dead);
            }

            return((amoutOfAliveNeighbours < 2) ? StateOfCell.Dead :
                   (amoutOfAliveNeighbours < 4) ? StateOfCell.Alive : StateOfCell.Dead);
        }
예제 #5
0
        public void CellsWithMoreThanThreeLiveNeighbors()
        {
            // Any live cell with more than three live neighbours dies
            StateOfCell CurrentCellState   = StateOfCell.Alive;
            int         NumOfLiveNeighbors = 4;

            StateOfCell output = Conway.GetNewCellState(CurrentCellState, NumOfLiveNeighbors);

            Assert.AreEqual(StateOfCell.Dead, output);
        }
예제 #6
0
        public void CellsWithTwoOrThreeliveNeighbors()
        {
            // Any live cell with two or three live neighbours lives
            StateOfCell CurrentCellState   = StateOfCell.Alive;
            int         NumOfLiveNeighbors = 3;

            StateOfCell output = Conway.GetNewCellState(CurrentCellState, NumOfLiveNeighbors);

            Assert.AreEqual(StateOfCell.Alive, output);
        }
예제 #7
0
        public void CellsWithLessThanTwoLiveNeighb()
        {
            // Any live cell with fewer than two live neighbours dies
            StateOfCell CurrentCellState   = StateOfCell.Alive;
            int         NumOfLiveNeighbors = 1;

            StateOfCell output = Conway.GetNewCellState(CurrentCellState, NumOfLiveNeighbors);

            Assert.AreEqual(StateOfCell.Dead, output);
        }
예제 #8
0
        public void DeadCellsWithExaltyThreeLiveNeighbors()
        {
            // Any dead cell with exactly three live neighbours becomes a live cell

            StateOfCell CurrentCellState   = StateOfCell.Dead;
            int         NumOfLiveNeighbors = 3;

            StateOfCell output = Conway.GetNewCellState(CurrentCellState, NumOfLiveNeighbors);

            Assert.AreEqual(StateOfCell.Alive, output);
        }
예제 #9
0
        private static StateOfCell GetNewState(StateOfCell currentState, List <StateOfCell> neighbours)
        {
            var amountOfAliveNeighbours = neighbours.Count(n => n == StateOfCell.Alive);

            if (currentState == StateOfCell.Dead && amountOfAliveNeighbours == 3)
            {
                return(StateOfCell.Alive);
            }
            if (currentState == StateOfCell.Alive && (amountOfAliveNeighbours == 2 || amountOfAliveNeighbours == 3))
            {
                return(StateOfCell.Alive);
            }
            return(StateOfCell.Dead);
        }
예제 #10
0
        private static StateOfCell[,] PopulateInitialState(Parameters parameters)
        {
            Random random = new Random();

            var initialState = new StateOfCell[parameters.Width, parameters.Width];

            for (int row = 0; row < parameters.Width; row++)
            {
                for (int column = 0; column < parameters.Width; column++)
                {
                    initialState[row, column] = (decimal)random.NextDouble() <= parameters.SpawnRate ?
                                                StateOfCell.Alive : StateOfCell.Dead;
                }
            }
            return(initialState);
        }