/// <summary> /// This method will return the list of cells that meet the specification. /// <param name="cell"> source cell, whose neighbours need to find </param> /// <param name="specification">Actual specification, which need to pass by neighbours. </param> /// <returns>return the cell collection which pass the specification. </returns> public ICollection<Cell> FindNeighbourCells(Cell cell, ISpecification<Cell> specification) { if(cell == null) throw new ArgumentNullException("Cell can't be empty"); List<Cell> cells = new List<Cell>(); int startRow; int startCol; int endRow; int endCol; int colIndex = cell.ColNumber; int rowIndex = cell.RowNumber; GetValidStartRowAndCol(rowIndex, colIndex, out startRow, out startCol); GetValidEndRowAndCol(rowIndex, colIndex, out endRow, out endCol); for (int xcoord = startRow; xcoord <= endRow; xcoord++) { for (int ycoord = startCol; ycoord <= endCol; ycoord++) { if (!(xcoord == rowIndex && ycoord == colIndex)) { if (specification == null) cells.Add(grid[xcoord, ycoord]); else if (specification.IsSpecificationMeet(grid[xcoord, ycoord])) cells.Add(grid[xcoord, ycoord]); } } } return cells; }
private static string GetUserInput(ref Cell[] cells) { string userinput = Console.ReadLine(); while (true) { try { cells = ValidateUserInput(userinput); break; } catch (Exception) { Console.WriteLine("\n\n\n\n\nInvalid input! Read the instructions below and try again.\n\n"); PrintInstructions(); userinput = Console.ReadLine(); } } return userinput; }
/// <summary> /// Evolve the cell by applying rule. /// </summary> /// <param name="cell">cell to evolve.</param> /// <returns> return the evolve cell.</returns> public Cell CellToEvolve(Cell cell) { if(cell == null) throw new ArgumentNullException("Cell can't be null."); Cell purposecell = new Cell(cell.RowNumber, cell.ColNumber, false); int aliveneighbours = this.neighbourRule.FindNeighbourCells(cell, aliveSpec).Count; if (cell.IsAlive) { if (aliveneighbours == 2 || aliveneighbours == 3) purposecell.IsAlive = true; } else if (aliveneighbours == 3) { purposecell.IsAlive = true; } return purposecell; }
/// <summary> /// /// </summary> /// <param name="rowCount">row limit of the grid</param> /// <param name="columnCount">column limit of the grid</param> public Game(int rowCount, int columnCount) { if ((rowCount <= 0 || columnCount <= 0) || (rowCount > maxLimit || columnCount > maxLimit)) { throw new ArgumentOutOfRangeException("The rowCount and columnCount should be greater than 0 and less than 10000."); } this.rowCount = rowCount; this.colCount = columnCount; cells = new Cell[rowCount][]; for (int rcount = 0; rcount < rowCount; rcount++ ) { cells[rcount] = new Cell[columnCount]; for (int colCount = 0; colCount < columnCount; colCount++) { new Cell(rcount, colCount, false); cells[rcount][colCount] = new Cell(rcount, colCount, false); } } }
private static void SetAliveValue(Cell[] cells, Game grid) { // Initialize with values got as user input. foreach (Cell cell in cells) { grid[cell.RowNumber, cell.ColNumber].IsAlive = true; } }