public Grid() { cells = new Cell[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { cells[x, y] = new Cell(cellsValues[x, y]); } } Console.WriteLine("Grid Constructor"); }
public CellLine(int seed) { Cells = new Cell[CELL_LINE_SIZE]; Random randomGen = new Random(seed); for (int c = 0; c < CELL_LINE_SIZE; c++) { //Final state + 1 makes "random" generator inclusive of final state CellState state = (CellState) randomGen.Next((int) CellState.THREE + 1); Cells[c] = new Cell(state); } }
public void regenerate() { Cell[] generatedCells = new Cell[CELL_LINE_SIZE]; for (int c = 0; c < CELL_LINE_SIZE; c++) { CellState state = generateNewState(c); generatedCells[c] = new Cell(state); } //Replace current line with the regenerated line Cells = generatedCells; }
public void Tick() { Cell[,] newCells = new Cell[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { newCells[x, y] = new Cell(); } } int neighbourCount; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { neighbourCount = 0; neighbourCount = CountNeighbours (neighbourCount, x, y); if (cells[x, y].State == 1) { if (neighbourCount < 2)// die newCells[x, y].State = 0; else if (neighbourCount > 3)// die newCells[x, y].State = 0; else if (neighbourCount == 2 || neighbourCount == 3)// live newCells[x, y].State = 1; } else { if (neighbourCount == 3)// live newCells[x, y].State = 1; } } } cells = (Cell[,])newCells.Clone(); }
public void Randomise(out Cell[,] cells) { cells = new Cell[0,0]; }
/// <summary> /// Выбор списка ячеек, соседних с выбранной ячейкой /// </summary> /// <param name="cell"></param> /// <returns></returns> protected IEnumerable<int> selectNeighbours(Cell cell) { int cx = cell.X; int cy = cell.Y; foreach (int xdiff in diffs) { foreach (int ydiff in diffs) { if (ydiff == 0 && xdiff == 0) continue; int x = cx + xdiff; int y = cy + ydiff; if (x >= 0 && x < _width && y >= 0 && y < _height) { yield return getIndex(x, y); } } } }
/// <summary> /// Выбор списка ячеек, соседних с выбранной ячейкой /// </summary> /// <param name="cell"></param> /// <returns></returns> protected IEnumerable<Cell> selectNeighbourCells(Cell cell) { int[] diffs = { -1, 0, 1 }; int cx = cell.X; int cy = cell.Y; foreach (int xdiff in diffs) { foreach (int ydiff in diffs) { if (ydiff == 0 && xdiff == 0) continue; int x = cx + xdiff; int y = cy + ydiff; if (x >= 0 && x < _width && y >= 0 && y < _height) { yield return GetCell(x, y); } } } }