public override void Start() { Console.WriteLine("Game of life started"); Cell[,] testCells = new Cell[sizeY, sizeX]; for (uint i = 0; i < testCells.GetLength(0); i++) { for (uint j = 0; j < testCells.GetLength(1); j++) { testCells[i, j] = new DeadCell(); } } Random rand = new Random(); for (int i = 0; i < 5000; i++) { testCells[rand.Next(0, sizeY), rand.Next(0, sizeX)] = new AliveCell(); } Engine.SwitchField(new Field(testCells)); timer.Start(); }
public Cell[,] CreateSetGrid() { Cell[,] outputGrid = new Cell[RandomInt(1, bi.Height), RandomInt(1, bi.Width)]; for (int y = 0; y < outputGrid.GetLength(0); y++) { for (int x = 0; x < outputGrid.GetLength(1); x++) { outputGrid[y, x] = new Cell(RandomBool(bi.Probability)); } } return(outputGrid); }
public static void Tick() { var nextGeneration = new Cell[_cells.GetLength(0), _cells.GetLength(1)]; for (var i = 0; i < nextGeneration.GetLength(0); i++) { for (var j = 0; j < nextGeneration.GetLength(1); j++) { nextGeneration[i, j] = new Cell(false); } } for (var i = 0; i < _cells.GetLength(0); i++) { for (var j = 0; j < _cells.GetLength(1); j++) { var nextGenerationCellResult = CellStatusResult.NoChange; var neighbours = GetNeighbours(_cells, i, j); foreach (var rule in rules) { var result = rule(neighbours, _cells[i, j].CellStatus()); if (result == CellStatusResult.Live || result == CellStatusResult.Die) { nextGenerationCellResult = result; } } //Apply Rule to next generation switch (nextGenerationCellResult) { case CellStatusResult.Live: nextGeneration[i, j].SetToAlive(); break; case CellStatusResult.Die: nextGeneration[i, j].SetToDie(); break; case CellStatusResult.NoChange: nextGeneration[i, j] = _cells[i, j]; break; } } } _cells = nextGeneration; }
public Cell[,] CreateRandomGrid() { Cell[,] outputGrid = new Cell[RandomInt(1, 50), RandomInt(1, 50)]; for (int y = 0; y < outputGrid.GetLength(0); y++) { for (int x = 0; x < outputGrid.GetLength(1); x++) { Double randomDouble = (double)RandomInt(1, 99) / 100; outputGrid[y, x] = new Cell(RandomBool(randomDouble)); } } return(outputGrid); }
public void UpdateStatus(Cell[,] currentWorld) { int liveNeighbourCount = 0; for (var r = Row - 1; r <= Row + 1; r++) { for (var c = Column - 1; c <= Column + 1; c++) { if (r >= 0 && r < currentWorld.GetLength(0) && c >= 0 && c < currentWorld.GetLength(1) && currentWorld[r, c].Status == CellStatus.Alive) { if (r == Row && c == Column) continue; liveNeighbourCount++; } } } //if (liveNeighbourCount < 2 || liveNeighbourCount > 3) //{ // Status = CellStatus.Dead; //} if (liveNeighbourCount == 3) { Status = CellStatus.Alive; } else if (liveNeighbourCount == 2) { Status = currentWorld[Row, Column].Status; } else { Status = CellStatus.Dead; } // if 2, nothing changes. Lived keeps alive }