Пример #1
0
        /*
         * Static method that can be utilised with 'Grid'/'GridInterface' objects to provide a
         * deep copy of a 'GridInterface' object.
         */
        public static GridInterface <CellInterface> getDeepCopy(this GridInterface <CellInterface> grid)
        {
            var gridCopy = new Grid(grid.noOfRows, grid.noOfColumns);

            foreach (var cell in grid.Cells)
            {
                var cellCopy = new Cell {
                    rowIndex = cell.rowIndex, columnIndex = cell.columnIndex, isAlive = cell.isAlive
                };
                gridCopy.addCell(cellCopy);
            }

            return(gridCopy);
        }
Пример #2
0
        /// <summary>
        /// Takes individual cells and returns a formatted
        /// string representing the grid.
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public static string toConsoleFormattedString(this GridInterface <CellInterface> grid)
        {
            var builder = new StringBuilder();

            for (var rowIndex = 0; rowIndex < grid.noOfRows; rowIndex++)
            {
                for (int columnIndex = 0; columnIndex < grid.noOfColumns; columnIndex++)
                {
                    builder.Append(grid.getCellByIndex(rowIndex, columnIndex).isAlive ? LiveCell : DeadCell);
                    builder.Append(Separator);
                }
                builder.Append(Environment.NewLine);
            }

            return(builder.ToString());
        }
Пример #3
0
        public void Execute(GridInterface <CellInterface> currentGrid)
        {
            var gridCopy = currentGrid.getDeepCopy();

            _neighbourCalculator.Grid    = gridCopy;
            _gameRules.LiveCellRule.Grid = gridCopy;
            _gameRules.DeadCellRule.Grid = gridCopy;

            foreach (var cell in currentGrid.Cells)
            {
                if (cell.isAlive)
                {
                    _gameRules.LiveCellRule.Execute(cell);
                }
                else
                {
                    _gameRules.DeadCellRule.Execute(cell);
                }
            }
        }
Пример #4
0
        private bool TakeLiveCellsFromUser()
        {
            Console.WriteLine(
                "Enter live cells in the format: rowIndex,colIndex : rowIndex,colIndex.\nfollowed by Enter key");
            var userInput = Console.ReadLine();
            var result    = false;

            try
            {
                _userGrid = _gridRowColumnParser.ParseGrid(userInput, _numberOfRows, _numberOfColumns);
                result    = true;
            }
            catch (ArgumentException exception)
            {
                Console.WriteLine("Following error occurred : " + exception.Message);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Unknown error occurred : " + exception.Message);
            }
            return(result);
        }