I need to prepare sudoku, I made my code and everything worked fine but then I had to add interfaces and now I have a problem because once it works and once not. There is a problem with arrayList I guess.
public class FillSudokuSolver implements SudokuSolver {
public int solve(int index, SudokuBoard tab){
boolean check = true;
while(check){
fill(index,tab);
if(tab.getDigit(8,8)!=0){
check = false;
return 0;
}
else{
tab.setBoard();
index=0;
}
}
return 0;
}
public int fill(int index, SudokuBoard tab){
if(index>80)
return 0;
int x = index/9;
int y = index%9;
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++)
numbers.add(i);
Collections.shuffle(numbers);
if(tab.getDigit(x,y) != 0){
solve(index + 1,tab);
}
else
{
int number = tab.getNextPossibleNumber(x, y, numbers);
if (number == -1)
return 0;
tab.setDigit(x,y,number);
solve(index + 1,tab);
}
return 0;
}
public int getNextPossibleNumber(int x, int y, List<Integer> numbers){
while (numbers.size() > 0){
int number = numbers.remove(0);
if (isPossibleX(x, y, number) && isPossibleY(x,y, number)
&& isPossibleBlock(x, y, number)){
return number;
}
}
return -1;
}
And the error looks like :
Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.remove(ArrayList.java:474)
at qrczak.sudoku.SudokuBoard.getNextPossibleNumber(SudokuBoard.java:76)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:39)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:45)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:45)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)
I think that mistkake occure somewhere in that part of the code, but to be honest I was looking for it for 2 days and i can not find it. Almost the same code but without interfaces works fine !