Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4050858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:07:19+00:00 2026-05-20T14:07:19+00:00

thats my first question here and i hope someone can help me. I´m new

  • 0

thats my first question here and i hope someone can help me.
I´m new at the iPhone programming and want to try an easy app…
It´s an SudokuSolver which is working with an recursive Method. In JAVA this code is making no problems, but in Objective-C the code isn´t stopping when Sudoku is solved. It´s still trying to solve the Sudoku and stops later.

Anyone an idea?!

Here´s the code.

- (SudokuSolver *) initWithField: (int[9][9]) field {
self = [super init];
if(self) {
    for (int i=0; i<9; i++) {
        for (int j=0; j<9; j++) {
            sudokuField[i][j] = field[i][j];
            if (field[i][j]) {
                sudokuFieldStatic[i][j] = 1;
            } else {
                sudokuFieldStatic[i][j] = 0;
            }
        }
    }
}
return self;
}

- (void) solve {
   [self solveFieldAtRow:0 andCol:0];
}

- (void) solveFieldAtRow: (int) row andCol: (int) col {
  if (row > 8) {
    return;
  } else {
    while (sudokuField[row][col] != 0) {
        if (++col > 8) {
            col = 0;
            row++;
            if (row > 8) {
                return;
            }
        }
    }
    for (int num=1; num<10; num++) {
        if ([self checkRow:row forNumber:num] && [self checkCol:col forNumber:num] && [self checkFieldAtRow:row andCol:col forNumber:num]) {
            sudokuField[row][col] = num;
            [self showFieldInConsole:0];
            if (col < 8) {
                [self solveFieldAtRow:row andCol:col+1];
            } else {
                [self solveFieldAtRow:row+1 andCol:0];
            }
        }
    }
    sudokuField[row][col] = 0;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:07:19+00:00Added an answer on May 20, 2026 at 2:07 pm

    The code isn’t stopping when the puzzle is solved because you don’t check whether the puzzle is solved after the recursive call. So even if the recursive call found a solution, the code just continues on even after finding a solution until it has tried every possibility.

    Since you say you have Java code that works, I suggest you compare the logic of the Java program versus this code. You’ll probably find the Java code does include such a test.


    Edit From your comment above, I see that you won’t find such a test in your Java code, because there you are abusing exceptions to “return” from the recursion when a solution is found. The proper way is to have each recursive call return a true value if it found a solution and false if it didn’t. And then each step should check if its child call succeeded, and itself return success if so. Something like this:

    - (BOOL) solveFieldAtRow: (int) row andCol: (int) col {
        if (row > 8) {
            // reached the end, so it must have succeeded
            return YES;
        } else {
            while (sudokuField[row][col] != 0) {
                if (++col > 8) {
                    col = 0;
                    row++;
                    if (row > 8) {
                        // reached the end, so it must have succeeded
                        return YES;
                    }
                }
            }
            for (int num=1; num<10; num++) {
                if ([self checkRow:row forNumber:num] && [self checkCol:col forNumber:num] && [self checkFieldAtRow:row andCol:col forNumber:num]) {
                    sudokuField[row][col] = num;
                    [self showFieldInConsole:0];
                    BOOL result;
                    if (col < 8) {
                        result = [self solveFieldAtRow:row andCol:col+1];
                    } else {
                        result = [self solveFieldAtRow:row+1 andCol:0];
                    }
                    if (result) {
                        // Our child call succeeded, so we pass that back up
                        // the stack.
                        return YES;
                    }
                }
            }
            sudokuField[row][col] = 0;
            // If we get here, we could not find a solution. Return failure
            // back up the stack.
            return NO;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this my first question on here. I hope someone can help. It is to
First time I'm writing something here. I have a question that i hope someone
hope someone of you can help me. I'm new to mac and qt, so
this is my first question here so I hope I can articulate it well
This is my first question here, so I hope that I am providing enough
Since this is my first question here on stackoverflow I hope my question is
I have another beginner's question that hopefully someone can help with. I'm trying to
This is my first question here, so I hope everything is correct. I've hacked
my first question here! I have a problem with a piece of code that
First question here: it is a very short yet fundamental thing in Java that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.