Numbers moving that shouldnt be

• December 6th, 2011, 07:11 AM
ppme
Numbers moving that shouldnt be
This code solves a Sudoku puzzle with no starting values perfectly..
But if i then in some values to start with it moves them around to suit itself which cannot happen.
Can anybody see the problem in the code? because it is frying my brain!

Code :

```public boolean solve(int row, int col) {   if (row == 9) { row = 0; col++; } if (col == 9 ) { return true; //Base case - puzzle solved. } Object checkHelp = board[row][col]; String s = checkHelp.toString().trim(); if(s.equals('1') ||s.equals('2') ||s.equals('3') ||s.equals('4') ||s.equals('5') || s.equals('6') ||s.equals('7') ||s.equals('8') ||s.equals('9') ) // skip filled cells {   solve(row+1,col); //recursive call }   for(int val = 1; val <10;val++) { if (valid(row,col,val,board)) //checks if value is legal { Object input = val; String z = input.toString().trim(); board[row][col] = z.charAt(0);; fireTableCellUpdated(row, col);     if (solve(row+1,col)) //call solve starting at next cell { return true; } } }   board[row][col] = '0'; // reset on backtrack return false;       }```
• December 6th, 2011, 07:16 AM
Norm
Re: Numbers moving that shouldnt be
Quote:

t moves them around to suit itself
Try debugging your code by adding printlns to show the values of variables as they are changed by the program. Be sure to print out enough information do show what the code is doing and what are the values of the variables it is using.
• December 6th, 2011, 07:19 AM
ppme
Re: Numbers moving that shouldnt be
Quote:

Originally Posted by Norm
Try debugging your code by adding printlns to show the values of variables as they are changed by the program. Be sure to print out enough information do show what the code is doing and what are the values of the variables it is using.

Because this is a recursive method debugging with printlns is an extremely difficult thing to do as the console window just gets flooded with printlns, I was hoping someone else on here could possibly see some silly mistake I am making
• December 6th, 2011, 08:29 AM
Norm
Re: Numbers moving that shouldnt be
Sometimes that's what happens. There could be lots of printlns and the answer to your problem will be in those print outs.
If there is one particular content of a square that gets moved, use if statements to control the printlns so that they only print when that content is being set/changed/moved.
• December 6th, 2011, 09:28 AM
ppme
Re: Numbers moving that shouldnt be
Okay.. using printlns this part of my code NEVER executes

Object checkHelp = board[row][col];
String s = checkHelp.toString().trim();
if(s.equals('1') ||s.equals('2') ||s.equals('3') ||s.equals('4') ||s.equals('5') ||
s.equals('6') ||s.equals('7') ||s.equals('8') ||s.equals('9') ) // skip filled cells
{
System.out.println("Skipped Number");
solve(row+1,col); //recursive call
}

anyone? :(
• December 6th, 2011, 09:32 AM
Norm
Re: Numbers moving that shouldnt be
What statements are before the code you posted that prevents the posted code from executing?
What are the values of the variables that are tested?

Did you test your code? See what this statement does:
System.out.println("1".equals('1')); // false
• December 6th, 2011, 07:44 PM
Herah
Re: Numbers moving that shouldnt be
You are using string comparison to compare chars.

(s.equals('1')) should be (s.equals("1)) etc. Right now, it's false.