Exemple #1
0
    /**
     * Checks if a combo exists in this numberList.
     * @param startIndex the index to check from
     * @param target the target to reach
     * @return true if combo exists, false otherwise.
     */
    public bool CheckForComboAt(int startIndex, int target)
    {
        RefreshArray();
        // SumList objects store a sum and the index they're found on.
        SumList sumListLeft = new SumList(), sumListRight = new SumList();

        sumListLeft.Add(0, -1);
        sumListRight.Add(0, -1);

        /**
         * Disregard all the numbers that cannot be part of the combo and return
         * the shorter array with numbers that can.
         */
        ShortList sl = FindShortList(startIndex, target);

        NumberNode[] shortList = sl.array;

        // Set the index to start counting from for counting towards
        // left and counting towards right.
        int leftStartIndex  = sl.GetStartIndex();
        int rightStartIndex = sl.GetStartIndex() + 1;

        // Create a temporary sum.
        int sum = 0;

        // Go over all numbers in the short list left of (and including) the start index
        for (int i = leftStartIndex; i > -1; i--)
        {
            // Increase the sum by the current value
            sum += shortList[i].value;
            // Store the sum and at what index in the shortList the sum occured
            sumListLeft.Add(sum, i);
        }
        // Reset the temporary sum.
        sum = 0;
        // Go over all numbers in the short list right of (and excluding) the start index
        for (int i = rightStartIndex; i < shortList.Length; i++)
        {
            // Increase the sum by the current value
            sum += shortList[i].value;
            // Store the sum and at what index in the shortList the sum occured
            sumListRight.Add(sum, i);
        }

        // For both sumlists...
        for (int sumListLeftIndex = 0; sumListLeftIndex < sumListLeft.sumList.Count; sumListLeftIndex++)
        {
            for (int sumListRightIndex = 0; sumListRightIndex < sumListRight.sumList.Count; sumListRightIndex++)
            {
                // Get the value of the currently selected sums added together
                int current_sum = sumListLeft.sumList[sumListLeftIndex] + sumListRight.sumList[sumListRightIndex];

                if (current_sum == target)
                {
                    // Found a combo!
                    // Kill all nodes that make up this combination.
                    for (int k = 0; k < sumListLeftIndex + 1; k++)
                    {
                        int index = sumListLeft.indexList[k];
                        if (index > -1)
                        {
                            shortList[index].Kill();
                        }
                    }
                    for (int k = 0; k < sumListRightIndex + 1; k++)
                    {
                        int index = sumListRight.indexList[k];
                        if (index > -1)
                        {
                            shortList[index].Kill();
                        }
                    }
                    return(true);
                }
                // If the combined sum is above it's target, no combo can be found, so
                // stop looking any further.
                else if (current_sum > target)
                {
                    return(false);
                }
            }
        }
        return(false);
    }