1, "1018" => 2, "1014" => 3, "1006" => 4, "990" => 5, "958" => 6, "894" => 7, "766" => 8, "510" => 9); // Set all masks to zero for($i=0; $i<9; $i++) { $row[$i]=0; $col[$i]=0; $square[$i]=0; for($j=0; $j<9; $j++) { $board["$i$j"]=0; } } // Verify doubles for($i=0; $i<9; $i++) { for($j=0; $j<9; $j++) { $num = value($i,$j); if($num != 0) { $num=$_POST["f$i$j"]; $newMask=1<<$num; if($row[$i] & $newMask) { print("Double number $num in row $i
"); } $row[$i] |= $newMask; if($col[$j] & $newMask) { print("Double number $num in column $j
"); } $col[$j] |= $newMask; $sq=(3*floor($i/3))+floor($j/3); if($square[$sq] & $newMask) { print("Double number $num in square $square
"); } $square[$sq] |= $newMask; if($board["$i$j"] & $newMask) { print("Double number $num at position $i $j"); } $sqI = $i - ($i % 3); $sqJ = $j - ($j % 3); # Rows and columns for($k=0; $k<9; $k++) { $board["$i$k"] |= $newMask; $board["$k$j"] |= $newMask; $ii = $sqI + ($k % 3); $jj = $sqJ + floor($k / 3); $board["$ii$jj"] |= $newMask; } $board["$i$j"] = 1023 ^ $newMask; } } } $sugg=array(); // Suggestions if number can only go into a single place of a square for($idx=0; $idx<9; $idx++) { $squareMask=$square[$idx]; for($num=1; $num <=9; $num++) { $numMask = 1 <<$num; if(! ($squareMask & $numMask)) { // Number is indeed missing... $I=floor($idx/3); $J=$idx%3; $lsugg="none"; $lsuggI="none"; $lsuggJ="none"; for($i=$I*3; $i<($I+1)*3; $i++) { for($j=$J*3; $j<($J+1)*3; $j++) { if($board["$i$j"] & $numMask) { continue; } if($lsugg == "none") { $lsugg = "$i$j"; } else { $lsugg = "amb"; } if($lsuggI == "none") { $lsuggI = "$i"; } else if($lsuggI != $i) { $lsuggI = "amb"; } if($lsuggJ == "none") { $lsuggJ = "$j"; } else if($lsuggJ != $j) { $lsuggJ = "amb"; } } } if($lsugg == "none") { print "Nowhere to place $num in square $idx
\n"; } else if($lsugg != "amb") { $sugg[$lsugg]=$num; } if($lsuggI != "amb") { for($j=0; $j<9; $j++) { if($j >= $J*3 && $j < ($J+1)*3) { continue; } if((1023 ^ $board["$lsuggI$j"]) & $numMask) { $idx=-1; } $board["$lsuggI$j"] |= $numMask; } } if($lsuggJ != "amb") { for($i=0; $i<9; $i++) { if($i >= $I*3 && $i < ($I+1)*3) { continue; } if((1023 ^ $board["$i$lsuggJ"]) & $numMask) { $idx=-1; } $board["$i$lsuggJ"] |= $numMask; } } } } } // Suggestions if number can only go into a single place of a row for($idx=0; $idx<9; $idx++) { $rowMask=$row[$idx]; for($num=1; $num <=9; $num++) { $numMask = 1 <<$num; if(! ($rowMask & $numMask)) { // Number is indeed missing... $lsugg="none"; for($j=0; $j<9; $j++) { if($board["$idx$j"] & $numMask) { continue; } if($lsugg == "none") { $lsugg = "$idx$j"; } else { $lsugg = "amb"; } } if($lsugg == "none") { print "Nowhere to place $num in row $idx
\n"; } else if($lsugg != "amb") { $sugg[$lsugg]=$num; } } } } // Suggestions if number can only go into a single place of a column for($idx=0; $idx<9; $idx++) { $colMask=$col[$idx]; for($num=1; $num <=9; $num++) { $numMask = 1 <<$num; if(! ($colMask & $numMask)) { // Number is indeed missing... $lsugg="none"; for($i=0; $i<9; $i++) { if($board["$i$idx"] & numMask) { continue; } if($lsugg == "none") { $lsugg = "$i$idx"; } else { $lsugg = "amb"; } } if($lsugg == "none") { print "Nowhere to place $num in col $idx
\n"; } else if($lsugg != "amb") { #print "Col sugg $num $lsugg
\n"; $sugg[$lsugg]=$num; } } } } // Suggestions if only a single number may fit into a given square for($i=0; $i<9; $i++) { for($j=0; $j<9; $j++) { $sq=(3*floor($i/3))+floor($j/3); $mask = $board["$i$j"]; if($mask == 1022) { print "No number to put on $i $j
\n"; } if(array_key_exists($mask,$nums)) { $sugg["$i$j"] = $nums[$mask]; } } } $firstSubmit = !array_key_exists("notfirst", $_POST); $tg=" id=\"target\" "; print " \n"; for($i=0; $i<9; $i++) { print " \n"; for($j=0; $j<9; $j++) { if((floor($i/3)+floor($j/3)) %2 == 0) { $col="#ffff40"; } else { $col="#ffffc0"; } print " \n"; } print " \n"; } if(array_key_exists("submit", $_POST)) { print ""; } print "
"; if(array_key_exists("f$i$j",$_POST) && $_POST["f$i$j"] != "") { $isPreset = array_key_exists("c$i$j", $_POST) || $firstSubmit; print " "; if($isPreset) { print $_POST["f$i$j"]; } else { print "".$_POST["f$i$j"].""; } print " "; if($isPreset) { print ""; } } else { print ""; $tg=""; } print "
\n"; ?>