/// <summary> /// Create input and output grids by using rows and column count and initialize reachable cells. /// Reachable Cells are cells which can be traversed from inner grid cells or outer grid cells i.e. virtual cells used for expanding grid /// </summary> /// <param name="rows"></param> /// <param name="columns"></param> public void PlayGame(int rows, int columns) { if (rows <= 0 || columns <= 0) { throw new ArgumentOutOfRangeException("Row and Column size must be greater than zero"); } try { _inputGrid = new Grid(rows, columns); _outputGrid = new Grid(rows, columns); ReachableCell.InitializeReachableCells(); } catch (Exception ex) { ExceptionLogging.SendErrorToText(ex); } }
/// <summary> /// Count live adjacent cells for specified cell co-ordinates /// </summary> /// <param name="grid"></param> /// <param name="coOrdinates"></param> /// <returns>returns number of live neighbours</returns> private static int CountAliveNeighbours(Grid grid, CoOrdinates coOrdinates) { int liveNeighbours = 0; // Get the Cell type of current cell CellTypeEnum enumInnerCell = ReachableCell.GetCellType(grid, coOrdinates); List <CoOrdinates> reachableCells = new List <CoOrdinates>(); // populate reachable cells from current cell for easier traversing ReachableCell.ReachableCells.TryGetValue(enumInnerCell, out reachableCells); if (reachableCells.Count == 0) { throw new ArgumentNullException("Cannot find reachable co-ordinates"); } foreach (CoOrdinates coOrds in reachableCells) { liveNeighbours += IsAliveNeighbour(grid, coOrdinates, coOrds); } return(liveNeighbours); }