$N=SudokuGenerate() ClipPut($N) MsgBox(0,0,$N) Func SudokuGenerate() $Timer=TimerInit() $1="123456789" Local $A[3][9] For $i1 = 0 To 2 For $i = 0 To 8 $A[$i1][$i]=$1 Next Next $N="" $Forced="" $CCC=TimerDiff($Timer) For $j=0 To 9*9-1 $Col=Int($j/9) $Raw=Mod($j,9) $Area=Int($Raw/3)+Int($Col/3)*3 $m=Add($A,$Area,$Raw,$Col,$Forced) If $m > 10 Then $N=StringTrimRight($N,1) $j-=2 $Forced=$m-10 ElseIf $m =10 Then $N=StringTrimRight($N,1) $j-=2 $Forced=10 Else $N&=$m $Forced="" EndIf Next ;~ MsgBox(0,0,TimerDiff($Timer)) ;~ MsgBox(0,0,$CCC) Add($S,$Area="Reset") Return $N EndFunc Func Add(ByRef $S,$Area=0,$Raw=0,$Col=0,$Forced="") Static $St[81] Static $Sol[81] If $Area=="Reset" Then Local $G[81] $Sol=$G $St=$G Return EndIf If ($Forced=10) Then $St[$Col*9+$Raw] = RemoveEx($St[$Col*9+$Raw],$Sol[$Col*9+$Raw]) Remove($S,$Sol[$Col*9+$Raw],$Area,$Raw,$Col,1) $Sol[$Col*9+$Raw]="" If $St[$Col*9+$Raw] = "" Then Return 10 EndIf If $St[$Col*9+$Raw] Then $Str=$St[$Col*9+$Raw] Else $Str=Execute("'"& StringRegExpReplace($S[0][$Area]&$S[1][$Raw]&$S[2][$Col],"(\d)","' & TestEx(""\1"") & '") &"'") TestEx("Reset") $St[$Col*9+$Raw]=$Str EndIf $C=Number(StringMid($Str,Random(1,StringLen($Str),1),1)) If $Forced And Not ($Forced=10) Then $C = (StringInStr($Str,$Forced)?$Forced:"") Remove($S,$C,$Area,$Raw,$Col) If $C>=1 And $C<=9 Then $Sol[$Col*9+$Raw]=$C Else $St[$Col*9+$Raw]="" Return $C+10 EndIf Return $C EndFunc Func Remove(ByRef $S,$N,$Area,$Raw,$Col,$T=0) $S[0][$Area]=RemoveEx($S[0][$Area],$N,$T) $S[1][$Raw]=RemoveEx($S[1][$Raw],$N,$T) $S[2][$Col]=RemoveEx($S[2][$Col],$N,$T) EndFunc Func RemoveEx($S,$N,$T=0) If $T Then Return $S&$N Else Return StringRegExpReplace($S,$N,"") EndIf EndFunc Func TestEx($N) Static $M[10] If $N="Reset" Then Local $G[10] $M=$G Return EndIf $M[$N]+=1 If $M[$N]=3 Then Return $N EndIf Return "" EndFunc