Possible Duplicate:
Sudoku algorithm, brute force
For several days I have tried to write a brute force algorithm for solving sudoku, my problem is that I never realy get the algorithm to work 100 %, can someone please direct me and give some help ?
The Algorithm is located in Square class, recursive function.
public abstract class Square {
private Square next;
private Box box;
private Row row;
private Columne columne;
private int value;
Square(int value, Box box, Row row, Columne columne) {
this.value = value;
this.box = box;
this.row = row;
this.columne = columne;
}
void setNumberMeAndTheRest(Board board) {
if(getNext() == null) {
System.out.println("next == null");
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
}
}
board.saveSolution();
return;
} else {
if(this instanceof DefinedSquare) {
getNext().setNumberMeAndTheRest(board);
} else {
for(int i = 1; i <= board.getDimension(); i++) {
if(legalValue(i)) {
setValue(i);
getNext().setNumberMeAndTheRest(board);
}
}
return;
}
}
}
int getValue() {
return value;
}
void setValue(int value) {
this.value = value;
}
void setNext(Square next) {
this.next = next;
}
public Square getNext() {
return next;
}
/**
* Checks if value is legal in box, row and column.
* @param value to check.
* @return true if value is legal, else false.
*/
boolean legalValue(int value) {
if(box.legalValue(value) && row.legalValue(value) && columne.legalValue(value)) {
return true;
}
return false;
}