コード例 #1
0
ファイル: RedBlueLife.cs プロジェクト: mgardner-git/life.net
        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);
                }
            }
        }
コード例 #2
0
ファイル: RedBlueLife.cs プロジェクト: mgardner-git/life.net
        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;
        }
コード例 #3
0
ファイル: RedBlueLife.cs プロジェクト: mgardner-git/life.net
        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);
            }
        }
コード例 #4
0
ファイル: RedBlueCell.cs プロジェクト: mgardner-git/life.net
 public RedBlueCell(RedBlueState newState)
 {
     _state = newState;
 }
コード例 #5
0
ファイル: RedBlueCell.cs プロジェクト: mgardner-git/life.net
 public RedBlueCell()
 {
     _state = RedBlueState.DEAD;
 }
コード例 #6
0
ファイル: RedBlueCell.cs プロジェクト: mgardner-git/life.net
 public RedBlueCell(RedBlueState newState)
 {
     _state = newState;
 }
コード例 #7
0
ファイル: RedBlueCell.cs プロジェクト: mgardner-git/life.net
 public RedBlueCell()
 {
     _state = RedBlueState.DEAD;
 }