Sudoku solution generator algorithm
I'm making a sudoku game using the win32 api atm, but for some reason, I can't seem to get my solution generator method to work as it should.
The way that I'm doing it, is like this (it's not the most efficient way of doing it, but atm, thats not my main concern):
It cycles through the smaller 3x3 grids one by one, randomly selecting an x and y coordinate, and provided that neither the column or the row it selects already has that number, and the square is empty, it places the number there. It first cycles through and places all the 1s, then all the 2s etc. The problem is, I'm still ending up with problems. This is my source code so far (all variables init to zero)...
The temp_x and temp_y variables are used to keep track which columns and rows there are already values in. They are reset once the next number to place on the board is selected. I end up with some strange program output from this. although I can't find the bug anywhere.
for(idx0=1; idx0<NUM_BUTTONS; idx0++)
for(idx1=0; idx1<(BUTTON_NUM_Y/3); idx1++)
for(idx2=0; idx2<(BUTTON_NUM_X/3); idx2++)
/* Generate x and y values, making sure not to have two in the same row or column */
num1 = rand()%3 + (3*idx2);
num2 = rand()%3 + (3*idx1);
} while( (this->solution[num1][num2] != 0)&&((temp_x[num1] == 1)||(temp_y[num2] == 1)) );
/* Change the index for future numbers */
temp_x[num1] = 1;
temp_y[num2] = 1;
/* If it gets this far, it's a good value, so record it */
this->solution[num1][num2] = idx0;
/* Reset the temporary array */
for(idx3=0; idx3<(NUM_BUTTONS - 1); idx3++)
temp_x[idx3] = 0;
temp_y[idx3] = 0;