I´m pretty sure that legalValue(int value) works, under is the code for the function:
boolean legalValue(int value) {
if(row.legalValue(value) && columne.legalValue(value) && box.legalValue(value)) {
return true;
}
return false;
}
After a long day I have finally got a step closer a perfectly working solution, the algorithm below works for some types of sudoku board, but not all.
It works for this 4 x 4 board.
0 0 | 1 0
0 3 | 0 4
----------
3 0 | 4 0
0 2 | 0 0
that has only one solution, namely
2 4 | 1 3
1 3 | 2 4
----------
3 1 | 4 2
4 2 | 3 1
On this 9 x 9 board that has 28 solutions throw out the algorithm, 1 wrong solution
0 0 1 | 0 0 3
0 0 0 | 0 0 0
--------------
0 0 0 | 0 2 0
2 6 0 | 0 0 0
--------------
0 0 0 | 3 0 0
3 0 0 | 1 0 2
the algorithm gives us
4 2 1 | 5 6 3
6 3 4 | 2 1 5
--------------
1 5 3 | 6 2 4
2 6 5 | 4 3 1
--------------
5 1 2 | 3 4 6
3 4 6 | 1 5 2
Her is the partially acting algorithm
boolean setNumberMeAndTheRest(Board board) {
if(next == null) {
for(int i = 1; i < board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
}
}
board.saveSolution();
System.out.println("Saved a solution");
return true;
}
if(this instanceof DefinedSquare) {
return next.setNumberMeAndTheRest(board);
}
for(int i = 1; i <= board.getDimension(); ++i) {
if(legalValue(i)) {
setValue(i);
if(next.setNumberMeAndTheRest(board)) {
return true;
}
}
}
setValue(0);
return false;
}
I need help with two thing:
- Make the algorithm to work for all dimension/ type of sudoko boards.
- Save all possible solutions for a board, or will this happen automatically when the algorithm works right, i saves a board "board.saveSolution();" when the algorithm finds the last value in the last square.
Appreciate the help