# Automatic Solver

• December 5th, 2011, 02:57 PM
ppme
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
Code :

```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;       }```

Code :

```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 }```
• December 5th, 2011, 03:11 PM
Norm
Re: Automatic Solver
Do you have a question?
• December 5th, 2011, 03:13 PM
ppme
Re: Automatic Solver
Quote:

Originally Posted by Norm
Do you have a question?

Can anyone see why my output wont match the correct output
• December 5th, 2011, 03:15 PM
ppme
Re: Automatic Solver
Got it. Finally.