public static cell[,] nextGeneration(cell[,] gen) { //Init new array with dimensions of last array cell[,] nextGen = new cell[gen.GetLength(0), gen.GetLength(1)]; //Loop for (int x = 0; x < gen.GetLength(0); x++) { for (int y = 0; y < gen.GetLength(1); y++) { //Getting each cell to calculate its next state gen[x, y].calcNextState(gen, x, y); } } for (int x = 0; x < gen.GetLength(0); x++) { for (int y = 0; y < gen.GetLength(1); y++) { //Switch cell state gen[x, y].switchState(); //Populating the new cell array. Cells state will be last cells switched state nextGen[x, y] = new cell(gen[x, y].getState()); } } /* * The reason why I calculate the state then switch the state is because if I did it all in one go. Each cells new state would * be based on the last cells switched state. Basically the new generation would be based on its self */ return(nextGen); }
//Initialize function public static cell[,] init(int width, int height) { //Create new cell 2d array based on width and height cell[,] cells = new cell[width, height]; //Loop through 2d array for (int i = 0; i < cells.GetLength(0); i++) { for (int x = 0; x < cells.GetLength(1); x++) { /* * Creation of new cell with random number * Even though the cells state can only be 0 or 1 im making the random number a double because when you seed the random number * its based on your computers time because there is no way to get "true randomness". * Since the random number is based on the seed. The sample can only be so many numbers. In this case if I did 0 to 1 * It would only ever be 0 or 1, so I need to generate a double, which can be a wide range of decimal numbers and round the number */ cells[i, x] = new cell(random.NextDouble()); } } return(cells); }