win2000 , visual studio 2005, console application
(sorry my poor english)
I dont know very well how start explain whats my problem..
Im making a sudoku game (not unic solution, just random =P)
the problem is the loop where it suppose to verify the sudoku rules and make the corrections necessary, are working 'fine'(?), but it doesnt finish mostly times ( wich means it works perfectly some times =/ )
Another weird thing, is that it are working just like i want to( i figure that cause i debugged this loop one million times, step by step, and didnt find any wrong stuff), so you think, LOGICAL error, yeah i agree, but if the problem is logical, why it works some times, and more weird:
if i comment the loop in parts( there are 3 parts, the blocks( the nine main blocks in sudoku grid), the horizontals lines, and the verticals lines), if i comment 2 of them( letting just one), it will works perfectly, generating a sudoku just with one of its rules...(and if i let 2 rules, it works more times( not making the loop forever)
i cant find the logical problem..
so let me explain my algorithm:
- sort 81 random numbers ( 0-9) and put them in a vector (int positions_fix[81])
- verify and correct errors loop:
while verification is not donne:
verification donne(this is a bool)
in block rules
block1 2 3 4 5 6 7 8 9(each block have its own loop)
if it find a error, it will fix it(changing the number by a right one, and verifing all again.
when it finishes, there are no errors in the block rules
verify horizontals
(verify all lines) if it find a error, it will fix it(changing the number by a right one, and verifing all again,and marking verification donne as false(cause the block rules are no more safe))
when it finishes, horizontals are free of error
verify verticals( the same as the horizontals)
end of loop
-show numbers
As you see, its very simple, and i SUPPOSE it should work IF IMPLEMENTED RIGHT...right? Cause it should verify until no more errors are find...wich happens some times...
i dont have a clue about what to do, i tried so many things that my brain got burned
So, yeah the code...this loop is kinda long, so if you think theres a problem on my algorythm, gimmie a help...if theres no way to find out whats the problem, im attaching a cpp file with the loop stuff , im also posting here parts of the loop ,
here is:
(obs: +10 are for fixed numbers(the start numbers on a sudoku game, but now all numbers are being fixed for test, so all 81 numbers are fixed (10-19)
here is where are all the numbers to be sorted and corrected
int positions_fix[81];//81 positions(fix numbers(+10))
// 0 1 2 3 4 5 6 7 8
// 9 10 11 12 13 14 15 16 17
// 18 19 20 21 22 23 24 25 26
// 27 28 29 30 31 32 33 34 35
// 36 37 38 39 40 41 42 43 44
// 45 46 47 48 49 50 51 52 53
// 54 55 56 57 58 59 60 61 62
// 63 64 65 66 67 68 69 70 71
// 72 73 74 75 76 77 78 79 80
how is the loop for the blocks rules:
//the loop
//block1 0 1 2
// 9 10 11
// 18 19 20
for(int x=0, aux_x=0; x<=20; x++){
for(int y=0, aux_y=0; y<=20; y++){
if(positions_fix[x]!=NULL){
if(positions_fix[x]>10&&positions_fix[y]>10&&x!=y){//both>10 //1
while(positions_fix[x]==positions_fix[y]&&x!=y){
positions_fix[x]=((rand()%9)+1)+10;
y=0;
aux_y=0;
}//F while
}//F if both >10
}//F if != NULL
aux_y++;
if(aux_y==3){y=y+6; aux_y=0;}//new line in the block(+1 cause for ++, so +7)
}//F for y
aux_x++;
if(aux_x==3){x=x+6; aux_x=0;}//each ciclo de 0 à 2
}//F for x
the horizontals loop
//horizontals
int linha=0;//what line are
int flinha=8;//where the line ends
int end_y_at=8;
int start_y_from=0;
int aux_y=0;
for(int start_x_from=0, aux_x=0; start_x_from<=80; start_x_from++){
for(NULL; aux_y<=end_y_at; start_y_from++){
if(positions_fix[start_x_from]!=NULL){
if(positions_fix[start_x_from]>10&&positions_fix[start_y_from]>10&&start_x_from!=start_y_from){//both>10 //1
while(positions_fix[start_x_from]==positions_fix[start_y_from]&&start_x_from!=start_y_from){
positions_fix[start_x_from]=((rand()%9)+1)+10;
start_y_from=linha;
aux_y=linha;
verification_donne=false;//numbers changed, verific blocks again
}//F while
}//F if both >10
}//F if != NULL
aux_y++;
}//F for y
aux_x++;
if(aux_x==9){
start_y_from=linha;
end_y_at=end_y_at+9;
linha=linha+9;
aux_y=aux_y+9;
flinha=flinha+9;
aux_x=0;
}//F if aux_x==9&&
else {
start_y_from=linha;
aux_y=linha;
end_y_at=flinha;
}
}//F for aux_x
//verticals
for( int x=0, y=72; x<=8,y<=80; x++, y++){//x= start line at/ y= end line at
for( int z=x; z<=y; z=z+9){
for( int w=x; w<=y; w=w+9){
if(positions_fix[z]!=NULL){
if(positions_fix[z]>10&&positions_fix[w]>10&&z!=w){//both>10 //1
while(positions_fix[z]==positions_fix[w]&&z!=w){
positions_fix[z]=((rand()%9)+1)+10;
w=x;
verification_donne=false;
}//F while [z]==[y]
}//F if both>10
}//F if == NULL
}//F for x
}//F for z
}//F for x
remembering, each of these works alone, just 2 of these works a little(with means sometimes it finishes ok, and sometimes never finish), and all of these almost doesnt work..
another important thing, is that when it DOESNT finish, it keep doing the entire loop( it keeps doing all verification rules, it dont get stucked in a specific point(never))
also, if i let less numbers in the grid(not 81, like 20 numbers) it finishes ok...