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
You have only one
returnstatement in thesolvefunction, and that isand since that is the last statement in the function, and unconditionally executed,
solvewill, unless an exception is thrown, always returnfalse.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 tomoveOn, so the simplest change would be to add a return value tomoveOn:and use that in `solve’: