//Create next generation of creatures internal void MakeLife() { for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { byte _n; _n = _neighbors[x, y]; //Kill cell if under- or over-populated if (current.Cols[x].Cells[y].IsAlive && (_n < 2 || _n > 3)) { next.Cols[x].Cells[y].IsAlive = false; } //Kepp cell alive if (current.Cols[x].Cells[y].IsAlive && (_n == 2 || _n == 3)) { next.Cols[x].Cells[y].IsAlive = true; } //Resurect dead cell if (!current.Cols[x].Cells[y].IsAlive && _n == 3) { next.Cols[x].Cells[y].IsAlive = true; } } } //Save created generation as current and fill in neighbors table current = next; FillNeighbors(); }
//Glider void DrawGlider(GRID g, int x, int y) { g.Cols[x].Cells[y].IsAlive = true; g.Cols[x + 1].Cells[y].IsAlive = true; g.Cols[x + 2].Cells[y].IsAlive = true; g.Cols[x].Cells[y + 1].IsAlive = true; g.Cols[x + 1].Cells[y + 2].IsAlive = true; }
//Dakota void DrawDakota(GRID g, int x, int y) { g.Cols[x + 1].Cells[y].IsAlive = true; g.Cols[x + 4].Cells[y].IsAlive = true; g.Cols[x].Cells[y + 1].IsAlive = true; g.Cols[x].Cells[y + 2].IsAlive = true; g.Cols[x + 4].Cells[y + 2].IsAlive = true; g.Cols[x].Cells[y + 3].IsAlive = true; g.Cols[x + 1].Cells[y + 3].IsAlive = true; g.Cols[x + 2].Cells[y + 3].IsAlive = true; g.Cols[x + 3].Cells[y + 3].IsAlive = true; }
private readonly GRID next; //temporary next generation //Create game of life object - class constructor public GOL(int GameWidth, int GameHeight) { Width = GameWidth; Height = GameHeight; _neighbors = new byte[Width, Height]; current = new(Width, Height); next = new(Width, Height); InitializeLife(-1); }
//Random board void DrawRandom(GRID g) { Random _rnd = new(); foreach (var row in g.Cols) { foreach (var cell in row.Cells) { if (_rnd.Next(3) < 1) { cell.IsAlive = false; } else { cell.IsAlive = true; } } } }
//Drawing methods //Blinker void DrawBlinker(GRID g, int x, int y) { g.Cols[x].Cells[y + 1].IsAlive = true; g.Cols[x + 1].Cells[y + 1].IsAlive = true; g.Cols[x + 2].Cells[y + 1].IsAlive = true; }