public World(int aHeight, int aWidth) { height = aHeight; width = aWidth; CurrentWorld = new Cell[height, width]; NextWorld = new Cell[height, width]; NumberOfCells = height * width; }
public Cell NextGeneration() { var nextGeneration = new Cell(x, y, Width, Height, 10) { Neighbours = Neighbours }; int liveNeighbours = 0; foreach (Cell neighbour in Neighbours) { if (neighbour.Alive) { liveNeighbours++; } } // Check if the nextGeneration is alive if (Alive) { nextGeneration.Alive = liveNeighbours == 2 || liveNeighbours == 3; } else { // Check if the nextGeneration is reborn nextGeneration.Alive = liveNeighbours == 3; } return nextGeneration; }
public void Setup() { cell = new Cell(1, 1, 1, 1, 1); }
private void DetectNeighbours(ref Cell cell, int xPos, int yPos) { cell.Neighbours.Clear(); // Get the north position int northPos = xPos == 0 ? height - 1 : xPos - 1; cell.Neighbours.Add(CurrentWorld[northPos, yPos]); // Get the south position int southPos = xPos == height - 1 ? southPos = 0 : xPos + 1; cell.Neighbours.Add(CurrentWorld[southPos, yPos]); // Get the east position int eastPos = yPos == 0 ? width - 1 : yPos - 1; cell.Neighbours.Add(CurrentWorld[xPos, eastPos]); // Get the south position int westPos = yPos == width - 1 ? 0 : yPos + 1; cell.Neighbours.Add(CurrentWorld[xPos, westPos]); cell.Neighbours.Add(CurrentWorld[northPos, eastPos]); cell.Neighbours.Add(CurrentWorld[northPos, westPos]); cell.Neighbours.Add(CurrentWorld[southPos, eastPos]); cell.Neighbours.Add(CurrentWorld[southPos, westPos]); }
/// <summary> /// Seeds the world with the Cells /// </summary> /// <param name="lifeChance">Chance that the cell will be alive</param> public void Seed(int lifeChance, int cellWidth, int cellHeight) { Random rnd = new Random(Environment.TickCount); // Generate the cells for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { CurrentWorld[i, j] = new Cell(i, j, cellWidth, cellHeight, 10) { Alive = rnd.Next(100) < lifeChance }; } } // Detect the neighbours for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { DetectNeighbours(ref CurrentWorld[i, j], i, j); } } }