private void buttonRun_Click(object sender, EventArgs e) { int maxGenerations = int.Parse(textBoxGenerations.Text); int generationsPerSecond = int.Parse(textBoxGenerationsPerSecond.Text); int spawnChanceAtStart = int.Parse(textBoxSpawnPercent.Text); int cellSize = int.Parse(textBoxCellSize.Text); if (_thread != null) { if (_thread.ThreadState == ThreadState.Suspended) { _thread.Resume(); return; } else { KillThread(); } } int gridSize = panel1.Width; gridSize = (int)Math.Ceiling((double)gridSize / cellSize); CellRenderer renderer = new CellRenderer(panel1.CreateGraphics(), generationsPerSecond, maxGenerations, gridSize, cellSize); _gameOfLife.Setup(spawnChanceAtStart, gridSize); _thread = new Thread(() => { // Generations for (int genNum = 0; genNum < maxGenerations; genNum++) { _gameOfLife.NewGeneration(renderer, genNum); } }); _thread.Start(); }
public override void NewGeneration(CellRenderer renderer, int generationNumber) { System.GC.KeepAlive(_cells); // Calculate the new state List<Point> points = new List<Point>(); for (int x = 0; x < _arraySize; x++) { for (int y = 0; y < _arraySize; y++) { Cell cell = _cells[x, y]; if (cell.IsAlive) { points.Add(cell.Point); } if (cell.ShouldDie()) { cell.DieNextGen(); } else { cell.LiveNextGen(); } } } for (int x = 0; x < _arraySize; x++) { for (int y = 0; y < _arraySize; y++) { _cells[x, y].UpdateFromLastGen(); } } renderer.Paint(generationNumber, points); }
public override void NewGeneration(CellRenderer renderer, int generationNumber) { System.GC.KeepAlive(_cells); // Calculate the new state List <Point> points = new List <Point>(); for (int x = 0; x < _arraySize; x++) { for (int y = 0; y < _arraySize; y++) { Cell cell = _cells[x, y]; if (cell.IsAlive) { points.Add(cell.Point); } if (cell.ShouldDie()) { cell.DieNextGen(); } else { cell.LiveNextGen(); } } } for (int x = 0; x < _arraySize; x++) { for (int y = 0; y < _arraySize; y++) { _cells[x, y].UpdateFromLastGen(); } } renderer.Paint(generationNumber, points); }
public abstract void NewGeneration(CellRenderer renderer, int generationNumber);