public static IGrid CreateGrid(IGridSize gridSize, IEnumerable<ICell> liveCells) { var grid = new Grid(gridSize); //Mark all cells foreach (var liveCell in liveCells) { grid.MakeCell(liveCell.Position, liveCell.Alive); } return grid; }
/// <summary> /// Parses a string specifying row and column index of a live /// <see cref="ICell" /> /// The format of the sting is rowIndex,colIndex | rowIndex,colIndex /// and returns a <see cref="IGrid{ICell}" /> object containing <paramref name="numberofRows" /> /// rows and <paramref name="numberOfcolumns" /> /// </summary> /// <param name="gridRowColumnString"> </param> /// <param name="gridSize"> </param> /// <returns> </returns> public static IEnumerable<ICell> ParseLiveCell(this string gridRowColumnString, IGridSize gridSize) { //create a grid and initialize it with dead cells //var grid = CreateGrid(numberofRows, numberOfcolumns); var liveCells = new List<ICell>(); gridRowColumnString = gridRowColumnString.Trim(' ', RowSeparator); if (gridRowColumnString.Length != 0) //no alive cells { var rowColumnPairs = gridRowColumnString.Split(RowSeparator); liveCells.AddRange( rowColumnPairs.Select(rowColumnPair => ParseCellPosition(rowColumnPair, gridSize)).Select( cellPosition => new Cell(cellPosition, true)).Cast<ICell>()); } return liveCells; }
/// <summary> /// Get the grid size information from user in rownumber,columnnumber format /// Populate the gridsize field with parsed information /// </summary> /// <returns> True or false based on whether procesing is successful or not </returns> private bool TakeGridSizeFromUser() { System.Console.Write("Enter Grid Size in (RowNumber,ColumnNumber) format followed by Enter key : "); ApplicationLogger.LogMessage(1, "Start getting the Grid size from user", LogCategory.Business, TraceEventType.Information); var userInput = System.Console.ReadLine(); var result = false; try { _gridSize = userInput.GetGridSize(); result = true; } catch (ArgumentException ex) { System.Console.WriteLine("Following error occurred : " + ex.Message); ApplicationLogger.LogException(1, ex, LogCategory.Business, TraceEventType.Error); } catch (Exception ex) { System.Console.WriteLine("Unknown error occurred : " + ex.Message); ApplicationLogger.LogException(1, ex, LogCategory.Business, TraceEventType.Error); } return result; }
private static IPosition ParseCellPosition(string rowColumnPair, IGridSize gridSize) { if (!rowColumnPair.Contains(ColumnSeparator)) //no valid row,col index pair { throw new ArgumentException(string.Format("The row column pair {0} has no rowColumn separator", rowColumnPair)); } var cellIndex = rowColumnPair.Split(ColumnSeparator); if (!cellIndex.Any()) //no valid row,col index { throw new ArgumentException(string.Format("The row column pair {0} is not valid", rowColumnPair)); } int rowIndex; if (!Int32.TryParse(cellIndex[0], out rowIndex) || rowIndex < 0 || rowIndex >= gridSize.Rows) { throw new ArgumentException(string.Format("The row column pair {0} is not valid", rowColumnPair)); } int colIndex; if (!Int32.TryParse(cellIndex[1], out colIndex) || colIndex < 0 || colIndex >= gridSize.Columns) { throw new ArgumentException(string.Format("The row column pair {0} is not valid", rowColumnPair)); } return new Position(rowIndex, colIndex); }
public GridStorage(IGridSize gridSize) { if (gridSize == null) throw new ArgumentNullException("gridSize"); _gridSize = gridSize; _gridDataContainer = new bool[gridSize.Rows,gridSize.Columns]; }
public Grid(IGridSize gridSize) { if (gridSize == null) throw new ArgumentNullException("gridSize"); GridSize = gridSize; _gridStorage = new GridStorage(gridSize); }