views:

278

answers:

0

I'm trying to create a recursive method that moves throw a NSMutableArray, something is happening that the code stays inside the loop or makes it only once.

Here is my code:

- (NSMutableArray* )solve:(NSMutableArray* )temp{

    //just a counter
    int count =0;
    int index =0;

    if (count==0)
    {
        //Calculates the different options for the numbers that can be placed in each cell, the information in stores inside a Cell object as a boolean array
        temp = [self calculatePosibilities2:temp];

        //Calculates the sum of options available for placing the numbers over each cell.
        temp = [self calculateSortIndex2:temp];

        //sorts the array using the sortIndex
        [temp sortUsingSelector:@selector(compareSortIndex:)];

        //gets the specific "sortIndex" value from a specific cell
        index = [[temp objectAtIndex:0] getSortIndex];
    }

    //while the counter is smaller than the possibilities of numbers is a cell and the index is smaller than 20 (just to control the loop)
    while (count<=index && index<20)
    {    
        //Updates the amount of nodes that the process has been over
        nodos++;
        printf("%d,   %2.d\n",nodos,[[temp objectAtIndex:0]getSortIndex]);

        //Sets the number of a cell as the next posible value that obeys the sudoku rules (ascending order)
        [[temp objectAtIndex:0] setNextPossibleValue];

        //Sets the number just stored in the cell as a fixed value (so other methods dont change it)
        [[temp objectAtIndex:0] setPredefinedValue];

        //Checks if the sudoku is compleatelly filled. If that is the case, it finishes the loop.
        if ([self allCellsHaveValue:temp]) {
            [self saveArray:temp];
            return nil;
        }

        count++;
        [self solve:temp];
    }

    return nil;
}