예제 #1
0
        public CellMatrix Import(string[] lines, CellMatrixOverflowPolicy overflowPolicy)
        {
            var matrix = new CellMatrix(lines[0].Length, lines.Length, overflowPolicy); // szerokość to długość zerowej linijki, wysokość to liczba linijek

            for (var x = 0; x < matrix.Width; ++x)
            {
                for (var y = 0; y < matrix.Height; ++y)
                {
                    CellLifeState state;

                    if (lines[y][x] == '#')
                    {
                        state = CellLifeState.Alive;
                    }
                    else
                    {
                        state = CellLifeState.Dead;
                    }

                    matrix.SetCellLifeState(x, y, state);
                }
            }

            return(matrix);
        }
예제 #2
0
        public void CalculateNextMatrix()
        {
            var nextMatrix = new CellMatrix(_currentMatrix.Width, _currentMatrix.Height, _currentMatrix.OverflowPolicy);

            for (var x = 0; x < _currentMatrix.Width; ++x)
            {
                for (var y = 0; y < _currentMatrix.Height; ++y)
                {
                    var state          = _currentMatrix.GetCellLifeState(x, y);
                    var aliveNeighbors = CountAliveNeighbors(x, y);

                    switch (state)
                    {
                    case CellLifeState.Dead:
                        if (aliveNeighbors == 3)
                        {
                            nextMatrix.SetCellLifeState(x, y, CellLifeState.Alive);
                        }
                        else
                        {
                            nextMatrix.SetCellLifeState(x, y, CellLifeState.Dead);
                        }
                        break;

                    case CellLifeState.Alive:
                        if (aliveNeighbors < 2 || aliveNeighbors > 3)
                        {
                            nextMatrix.SetCellLifeState(x, y, CellLifeState.Dead);
                        }
                        else
                        {
                            nextMatrix.SetCellLifeState(x, y, CellLifeState.Alive);
                        }
                        break;
                    }
                }
            }

            _currentMatrix = nextMatrix;
        }