#include <Array.au3>
#include <GDIPlus.au3>
;=======================>
#region GUI
$hGUI = GUIcreate("Sodoku", 300, 370)
Local $aInputs[9][9]
For $x = 0 To 8
	For $y = 0 To 8
		$left = ($x + 1) * 30
		$top = ($y + 1) * 30
		$aInputs[$x][$y] = GUICtrlCreateInput("", $left, $top, 20, 20)
	Next
Next
$idStartBut = GUIctrlcreatebutton("Start", 100, 320, 100)
load_old_sodoku($aInputs)
GUIsetstate()
;====================>Mit GDI+ die Striche draufmalen
draw_lines()
;======>
While 1
	$nMsg = GUIgetmsg()
	Switch $nMsg 
	Case -3 ;GUI_EVENT_CLOSE
		Exit
	Case $idStartBut 
		$aSodoku = get_array_of_sodoku($aInputs)
		save_Sodoku_Ini($aSodoku)
		MakeSodoku($aSodoku, $aInputs)
	EndSwitch
	Sleep(20)
WEnd

Func load_old_sodoku($aInputs)
	If NOT FileExists("last_sodoku.ini") Then return -1
	For $x = 0 To 8
		For $y = 0 To 8
			GUICtrlSetData($aInputs[$x][$y], Iniread("last_sodoku.ini", "section", $x&"|"&$y, ""))
		Next
	Next
	return 0
endfunc

Func save_Sodoku_Ini($aSodoku)
	For $x = 0 To 8
		For $y = 0 To 8
			If $aSodoku[$x][$y] <> 0 Then 
				Iniwrite("last_sodoku.ini", "section", $x&"|"&$y, $aSodoku[$x][$y])
			Else
				Iniwrite("last_sodoku.ini", "section", $x&"|"&$y, "")
			endif
		Next
	Next
EndFunc

Func get_array_of_sodoku($aInputs)
	Local $aSodoku[10][10]
	For $x = 0 To 8 ;die nicht-existierende Reihe mit 0en auffüllen
		$aSodoku[$x][9] = 0
	Next
	For $x = 0 To 8
		For $y = 0 To 8
			$aSodoku[$x][$y] = GUIctrlread($aInputs[$x][$y])
			If $aSodoku[$x][$y] = "" Then $aSodoku[$x][$y] = 0
		Next
	Next
	return $aSodoku
EndFunc

Func draw_lines()
	_GDIPlus_Startup()
	$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGui)
	$hPen = _GDIPlus_PenCreate(0xFF000000, 3)
	_GDIPlus_GraphicsDrawLine($hGraphics, 115, 20, 115, 300, $hPen) ;senkrecht
	_GDIPlus_GraphicsDrawLine($hGraphics, 205, 20, 205, 300, $hPen) ;senkrecht
	_GDIPlus_GraphicsDrawLine($hGraphics, 20, 115, 295, 115, $hPen) ;waagrecht
	_GDIPlus_GraphicsDrawLine($hGraphics, 20, 205, 295, 205, $hPen) ;waagrecht
	_GDIPlus_Shutdown()
EndFunc

Func array_drehen2D($array)
	$er = $array
	For $i = 0 To Ubound($array) -1
		For $i2 = 0 To Ubound($array, 2) -1
			$er[$i][$i2] = $array[$i2][$i]
		Next
	Next
	return $er
EndFunc ;==>by Zeitriss

#endregion GUI


Func MakeSodoku($aFeld, $aInputs)
	Local $aMerk[9][9] ;zum merken, welche Zahlen dort hin passen 
	While 1
	$Ziffervoll = true
	For $x = 0 To 8 ;==>Alle Ziffern setzen
		For $y = 0 To 8
			If $aFeld[$x][$y] = 0 Then
				$Ziffervoll = False
				$aMerk[$x][$y] = getpossibleNrs($aFeld, $x, $y)
			Else
				$aMerk[$x][$y] = "" 
			EndIf
		Next
	Next
	If $Ziffervoll Then exitloop 
	;===>Alle Ziffern setzen, die eindeutig sind
	For $x = 0 To 8 ;==>Alle Ziffern setzen
		For $y = 0 To 8
			If Stringlen($aMerk[$x][$y]) = 1 Then 
				$aFeld[$x][$y] = $aMerk[$x][$y]
				$aMerk[$x][$y] = ""
			EndIf
		Next
	Next
	Sleep(5)
	WEnd
	;===========================>Berechnung ist beendet
	;=======>Jetzt werden die Kontrollfelder ausgefüllt
	For $x = 0 To 8
		For $y = 0 To 8
			GUIctrlsetdata($aInputs[$x][$y], $aFeld[$x][$y])
		Next
	Next
EndFunc

Func getpossibleNrs($aFeld, $iX_coord, $iY_coord)
	$sCase = "123456789"
	;========>Senkrechte
	For $y = 0 To 8
		If $aFeld[$iX_coord][$y] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX_coord][$y], "")	
	Next
	;=========>Waagrechte
	For $x = 0 To 8
		If $aFeld[$x][$iY_coord] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$x][$iY_coord], "")	
	Next
	;==============>Daten des 9er-Feldes bestimmen
	If $iX_coord < 3 Then ;die Yer
		$iX = 1
	elseif $iX_coord > 5 Then 
		$iX = 7
	elseif $iX_coord < 6 and $iX_coord > 2  Then 
		$iX = 4
	EndIf
	;=>
	If $iY_coord < 3 Then ;die Yer
		$iY = 1
	elseif $iY_coord > 5 Then 
		$iY = 7
	elseif $iY_coord < 6 and $iY_coord > 2  Then 
		$iY = 4
	EndIf
	;=========>9er-Feld
		If $iX > 0 Then 
			If $aFeld[$iX-1][$iY] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX-1][$iY], "")
		EndIf
		If $iX < 9 Then 
			If $aFeld[$iX+1][$iY] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX+1][$iY], "")
		EndIf
		If $iX > 0 and $iY > 0 Then 
			If $aFeld[$iX-1][$iY-1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX-1][$iY-1], "")
		EndIf
		If $iX < 9 and $iY > 0 Then 
			If $aFeld[$iX+1][$iY-1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX+1][$iY-1], "")
		EndIf
		If $iY > 0 Then 
			If $aFeld[$iX][$iY-1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX][$iY-1], "")
		EndIf
		If $iX < 9 and $iY < 9 Then 	
			If $aFeld[$iX+1][$iY+1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX+1][$iY+1], "")
		EndIf
		If $iX > 0 and $iY < 9 Then 
			If $aFeld[$iX-1][$iY+1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX-1][$iY+1], "")
		EndIf
	If $iY < 9 Then 
		If $aFeld[$iX][$iY+1] <> 0 Then $sCase = Stringreplace($sCase, $aFeld[$iX][$iY+1], "")
	EndIf
	;=============>
	return $sCase
EndFunc  