Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>your problem is in this function you were doing</p> <pre><code> public static boolean isinsq(int sr, int sc, int x) { for ( sr = 0; sr &lt; 3; sr++) { // ^ you are reseting the value of sr that you pass in // effectivel making it so that you always check the first square for ( sc = 0; sc &lt; 3; sc++) { // ^ same here if (sud[sr][sc] == x) { return true; } } } return false; } </code></pre> <p>this was resetting sr back to 0 so you only ever checked the top left corner not the coordinates that you were passing in. you should have done something like:</p> <pre><code>public static boolean isinsq(int xcorner, int ycorner, int x) { for (int sr = xcorner; sr &lt; 3; sr++) { //^ here w create a new variable with the starting value that you passed in for ( int sc = ycorner; sc &lt; 3; sc++) { //^ here w create a new variable with the starting value that you passed in if (sud[sr][sc] == x) { return true; } } } return false; } </code></pre> <p>as an asside your Double nested for loop in solve is unnecessary and adds a bunch of overhead. Instead of looking for the next open spot why not assume they are all open and check to see if they aren't this way your recursion will take care of that iterating for you. Consider something like this (its also more simple....at least to me)</p> <pre><code>bool solve(int[][] sud, int x, int y) { //here need to make sure that when x&gt;9 we set x to 0 and increment y //also if x == 9 and y == 9 need to take care of end condition if(sud[x][y]==0) //not a pre given value so we are ok to change it { for(int i =1; i&lt;10; ++i) { sud[x][y] = i; if(validBoard(sud)) //no point in recursing further if the current board isnt valid { if(solve(sud, x+1,y)) { return true; } } } } else { return solve(x+1,y); } return false; } </code></pre> <p>I left some areas that need filling (where's the fun in having me do it for you :). And as everyone else has suggested you should use more meaningful variable names.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload