コード例 #1
0
        public override ILifeBoard NextGeneration(ILifeBoard initial)
        {
            if (initial == null)
            {
                throw new ArgumentNullException(nameof(initial));
            }

            initial.GenerationCount++;

            ILifeBoard newCells = new LifeBoardBool(initial.RowCount, initial.ColumnCount, initial.GenerationCount, null);

            for (int i = 0; i < initial.RowCount; i++)
            {
                for (int j = 0; j < initial.ColumnCount; j++)
                {
                    int countNeighbors = CountNeighbors(initial, i, j);

                    newCells[i, j] = initial[i, j] ?
                                     (countNeighbors == 2 || countNeighbors == 3)
                                                : (countNeighbors == 3);
                }
            }

            return(newCells);
        }
コード例 #2
0
        static public ILifeBoard FromPattern(IEnumerable <string> initialRows)
        {
            if (initialRows == null)
            {
                throw new ArgumentNullException(nameof(initialRows));
            }

            if (initialRows.Any(l => l == null))
            {
                throw new ArgumentOutOfRangeException(nameof(initialRows));
            }

            int rowCount = initialRows.Count();

            if (rowCount == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialRows));
            }

            int colCount = initialRows.Max(l => l.Length);

            if (colCount == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(initialRows));
            }

            if (rowCount > GetMaxRows() || colCount > GetMaxColumns())
            {
                throw new ArgumentOutOfRangeException(nameof(initialRows));
            }

            ILifeBoard cells = new LifeBoardBool(rowCount, colCount, 0, null);

            int rowNum = 0;

            foreach (var row in initialRows)
            {
                var l = row;
                if (l.Length < colCount)
                {
                    l = l + new string('0', colCount - l.Length);
                }

                for (int c = 0; c < colCount; c++)
                {
                    cells[rowNum, c] = l[c] == '1' || l[c] == 'X';
                }
                rowNum++;
            }
            return(cells);
        }