/* Creates further generations */ public static void NewGeneration(ref Cell[,] currentGeneration) { Cell[,] nextGeneration = new Cell[COLUMNS, ROWS]; for (var i = 0; i < Game.COLUMNS; i++) { for (var j = 0; j < Game.ROWS; j++) { var currentCell = (Cell)currentGeneration[i, j].Clone(); var state = currentCell.State; /* Calculate neighbors of the current cell and store the returned value. */ var neighbors = Game.CountNeighbors(currentGeneration, i, j); /* Rule #4: Any dead cell with exactly 3 neighbors becomes an alive cell. */ if (state == Cell.DEAD && neighbors == 3) { nextGeneration[i, j] = currentCell; nextGeneration[i, j].State = Cell.ALIVE; nextGeneration[i, j].Age = 1; } /* Rule #1 and #3: Any alive cell with fewer than 2 or more than 3 alive neighbors dies. */ else if (state == Cell.ALIVE && (neighbors < 2 || neighbors > 3)) { nextGeneration[i, j] = currentCell; nextGeneration[i, j].State = Cell.DEAD; nextGeneration[i, j].Age = 0; /* Later WasAlive property will be used to distinguish cells which were once alive from dead ones. */ nextGeneration[i, j].WasAlive = true; } /* The rest stays same. Follows Rule #2: Any alive cell with 2 or 3 neighbors lives on the next generation. */ else { nextGeneration[i, j] = currentCell; /* Increase age of the alive cell as it'll keep living on the next generation. */ if (currentCell.State == Cell.ALIVE) { nextGeneration[i, j].Age++; } } /* Regenerate colors based on changed properties. */ nextGeneration[i, j].ChangeColor(); } } currentGeneration = nextGeneration.Clone() as Cell[, ]; }