public int GetLivingNeighbors(int xPoint, int yPoint) { var livingNeighbors = 0; for (var x = -1; x <= 1; x++) { for (var y = -1; y <= 1; y++) { if (x == 0 && y == 0) { /*do nothing, as is self*/ } else if (x + xPoint < 0 || y + yPoint < 0 || x + xPoint >= Grid.GetLength(0) || y + yPoint >= Grid.GetLength(1)) { /*do nothing, as out of bounds of Grid*/ } else if (Grid[xPoint + x, yPoint + y].IsAlive) { livingNeighbors++; } } } return(livingNeighbors); }
/// <summary> /// Advances to the next generation /// </summary> public void TickNextGen() { int neightbours; // first pass to mark changes for (int row = 0; row < Grid.GetLength(0); row++) { for (int col = 0; col < Grid.GetLength(1); col++) { neightbours = CountLiveNeighbours(row, col); if (Grid[row, col] == (int)CellStatus.dead && neightbours == 3) { Grid[row, col] = (int)CellStatus.born; } else if (Grid[row, col] == (int)CellStatus.alive && neightbours != 2 && neightbours != 3) { Grid[row, col] = (int)CellStatus.dying; } } } // second pass to make changes for (int row = 0; row < Grid.GetLength(0); row++) { for (int col = 0; col < Grid.GetLength(1); col++) { if (Grid[row, col] == (int)CellStatus.dying) { Grid[row, col] = (int)CellStatus.dead; } else if (Grid[row, col] == (int)CellStatus.born) { Grid[row, col] = (int)CellStatus.alive; } } } }