Note: this problem has been solved, the actual problem is NOT in this method but the other, so if you're searching for something about Sudoku and finally get into this page, you can absolutely use my method below, it works.
Ok, forget about all the complex algorithms used to solve Sudoku. I'm writing a simple solver on Java to solve simple Sudoku games. The idea of this method is very common, so I think everyone knows it already. I'm also surprised that I can't get it done.
The method is to go over every cell on the board and fill in all the cells that have only 1 possibility. Repeat until every cell is filled. Very simple, and here is my code, return int number of fillings can be made:
public int solveGame() {
/*
variable possible contains 10 elements, the first element is true if there
is one or more possible value to fill in, false otherwise. The remaining
elements (1-9) are whether true or false depending on their indexes
e.g. possible[3] is true if 3 is a possibility.
*/
boolean[] possible;
int[] save;
int count;
int numresolve = 0;
while (!isFinished()) {
for (int i = 0; i < GAMESIZE; i++) {
for (int j = 0; j < GAMESIZE; j++) {
possible = new boolean[10];
possible = getPossible(i,j);
if (possible[0]) {
count = 0;
save = new int[9];
for (int k = 1; k < 10; k++) {
if (possible[k]) {
count++;
save[count] = k;
}
}
if (count == 1) {
setCell(i,j,save[count]);
numresolve++;
}
}
}
}
}
return numresolve;
}
The problem of my code is that it can never finish the loop because after filling all the cells that have 1 possibility, the remaining cells will have more than 1 possibility, which is impossible for the loop to be finished.
I know I'm missing something that I can't think of.