private int NeighborsCount(int x, int y) { int neighbors = 0; if (y - 1 >= 0 && StepRepair[y - 1, x]) //N { ++neighbors; } if (y + 1 < StepRepair.GetLength(0) && StepRepair[y + 1, x]) //S { ++neighbors; } if (x - 1 >= 0 && StepRepair[y, x - 1]) //W { ++neighbors; } if (x + 1 < StepRepair.GetLength(1) && StepRepair[y, x + 1]) //E { ++neighbors; } if (y - 1 >= 0 && x - 1 > 0 && StepRepair[y - 1, x - 1]) //NW { ++neighbors; } if (y - 1 >= 0 && x + 1 < StepRepair.GetLength(1) && StepRepair[y - 1, x + 1]) //NE { ++neighbors; } if (y + 1 < StepRepair.GetLength(0) && x - 1 >= 0 && StepRepair[y + 1, x - 1]) //SW { ++neighbors; } if (y + 1 < StepRepair.GetLength(0) && x + 1 < StepRepair.GetLength(1) && StepRepair[y + 1, x + 1]) //SE { ++neighbors; } return(neighbors); }
private void CellularAlgorithm() { bool[,] newCellularArray = new bool[StepRepair.GetLength(0), StepRepair.GetLength(1)]; for (int i = 0; i < StepRepair.GetLength(0); ++i) { for (int j = 0; j < StepRepair.GetLength(1); ++j) { int neighbors = NeighborsCount(j, i); if (StepRepair[i, j]) { newCellularArray[i, j] = (neighbors >= 2); } else { newCellularArray[i, j] = neighbors > 4; } } } StepRepair = newCellularArray; }