Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Automatic Solver

Between the 2 methods below it should solve a Sudoku puzzle automatically,
Expected output is this : http://moritz.faui2k3.org/images/sudoku/default.png
My output the same but only with the number one.

row and col both = 0 when starting
```public boolean solve(int row, int col, char[][] board) {
if (row == 9)
{
row = 0;
col++;
}
if ((col) == 9)
{
return true;                //Base case - puzzle solved.
}
if(board[row][col] =='1' || board[row][col] =='2' || board[row][col] =='3' || board[row][col] =='4'
|| board[row][col] =='5' |board[row][col] =='6' ||
board[row][col] =='7' ||board[row][col] =='8' ||board[row][col] =='9') // skip filled cells
{

solve(row+1,col,board);   		    //recursive call
}

for(int val = 1; val <10;val++)
{
if (valid(row,col,val,board))  //checks if value is legal
{
Object input = val;
String s = input.toString().trim();

board[row][col] = s.charAt(0);;
fireTableCellUpdated(row, col);
}
if (solve(row+1,col,board))     //call solve starting at next cell
{
return true;
}

}

board[row][col] = '0'; // reset on backtrack
return false;

}```

```public Boolean valid(int row, int col, int val, char[][] board) {

Object input = val;
String s = input.toString().trim();
//Check Rows

for(int i = 0; i <9; i++)
{

//Check Rows
if(  s.charAt(0) == board[i][col])
{
return false;
}
//Check Cols
if (s.charAt(0) == board[row][i])
{
return false;
}
}

//check box
int boxRowOffset = (row / 3)*3;
int boxColOffset = (col / 3)*3;
for (int k = 0; k < 3; k++) // box
for (int m = 0; m < 3; m++)
{
{
if (boxRowOffset+k!=row && boxColOffset+m!=col)
if (s.charAt(0) == board[boxRowOffset+k][boxColOffset+m])
{{
return false;
}}
}
}

return true; // no violations, so it's legal
}```

2. ## Re: Automatic Solver

Do you have a question?

3. ## Re: Automatic Solver

Originally Posted by Norm
Do you have a question?
Can anyone see why my output wont match the correct output

4. ## Re: Automatic Solver

Got it. Finally.