public Field nextStep() { Field result = new Field(height, width); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { List<Point> adjacent = getAdjacent(new Point(i, j)); int adjacentAliveCount = 0; foreach (var adjCellCoordinates in adjacent) { if (grid[adjCellCoordinates.X][adjCellCoordinates.Y].Alive) adjacentAliveCount++; } if (grid[i][j].Alive) { if (2 <= adjacentAliveCount && adjacentAliveCount <= 3) result.grid[i][j].Alive = true; } else { if (3 == adjacentAliveCount) result.grid[i][j].Alive = true; } } } return result; }
public static Field GenerateRandomField(int height, int width) { Random random = new Random(146); Field result = new Field(height, width); if(height <= 0 || width <= 0) throw new ArgumentOutOfRangeException(); for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) result.grid[i][j].Alive = random.Next(2) == 1; return result; }