コード例 #1
0
        /// <summary>
        /// Instantiate a new operator node and make sure that its value is as specified
        /// and the tree rooted at this node has the specified number of operand nodes.
        /// </summary>
        /// <param name="goal"></param>
        /// <param name="numberOfNewNodes"></param>
        public OperatorNode(int goal, int numberOfNewNodes = 2)
        {
            int  left, right;
            char op;

            // Choose a random valid operator for this node and split the goal
            do
            {
                op = Tree.operators.GetRandomElement();
            } while (!SplitHelper.SplitValue(goal, op, out left, out right));

            Operator = new Operator(op);
            Left     = new OperandNode(left);
            Right    = new OperandNode(right);

            int toLeft, toRight;

            // Distribute the number of new operands to be made between the two sides
            SplitHelper.SplitNumberOfOperands(left, right, numberOfNewNodes, out toLeft, out toRight);

            Left  = Left.Expand(toLeft);
            Right = Right.Expand(toRight);
        }