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 8703381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:51:24+00:00 2026-06-13T02:51:24+00:00

I am working a sudoku solver and am having trouble returning / ending the

  • 0

I am working a sudoku solver and am having trouble returning / ending the solver function correctly. The show() in the moveOn() function gets called and it displays the compleated sudoku fine, however solve returns false. I am trying to have solve return true when the problem is solved and null when it is unsolveable however do not know how to accomplish this.

L is the length of the board (a 9 x 9 sudoku would have L = 9)

getSquare(r,c) returns the value in a 2 dimensional array representing the sudoku board

the different check functions check to see if a value can fit in a specific location. They are not the issue.

the show() function prints out the array in console so it looks like a proper sudoku board.

I also have an isSolved() function that checks the 2D array and if it is a valid solved sudoku returns true, otherweise returns false. I have attempted to implement this as well into the solve() method hoping to use that to return true, though have had no success

//This method's only purpose it to call the findNum function on the next location in the sudoku
public void moveOn(int row, int column) {
    //if the previous location was not the last in the row move to ne next cell in said row.
    //if it was the last location in the row, move to the first column of the next row
    if (column + 1 != L) {solve(row, column + 1);}
    else if (row + 1 != L) {solve(row + 1, 0);}
    else {show();}
}

//This method finds a valid number for a specific location on the sudoku grid\
public boolean solve(int row, int column) {
    if (row >= L) {return true;}
    //pass over any numbers that are not empty
    if (getSquare(row, column) != 0) {moveOn(row, column);}
    else {
        //attempt to find a valid number for the location
        for (int n = 1; n <= L; n++) {
            if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
                // If a number is allowed at a specific location set that location to the number
                setSquare(row, column, n);
                //Begin checking for a solution based on previous numbers changed           
                moveOn(row, column);
            }               
        }
        //If no number is allowed in this space backtrack to the last successful number 
        //changed and reset all locations that have been changed recursively
        setSquare(row, column, 0);          
    }
    //If the puzzle is unsolveable
    return false;
}

Many thanks to anybody that can help shed some light on the situation.
If more of my code / information is needed I will gladly provide

Sample input file: http://pastebin.com/6mSKT3ES

Edit: complete code removed

  • 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-06-13T02:51:24+00:00Added an answer on June 13, 2026 at 2:51 am

    You have only one return statement in the solve function, and that is

    return false;
    

    and since that is the last statement in the function, and unconditionally executed, solve will, unless an exception is thrown, always return false.

    To get a return value that actually tells you whether you found a solution, you need to make the return value depend on a condition. Also, once you have found a solution, for well-posed puzzles, there is no point in continuing to search.

    So you should add a conditional return true; in the searching loop. For that, you need to know when you have found a solution. You wrap the recursion in an intermediate call to moveOn, so the simplest change would be to add a return value to moveOn:

    public boolean moveOn(int row, int column) {
        //if the previous location was not the last in the row move to ne next cell in said row.
        //if it was the last location in the row, move to the first column of the next row
        if (column + 1 != L) {return solve(row, column + 1);}
        else if (row + 1 != L) {return solve(row + 1, 0);}
        else {show(); return true;}  // reached end of grid, solved
    }
    

    and use that in `solve’:

    public boolean solve(int row, int column) {
        //pass over any numbers that are not empty
        if (getSquare(row, column) != 0) {return moveOn(row, column);}
        else {
            //attempt to find a valid number for the location
            for (int n = 1; n <= L; n++) {
                if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
                    // If a number is allowed at a specific location set that location to the number
                    setSquare(row, column, n);
                    //Begin checking for a solution based on previous numbers changed           
                    if (moveOn(row, column)) {
                        return true;       // solved, yay!
                    }
                }               
            }
            //If no number is allowed in this space backtrack to the last successful number 
            //changed and reset all locations that have been changed recursively
            setSquare(row, column, 0);          
        }
        //If the puzzle is unsolveable
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Sudoku solver at school and we're having a little performance
I'm currently working on my GUI for this Sudoku solver I'm making. I've managed
I am trying to build a Sudoku solver for a project I am working
I'm working on a sudoko solver (python). my method is using a game tree
I've been trying to get this Sudoku game working, and I am still failing
Working with an undisclosed API, I found a function that can set the number
I'm currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional
So I'm working with a two-dimensional array of JTextFields for a Sudoku program. public
Working in a hackathon and we are having an issue with our phone mockup.
Hello Im working on a Sudoku Checker which verifies the completed board's solution is

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.