MikΣ: Sudoku: Funktioniert

Beitrag lesen

  
<?php  
  
  
// Zeilen  
  
$z[1] = array("-",0,0,0,0,0,0,0,0,0);  
$z[2] = array("-",0,0,0,0,0,0,0,0,0);  
$z[3] = array("-",0,0,0,0,0,0,0,0,0);  
$z[4] = array("-",0,0,0,0,0,0,0,0,0);  
$z[5] = array("-",0,0,0,0,0,0,0,0,0);  
$z[6] = array("-",0,0,0,0,0,0,0,0,0);  
$z[7] = array("-",0,0,0,0,0,0,0,0,0);  
$z[8] = array("-",0,0,0,0,0,0,0,0,0);  
$z[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
// Spalten  
  
$s[1] = array("-",0,0,0,0,0,0,0,0,0);  
$s[2] = array("-",0,0,0,0,0,0,0,0,0);  
$s[3] = array("-",0,0,0,0,0,0,0,0,0);  
$s[4] = array("-",0,0,0,0,0,0,0,0,0);  
$s[5] = array("-",0,0,0,0,0,0,0,0,0);  
$s[6] = array("-",0,0,0,0,0,0,0,0,0);  
$s[7] = array("-",0,0,0,0,0,0,0,0,0);  
$s[8] = array("-",0,0,0,0,0,0,0,0,0);  
$s[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
// Quadrate  
  
$q[1] = array("-",0,0,0,0,0,0,0,0,0);  
$q[2] = array("-",0,0,0,0,0,0,0,0,0);  
$q[3] = array("-",0,0,0,0,0,0,0,0,0);  
$q[4] = array("-",0,0,0,0,0,0,0,0,0);  
$q[5] = array("-",0,0,0,0,0,0,0,0,0);  
$q[6] = array("-",0,0,0,0,0,0,0,0,0);  
$q[7] = array("-",0,0,0,0,0,0,0,0,0);  
$q[8] = array("-",0,0,0,0,0,0,0,0,0);  
$q[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
// Fueller  
  
// 9 x fuer 9 Zeilen  
  
$count = 1;  
while ($count <= 9)  
 {  
  
// 9 x fuer 9 Spalten  
  
 $unterbrecher = 1;  
  
 $c=1;  
 while ($c <= 9)  
  {  
  
// Bestimmung des aktuellen Quadrates $qc  
// |---|---|---|  
// |-1-|-2-|-3-|  
// |---|---|---|  
// |-4-|-5-|-6-|  
// |---|---|---|  
// |-7-|-8-|-9-|  
// |---|---|---|  
  
  if ($count < 4)  
   {  
   if ($c < 4) $qc = 1;  
   if ($c < 7 && $c > 3) $qc = 2;  
   if ($c > 6) $qc = 3;  
   }  
  if ($count < 7 && $count > 3)  
   {  
   if ($c < 4) $qc = 4;  
   if ($c < 7 && $c > 3) $qc = 5;  
   if ($c > 6) $qc = 6;  
   }  
  if ($count > 6)  
   {  
   if ($c < 4) $qc = 7;  
   if ($c < 7 && $c > 3) $qc = 8;  
   if ($c > 6) $qc = 9;  
   }  
  
// Erzeugung der fortlaufenden Nummer $qSum  
// im Array für das aktuelle Quadrat ($q[x])  
// |---|  
// |123|  
// |456|  
// |789|  
// |---|  
  
  if ($count == 1 || $count == 4 || $count == 7) $qSumA = 0;  
  if ($count == 2 || $count == 5 || $count == 8) $qSumA = 3;  
  if ($count == 3 || $count == 6 || $count == 9) $qSumA = 6;  
  
  if ($c < 4) $qSumB = $c;  
  if ($c > 6) $qSumB = $c - 6;  
  if ($c > 3 && $c < 7) $qSumB = $c - 3;  
  
  $qSum = $qSumA + $qSumB;  
  
// Zufallszahl  
  
  srand((double)microtime()*1000000);  
  $a = rand(1,9);  
  
// wenn die nicht in akt. Zeile, Spalte, Quadrat vorhanden,  
// in akt. Zeile, Spalte, Quadrat zufuegen  
  
  if (!in_array($a, $z[$count]) && !in_array($a, $s[$c]) && !in_array($a, $q[$qc]))  
   {  
  
   $z[$count][$c] = $a;  
   $s[$c][$count] = $a;  
   $q[$qc][$qSum] = $a;  
  
   $c++;  
   }  
  
// nach 500 Versuchen des Zufuegens den ganzen Vorgang neu starten  
  
   else  
    {  
    $unterbrecher++;  
    if ($unterbrecher == 500)  
     {  
  
// Zeilen  
  
$z[1] = array("-",0,0,0,0,0,0,0,0,0);  
$z[2] = array("-",0,0,0,0,0,0,0,0,0);  
$z[3] = array("-",0,0,0,0,0,0,0,0,0);  
$z[4] = array("-",0,0,0,0,0,0,0,0,0);  
$z[5] = array("-",0,0,0,0,0,0,0,0,0);  
$z[6] = array("-",0,0,0,0,0,0,0,0,0);  
$z[7] = array("-",0,0,0,0,0,0,0,0,0);  
$z[8] = array("-",0,0,0,0,0,0,0,0,0);  
$z[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
// Spalten  
  
$s[1] = array("-",0,0,0,0,0,0,0,0,0);  
$s[2] = array("-",0,0,0,0,0,0,0,0,0);  
$s[3] = array("-",0,0,0,0,0,0,0,0,0);  
$s[4] = array("-",0,0,0,0,0,0,0,0,0);  
$s[5] = array("-",0,0,0,0,0,0,0,0,0);  
$s[6] = array("-",0,0,0,0,0,0,0,0,0);  
$s[7] = array("-",0,0,0,0,0,0,0,0,0);  
$s[8] = array("-",0,0,0,0,0,0,0,0,0);  
$s[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
// Quadrate  
  
$q[1] = array("-",0,0,0,0,0,0,0,0,0);  
$q[2] = array("-",0,0,0,0,0,0,0,0,0);  
$q[3] = array("-",0,0,0,0,0,0,0,0,0);  
$q[4] = array("-",0,0,0,0,0,0,0,0,0);  
$q[5] = array("-",0,0,0,0,0,0,0,0,0);  
$q[6] = array("-",0,0,0,0,0,0,0,0,0);  
$q[7] = array("-",0,0,0,0,0,0,0,0,0);  
$q[8] = array("-",0,0,0,0,0,0,0,0,0);  
$q[9] = array("-",0,0,0,0,0,0,0,0,0);  
  
$count=1;  
$c=1;  
     }  
    }  
  }  
  
 $count++;  
 }  
  
// Ausgabe ueber die Zeilen-Arrays  
  
echo "<font face=\"monospace\"><center>";  
  
$cA = 1;  
while ($cA <= 9)  
 {  
 $cB = 1;  
 while ($cB <= 9)  
  {  
  echo $z[$cA][$cB];  
  if ($cB == 3 || $cB == 6) echo "|";  
  $cB++;  
  }  
$brech = "<br>";  
  if ($cA == 3 || $cA == 6) $brech = "<br>---|---|---<br>";  
echo $brech;  
 $cA++;  
 }  
  
?>