// project created on 08/10/2006 at 16:25 using System; namespace CellSimple { /* From wikipedia: 1. Any live cell with fewer than two neighbours dies, as if by loneliness. 2. Any live cell with more than three neighbours dies, as if by overcrowding. 3. Any live cell with two or three neighbours lives, unchanged, to the next generation. 4. Any dead cell with exactly three neighbours comes to life.*/ class MainClass { public static int dims = 10; public static int[,] theGrid = new int[dims, dims]; public static void Main(string[] args) { Random populator = new Random(); Console.WriteLine("Start of the CellSimple program."); int i, j; for (i = 0; i < dims; i++) { for(j = 0; j < dims; j++) { int newNumber; newNumber = (int) Math.Round(populator.NextDouble()); theGrid[i,j] = newNumber; // Console.WriteLine(i + ", " + j + ": " + theGrid[i,j]); } } outputGrid(); //Console.Write(cellStat(1,1)); updateGrid(); Console.WriteLine("--------------------------------------------"); outputGrid(); } public static void outputGrid() { int i = 0, j = 0; string line = ""; //line = new String(); for (i = 0; i < dims; i++) { line = "\n"; for(j = 0; j < dims; j++) { if (theGrid[i,j] == 1) line = line + "#\t"; else line = line + "0\t"; //Console.WriteLine(i + ", " + j); } Console.WriteLine(line); } } public static string cellStat(int xCo, int yCo) { string output; output = "Value of Cell (" + xCo + ", " + yCo + ") is: " + theGrid[xCo,yCo] + ". "; output += "The number of living neighbours to this cell is: " + getNeighbours(xCo, yCo, theGrid) + "."; return output; } public static void updateGrid() { int[,] oldGrid = theGrid; for (int i = 0; i < dims; i++) { for(int j = 0; j < dims; j++) { if (oldGrid[i,j] ==1) // if it's alive { if (getNeighbours(i,j, oldGrid) != 2 && getNeighbours(i,j, oldGrid) != 3) { //Console.WriteLine("killing cell " + i + ", " + j + " as it has " + getNeighbours(i,j, oldGrid)); theGrid[i,j] = 0; // we kill it } } else { //Console.WriteLine("cell " + i + ", " + j + " has " + getNeighbours(i,j, oldGrid)); if (getNeighbours(i, j, oldGrid) == 3) theGrid[i,j] = 1; // we revive it } } } } /** Oh mercy this is weak, needs re-write */ public static int getNeighbours(int xCo, int yCo, int[,] givenGrid) { int[,] theGrid = givenGrid; int count = 0; if (xCo > 0 && yCo > 0) // getting the top left if (theGrid[xCo-1, yCo-1] == 1) count++; if (xCo > 0) // getting the top center if (theGrid[xCo-1, yCo] == 1) count++; if (xCo > 0 && yCo < dims-1) // getting the top right if (theGrid[xCo-1, yCo+1] == 1) count++; if (yCo > 0) // getting the top left if (theGrid[xCo, yCo-1] == 1) count++; if (yCo < dims-1) // getting the middle left if (theGrid[xCo, yCo+1] == 1) count++; if (xCo < dims-1 && yCo > 0) // getting the top left if (theGrid[xCo+1, yCo-1] == 1) count++; if (xCo < dims-1) // getting the top left if (theGrid[xCo+1, yCo] == 1) count++; if (xCo < dims-1 && yCo < dims-1) // getting the top left if (theGrid[xCo+1, yCo+1] == 1) count++; /**/ return count; } } }