/// <summary> /// Finds the number of neighbours of a given cell that meet the passed in specification. /// </summary> /// <param name="cell">Cell whose neighbours have to be found.</param> /// <param name="specification">The specification that should be met by the neighbouring cells.</param> /// <returns>Returns the collection of cells that meet the passed in specification.</returns> public ICollection<Cell> FindNeighbours(Cell cell, ISpecification<Cell> specification) { List<Cell> cells = new List<Cell>(); if (cell == null) { throw new ArgumentNullException(paramName:"Cell"); } int column = cell.ColumnNumber; int row = cell.RowNumber; int startrow; int startcol; int endcol; int endrow; GetValidStartRowAndColumn(row, column, out startrow, out startcol); GetValidEndRowAndColumn(row, column, out endrow, out endcol); for (int xcoord = startrow; xcoord <= endrow; xcoord++) { for (int ycoord = startcol; ycoord <= endcol; ycoord++) { if (!(xcoord == row && ycoord == column)) { if (specification == null) cells.Add(grid[xcoord, ycoord]); else if(specification.IsSatisfiedBy(grid[xcoord, ycoord])) cells.Add(grid[xcoord, ycoord]); } } } return cells; }
/// <summary> /// Evolves the cell by applying the evolution rules. /// </summary> /// <param name="cell">Cell to evolve.</param> /// <returns>Returns the evolved cell.</returns> public Cell EvolveCell(Cell cell) { if(cell == null) { throw new ArgumentNullException(paramName:"Cell"); } Cell evolvedcell = new Cell(cell.RowNumber, cell.ColumnNumber, false); int aliveneighbours = neighbourRule.FindNeighbours(cell, aliveSpecification).Count; if ((cell.IsAlive && (aliveneighbours == 2 || aliveneighbours == 3)) || (!cell.IsAlive) && aliveneighbours == 3) { evolvedcell.IsAlive = true; } return evolvedcell; }
public Grid(int rowCount, int columnCount) { if ((rowCount <= 0 || columnCount <= 0) && (rowCount> 100000 || columnCount> 100000)) { throw new ArgumentOutOfRangeException("The rowCount and columnCount should be greater than 0 and less than 100000."); } this.rowCount = rowCount; this.columnCount = columnCount; cells = new Cell[rowCount][]; for (int i = 0; i < rowCount; i++) { cells[i] = new Cell[columnCount]; for (int j = 0; j < columnCount; j++) { cells[i][j] = new Cell(i, j, false); } } }
public static void MyClassInitialize(TestContext testContext) { alivecell = new Cell(0, 0, true); deadcell = new Cell(0, 0, false); }