Exemplo n.º 1
0
 /// <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 Game(int rows, int columns)
 {
     if (rows <= 0 || columns <= 0)
     {
         throw new ArgumentOutOfRangeException("Row and Column size must be greater than zero");
     }
     _inputGrid  = new Grid(rows, columns);
     _outputGrid = new Grid(rows, columns);
     ReachableCell.InitializeReachableCells();
 }
Exemplo n.º 2
0
        /// <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);
        }