Exemple #1
0
        /// <summary>
        /// Prints the tree by using a stack and keeping track of the score left
        /// </summary>
        public static void PrintScoreTree(int totalScore, GameScoreNode root)
        {
            Stack <int> scoreStack = new Stack <int>();

            //skips the root (zero) and recurse on each children
            foreach (GameScoreNode node in root.Children)
            {
                FindLeafAndPrint(totalScore, node, scoreStack);
            }
        }
Exemple #2
0
        /// <summary>
        /// Builds a n-ary tree with game score possibilities.
        /// </summary>
        /// <param name="totalScore">Total game points.</param>
        /// <param name="scores">List of possible scores</param>
        public static GameScoreNode BuildScoreTree(int totalScore, int[] scores)
        {
            //root has a zero as the subtracted node
            GameScoreNode root = new GameScoreNode(0);

            //starts recursion
            Score(totalScore, root, scores);

            return(root);
        }
Exemple #3
0
        /// <summary>
        /// Recursivelly adds scores changes to a root node.
        /// Assumes root is non-null.
        /// </summary>
        private static void Score(int scoreLeft, GameScoreNode root, int[] scores)
        {
            //for each score 1, 2, 3, subtract it from current game score by adding a new node
            for (int i = 0; i < scores.Length; i++)
            {
                int nextScore = scoreLeft - scores[i];

                //if there's score to be consumed, add a new node and recurse
                if (nextScore >= 0)
                {
                    GameScoreNode newPointNode = new GameScoreNode(scores[i]);
                    Score(nextScore, newPointNode, scores);
                    root.Children.Add(newPointNode);
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Find the leaves of a the tree and prints the path.
        /// </summary>
        private static void FindLeafAndPrint(int scoreLeft, GameScoreNode root, Stack <int> scoreStack)
        {
            //pushes the node value to the stack
            scoreStack.Push(root.ValueSubtracted);

            int newScore = scoreLeft - root.ValueSubtracted;

            //found a leaf? (root.Children is empty)
            if (newScore == 0)
            {
                PrintStack(scoreStack);
            }
            else
            {
                //not a leaf yet, so keep recursing
                foreach (GameScoreNode node in root.Children)
                {
                    FindLeafAndPrint(newScore, node, scoreStack);
                }
            }

            //remove current score from the stack when returning
            scoreStack.Pop();
        }
Exemple #5
0
        /// <summary>
        /// Recursivelly adds scores changes to a root node.
        /// Assumes root is non-null.
        /// </summary>
        private static void Score(int scoreLeft, GameScoreNode root, int[] scores)
        {
            //for each score 1, 2, 3, subtract it from current game score by adding a new node
            for (int i = 0; i < scores.Length; i++)
            {
                int nextScore = scoreLeft - scores[i];

                //if there's score to be consumed, add a new node and recurse
                if (nextScore >= 0)
                {
                    GameScoreNode newPointNode = new GameScoreNode(scores[i]);
                    Score(nextScore, newPointNode, scores);
                    root.Children.Add(newPointNode);
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Find the leaves of a the tree and prints the path.
        /// </summary>
        private static void FindLeafAndPrint(int scoreLeft, GameScoreNode root, Stack<int> scoreStack)
        {
            //pushes the node value to the stack
            scoreStack.Push(root.ValueSubtracted);

            int newScore = scoreLeft - root.ValueSubtracted;

            //found a leaf? (root.Children is empty)
            if (newScore == 0)
            {
                PrintStack(scoreStack);
            }
            else
            {
                //not a leaf yet, so keep recursing
                foreach (GameScoreNode node in root.Children)
                {
                    FindLeafAndPrint(newScore, node, scoreStack);
                }
            }

            //remove current score from the stack when returning
            scoreStack.Pop();
        }
Exemple #7
0
        /// <summary>
        /// Prints the tree by using a stack and keeping track of the score left
        /// </summary>
        public static void PrintScoreTree(int totalScore, GameScoreNode root)
        {
            Stack<int> scoreStack = new Stack<int>();

            //skips the root (zero) and recurse on each children
            foreach (GameScoreNode node in root.Children)
            {
                FindLeafAndPrint(totalScore, node, scoreStack);
            }
        }
Exemple #8
0
        /// <summary>
        /// Builds a n-ary tree with game score possibilities.
        /// </summary>
        /// <param name="totalScore">Total game points.</param>
        /// <param name="scores">List of possible scores</param>
        public static GameScoreNode BuildScoreTree(int totalScore, int[] scores)
        {
            //root has a zero as the subtracted node
            GameScoreNode root = new GameScoreNode(0);

            //starts recursion
            Score(totalScore, root, scores);

            return root;
        }
Exemple #9
0
        public static void Test()
        {
            GameScoreNode root = BuildScoreTree(10, new int[] { 1, 2, 3 });

            PrintScoreTree(10, root);
        }