Where am I going wrong? This doesn’t seem to work. I’m trying to specify in the parameter an i and j which locates the cell. And returns the possible values in a 9 x 9 sudoku grid.
What this does it that the first array i.e a[0]= true if the cell is empty and false if its being played or occupied.
the rest of the array consists of values which are possible values that can be inserted into that i and j cell. therefore a possible value is 4 then it will return a[4]= true and the rest of the boolean array would be possible. can anyone tel me where I’m going wrong? Is the while loop incorrect?
public boolean[] getPossible( int i, int j)
{
final int booArraySize = 10;
boolean[] possibleArray = new boolean[booArraySize];
int zero = 0;
if ( game[i][j] == 0)
{
for( int b=1; b < possibleArray.length; b++)
{
possibleArray[b] = true;
}
int row=i;
int col= 0;
int[] copyy = new int[GRID_SIZE];
for( int m = 0; m < copyy.length; m++)
{
copyy[m] = 1;
}
while ( (copyy[0] < 10) && (copyy[0] >0))
{
for ( int q= col+1; q < game.length; q++)
{
if( copyy[0] == game[row][q])
{
possibleArray[q] = false;
}
else
{
possibleArray[q] = possibleArray[q];
}
}
copyy[0] = copyy[0] + 1;
}
possibleArray[0]= true;
}
return possibleArray;
}
As I understand it, you are trying to set
possibleArrayto betrueat positions corresponding to allowed values. You seem to be scanning the game board and doing some testing in order to decide when to set each index position totrue. I would suggest you reverse your logic: setpossibleArraytotruefor all values and then scan the board for values that would rule out particular values. You can do that by using the value of the game board at each position relevant to (i, j) as an index to setpossibleArraytofalse, as in this pseudocode:For empty game positions, this will set possibleArray[0] to false, but you fix that after the loop exits.
EDIT:
Here’s my attempt to deal with rows and columns simultaneously. It doesn’t deal with the 3×3 box containing position (i,j); that’s left as an exercise. 🙂
I assume (based on your code) that game[1][1] through game[GRID_SIZE][GRID_SIZE] are the right index range for the game board.