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(); RedBlueState state = GetCell(rowIndex, columnIndex).state; switch (state) { case RedBlueState.RED: square.Background = Brushes.Red; break; case RedBlueState.BLUE: square.Background = Brushes.Blue; break; case RedBlueState.DEAD: square.Background = Brushes.White; break; } matrix.Children.Add(square); Grid.SetRow(square, rowIndex); Grid.SetColumn(square, columnIndex); } } }
public override void Iterate() { RedBlueCell[,] newCells = new RedBlueCell[_size, _size]; for (int row = 0; row < _size; row++) { for (int column = 0; column < _size; column++) { int living = countLivingSurroundingCells(row, column); RedBlueState victoriousState = getVictors(row, column); //this species wins in this area if (isAlive(row, column)) { if (living == 2 || living == 3) { RedBlueState dominantSpecies = getVictors(row, column); newCells[row, column] = new RedBlueCell(dominantSpecies); } else { newCells[row, column] = new RedBlueCell(RedBlueState.DEAD); //dead } } else { if (living == 3) { RedBlueState dominantSpecies = getVictors(row, column); newCells[row, column] = new RedBlueCell(dominantSpecies); //no tie possible here } else { newCells[row, column] = new RedBlueCell(RedBlueState.DEAD); } } } } cells = newCells; }
private RedBlueState getVictors(int row, int column) { int numRed = 0; int numBlue = 0; for (int rowIndex = row - 1; rowIndex < row + 2; rowIndex++) { for (int columnIndex = column - 1; columnIndex < column + 2; columnIndex++) { if (isAlive(rowIndex, columnIndex)) { RedBlueState state = GetCell(rowIndex, columnIndex).state; if (state == RedBlueState.RED) { numRed++; } else if (state == RedBlueState.BLUE) { numBlue++; } } } } if (numRed == numBlue) { //a tie can only occur when this center cell is alive, in that case it retains it's color return(GetCell(row, column).state); } else if (numRed > numBlue) { return(RedBlueState.RED); } else { return(RedBlueState.BLUE); } }
public RedBlueCell(RedBlueState newState) { _state = newState; }
public RedBlueCell() { _state = RedBlueState.DEAD; }