I am in the process of writing a sudoku solver (still need to write the box check and actually complete the program) but I’m testing it as I know. The puzzle I’m testing right now is “very easy” as in there is only one empty cell in any row/column. THe puzzle starts with “empty” cells as zeros. My issues is that when I run the program and print out the puzzle after solve() is called, the zeros aren’t changing and the original puzzle is just printed out. Not sure what my issue is, would appreciate some direction!
public ArrayList<Integer> create(){
ArrayList<Integer> possible = new ArrayList<Integer>();
for(int i=1; i<10; i++){
possible.add(i);
}
return possible;
}
public sudoku( int size )
{
SIZE = size;
N = size*size;
Grid = new int[N][N];
for( int i = 0; i < N; i++ )
for( int j = 0; j < N; j++ )
Grid[i][j] = 0;
}
public void solve()
{
int a, b, c, d, i, j, k, l;
int count = 0;
int value= 0;
for(i=0; i<N;i++){
for(j=0; j<N;j++){
if(Grid[i][j]==0){
ArrayList<Integer> possible = create();
//check row
for(a=0; a<N;a++){
for(b=0; b<N; b++){
if(Grid[a][0]==possible.get(a)){
possible.set(a, 0);
}
}
}
//check column
for(c=0; c<N;c++){
for(d=0; d<N;d++){
if(Grid[0][d]==possible.get(d)){
possible.set(d,0);
}
}
}
for(k=0; k<9; k++){
if(possible.get(k)!=0){
count++;
}
}
if(count==1){
for(l=0; l<9; l++){
if(possible.get(l)!=0){
value=possible.get(l);
}
}
}
Grid[i][j]=value;
}
}
}
}
Look at your line
if(Grid[a][0]==possible.get(a))(and similar spots). What is it doing there vs what do you actually want?Your possible array looks something like this:
[1,2,3,4,5,6,7,8,9]and your grid (just the first row, since you’re only checking Grid[a][0]) might look something like this:
[3,7,8,1,2,9,5,0,4]Your loop is looking at each element stepwise individually and seeing if they’re equal, like this:
… etc
So, as you can see, when you do your
Your possible array is still going to be full of options most of the time, unless your first row happens to be some variation on
[1,2,3,4,5,6,7,8,9]with a 0 in one of the spaces… so count is definitely going to be > 1So, your next loop (
for(l=0; l<9; l++)) next gets executed, so value is still (as you initialized it) 0.Try stepping through a debugger on these points and seeing how the arrays are interacting.