| bobarman
| Joined: 30 Dec 2006 | Posts: 1 | : | | Items |
|
Posted: Sat Dec 30, 2006 4:29 pm Post subject: A problem with my program which "try" :-) to gener |
|
|
Hello,
I try to make a program which generates full sudoku matrix. I get a random position in box(I tell "box" to the litlle squares with 9 elements) from 1 to 9 and put in that position the digits from 1 to 9, but it doesn't work properly.So, I know that the worst thing is to verify some other code, but please somebody help me to find where is my logical error.
This is my sorce code write on PHP:
Code: | <?php
$boxapspos=array();
$boxapspos[0]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[1]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[2]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[3]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[4]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[5]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[6]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[7]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxapspos[8]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for($i=0;$i<9;$i++){
$boxnumpos=array();
$boxnumpos[0]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[1]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[2]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[3]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[4]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[5]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[6]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[7]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$boxnumpos[8]=array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
for($j=0;$j<9;$j++){
$num=$i+1; // the digit
$indexran = 0;
for($br1=0; $br1<$boxapspos[$j][9]; $br1++) {
for($br2=0; $br2<$boxnumpos[$j][9]; $br2++) {
if($boxapspos[$j][$br1] == $boxnumpos[$j][$br2]) {
$ran[$indexran] = $boxapspos[$j][$br1];
$indexran++;
}
}
}
$index = rand(0,$indexran-1);
$numpos = $ran[$index];
$box[$j][$numpos]=$num;
for ($br3=0; $br3<$boxapspos[$j][9]; $br3++) {
if ($boxapspos[$j][$br3] == $numpos) {
$t = $boxapspos[$j][$br3];
$boxapspos[$j][$br3] = $boxapspos[$j][$boxapspos[$j][9]-1];
$boxapspos[$j][$boxapspos[$j][9]-1] = $t;
$boxapspos[$j][9]--;
}
}
$ostr=$numpos/3;
settype($ostr,"integer");
$ostc=$numpos%3;
$indrb=$j;
while($indrb%3 != 2){
for($br4=0; $br4<$boxnumpos[$indrb+1][9]; $br4++){
$ost=$boxnumpos[$indrb+1][9]/3;
settype($ost,"integer");
if ($ost == $ostr) {
$t = $boxnumpos[$indrb+1][$br4];
$boxnumpos[$indrb+1][$br4] = $boxnumpos[$indrb+1][$boxnumpos[$indrb+1][9]-1];
$boxnumpos[$indrb+1][$boxnumpos[$indrb+1][9]-1] = $t;
$boxnumpos[$indrb+1][9]--;
}
}
$indrb++;
}
$indcb=$j;
while($indcb < 6){
for($br5=0; $br5<$boxnumpos[$indcb+3][9]; $br5++){
$ost=$boxnumpos[$indcb+3][9]%3;
if ($ost == $ostc) {
$t = $boxnumpos[$indcb+3][$br5];
$boxnumpos[$indcb+3][$br5] = $boxnumpos[$indcb+3][$boxnumpos[$indcb+3][9]-1];
$boxnumpos[$indcb+3][$boxnumpos[$indcb+3][9]-1] = $t;
$boxnumpos[$indcb+3][9]--;
}
}
$indcb+=3;
}
}
}
for($p=0;$p<9;$p++){echo("<br>");
for($q=0;$q<9> |
Thanks. |
|