public override void Randomize() { Random rnd = new Random(); for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { //get a random number that is from 0 to 5 inclusive int random = rnd.Next(6); ColorCell cell = GetCell(row, column); if (random > 2) { cell.IsAlive = false; } else { /* * Byte[] randomColorValues = new byte[3]; * Random randomNumberGenerator = new Random(); * randomNumberGenerator.NextBytes(randomColorValues); //fill the array with 3 random values within the range allowed for bytes * * cell.IsAlive = true; * cell.red = randomColorValues[0]; * cell.green = randomColorValues[1]; * cell.blue = randomColorValues[2]; */ cell.IsAlive = true; cell.red = (byte)rnd.Next(255); cell.green = (byte)rnd.Next(255); cell.blue = (byte)rnd.Next(255); } } } }
public override void UpdateDisplay(Grid matrix) { matrix.Children.Clear(); for (int rowIndex = 0; rowIndex < _size; rowIndex++) { for (int columnIndex = 0; columnIndex < _size; columnIndex++) { Button square = new Button(); ColorCell cell = GetCell(rowIndex, columnIndex); if (cell.IsAlive) { Color color = Color.FromArgb(255, cell.red, cell.green, cell.blue); SolidColorBrush brush = new SolidColorBrush(color); square.Background = brush; } else { square.Background = Brushes.White; } matrix.Children.Add(square); Grid.SetRow(square, rowIndex); Grid.SetColumn(square, columnIndex); } } }
private bool isAlive(int row, int column) { if (row < 0 || row >= _size || column < 0 || column >= _size) { return(false); } else { ColorCell cell = GetCell(row, column); return(cell.IsAlive); } }
public GeneticLife(int inSize) { this._size = inSize; cells = new ColorCell[_size,_size]; for (int row =0; row < _size; row++) { for (int column = 0; column < _size; column++) { cells[row, column] = new ColorCell(); } } }
public GeneticLife(int inSize) { this._size = inSize; cells = new ColorCell[_size, _size]; for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { cells[row, column] = new ColorCell(); } } }
public override String ToString() { String result = ""; for (int rowIndex = 0; rowIndex < _size; rowIndex++) { for (int columnIndex = 0; columnIndex < _size; columnIndex++) { ColorCell cell = GetCell(rowIndex, columnIndex); result += "(" + cell.red + "," + cell.green + "," + cell.blue + ")\t"; } result += "\n"; } return(result); }
public override void Iterate() { ColorCell[,] newCells = new ColorCell[_size, _size]; for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { List <ColorCell> living = getLivingSurroundingCells(row, column); if (isAlive(row, column)) { if (living.Count == 2 || living.Count == 3) { newCells[row, column] = GetCell(row, column); //lives on with no change } else { newCells[row, column] = new ColorCell(); newCells[row, column].IsAlive = false; } } else { if (living.Count == 3) { ColorCell combine = combineCells(living); newCells[row, column] = combine; } else { newCells[row, column] = new ColorCell(); newCells[row, column].IsAlive = false; } } } } cells = newCells; }
private ColorCell combineCells(List <ColorCell> parents) { //use integer to avoid overflow int[] sums = new int[3]; for (int index = 0; index < parents.Count; index++) { ColorCell check = parents[index]; sums[0] += check.red; sums[1] += check.green; sums[2] += check.blue; } //now convert back down to byte for the average byte redAvg = (byte)(sums[0] / parents.Count); //we loose some precision on the division, but that's ok byte greenAvg = (byte)(sums[1] / parents.Count); byte blueAvg = (byte)(sums[2] / parents.Count); ColorCell result = new ColorCell(); result.IsAlive = true; result.red = redAvg; result.green = greenAvg; result.blue = blueAvg; return(result); }
private ColorCell combineCells(List<ColorCell> parents) { //use integer to avoid overflow int[] sums = new int[3]; for (int index=0; index < parents.Count; index++) { ColorCell check = parents[index]; sums[0] += check.red; sums[1] += check.green; sums[2] += check.blue; } //now convert back down to byte for the average byte redAvg = (byte)(sums[0] / parents.Count); //we loose some precision on the division, but that's ok byte greenAvg = (byte)(sums[1] / parents.Count); byte blueAvg = (byte)(sums[2] / parents.Count); ColorCell result = new ColorCell(); result.IsAlive = true; result.red = redAvg; result.green = greenAvg; result.blue = blueAvg; return result; }
public override void Iterate() { ColorCell[,] newCells = new ColorCell[_size,_size]; for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { List<ColorCell> living = getLivingSurroundingCells(row, column); if (isAlive(row, column)) { if (living.Count == 2 || living.Count == 3) { newCells[row, column] = GetCell(row, column); //lives on with no change } else { newCells[row, column] = new ColorCell(); newCells[row, column].IsAlive = false; } } else { if (living.Count == 3) { ColorCell combine = combineCells(living); newCells[row, column] = combine; } else { newCells[row, column] = new ColorCell(); newCells[row, column].IsAlive = false; } } } } cells = newCells; }
private void initializeSimpleColors(ColorCell cell) { cell.red = 1; cell.green = 2; cell.blue = 3; }