Jump to content

Recommended Posts

Posted

Made this Sudoku game yesterday for fun. The coding is pretty rough and not at all optimized but i think its working fine :) Anyway, you're welcome to try it. I'm probably gonna work some more on it later on so if you're interested check back for an update later on.

Source:

#cs ----------------------------------------------------------------------------
    
    AutoIt Version: 3.3.6.1
    Author:         Datenshi @ Autoit Forums
    
    Script Function:
    Sudoku Game. Enjoy
    
#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#region ### START Koda GUI section ### Form=
$SudokuGUI = GUICreate("Sudoku", 408, 334, 192, 124)
Opt("GUIDataSeparatorChar", "|")

;# DRAW GRID LABELS
GUICtrlCreateLabel("", 25, 110, 262, 5)
GUICtrlSetBkColor(-1, 0x00000)
GUICtrlCreateLabel("", 25, 200, 262, 5)
GUICtrlSetBkColor(-1, 0x00000)
GUICtrlCreateLabel("", 108, 25, 5, 265)
GUICtrlSetBkColor(-1, 0x00000)
GUICtrlCreateLabel("", 197, 25, 5, 265)
GUICtrlSetBkColor(-1, 0x00000)
;# DRAW GRID LABELS
$GenSudoku = GUICtrlCreateButton("Generate Sudoku", 296, 288, 97, 41)
$Check = GUICtrlCreateButton("Check", 296, 240, 97, 41)
$Solve = GUICtrlCreateButton("Solve it!", 296, 190, 97, 41)
$Combo1 = GUICtrlCreateCombo("", 312, 16, 89, 25)
GUICtrlSetData($Combo1, "Easy|Medium|Hard", "Easy")
#endregion ### END Koda GUI section ###
Global $_aGUICtrlTableBordersINTERNALSTORE[1][2]
$genstart = 0
Dim $array[1]
Dim $SudArray[10][10]


$Table1 = _GUICtrlTableEDIT_Create(25, 27, 20, 20, 9, 9, 10)
GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GenSudoku
            $genstart = 1
            $LAbel = GUICtrlCreateLabel("Generating Table", 130, 6, 97, 21)
            Do
                SudokuEngine()
            Until GUICtrlRead($SudArray[UBound($SudArray, 1) - 1][UBound($SudArray, 2) - 1]) <> ""
            GUICtrlDelete($LAbel)
            ;SudokuEngine()
            Switch GUICtrlRead($Combo1)
                Case "Easy"
                    SudokuFill(25)
                Case "Medium"
                    SudokuFill(20)
                Case "Hard"
                    SudokuFill(15)
            EndSwitch
        Case $Check
            If $genstart = 1 Then SudokuCheck()
        Case $Solve
            SudokuFill(81)
    EndSwitch
WEnd

Func _GUICtrlTableEDIT_Create($iLeft, $iTop, $iWidth, $iHeight, $iRows, $iColumns, $iGapWidth = 1) ; ; Table UDF Author ........: AndyBiochem ; Modified by Datenshi

    Local $i, $j, $iCurrBoxLeft = 0, $iCurrBoxTop = 0, $aTemp
    Global $array[$iRows + 1][$iColumns + 1]

    If $iGapWidth < 0 Then $iGapWidth = 0
    $iGapWidth = Round($iGapWidth)

    For $i = 1 To $iRows

        For $j = 1 To $iColumns
            $array[$i][$j] = GUICtrlCreateEdit("", $iLeft + $iCurrBoxLeft, $iTop + $iCurrBoxTop, $iWidth, $iHeight, 0x2000) ;GUICtrlCreateLabel("", $iLeft + $iCurrBoxLeft, $iTop + $iCurrBoxTop, $iWidth, $iHeight)
            GUICtrlSetBkColor(-1, 0xFFFFFF)
            $iCurrBoxLeft += $iWidth + $iGapWidth

        Next
        $iCurrBoxLeft = 0
        $iCurrBoxTop += $iHeight + $iGapWidth

    Next

    ReDim $_aGUICtrlTableBordersINTERNALSTORE[UBound($_aGUICtrlTableBordersINTERNALSTORE, 1) + 1][2]
    Dim $aTemp[$iRows + 1][$iColumns + 1][5]
    $_aGUICtrlTableBordersINTERNALSTORE[UBound($_aGUICtrlTableBordersINTERNALSTORE) - 1][0] = $array[1][1]
    $_aGUICtrlTableBordersINTERNALSTORE[UBound($_aGUICtrlTableBordersINTERNALSTORE) - 1][1] = $aTemp

    Return $array

EndFunc   ;==>_GUICtrlTableEDIT_Create

Func SudokuEngine()
    ReDim $SudArray[10][10]
    Local $Collision, $break = 0, $NumberPool = "123456789"
    For $i = 1 To 9
        $NumberPool = "123456789"
        For $j = 1 To 9
            $NrPick = RandomizeString($NumberPool, $i - 1, $j)
            $SudArray[$i][$j] = $NrPick
            Sleep(0)
            $NumberPool = StringReplace($NumberPool, $NrPick, "")
        Next
    Next
EndFunc   ;==>SudokuEngine

Func RandomizeString($String, $last, $coll)
    If $last > 0 Then
        For $e = 1 To $last
            $String = StringReplace($String, $SudArray[$e][$coll], "")
        Next
    EndIf
    $Len = StringLen($String)
    $Rndm = Random(1, $Len, 1)
    If $Len = 1 Then $Rndm = 1
    Return StringMid($String, $Rndm, 1)
EndFunc   ;==>RandomizeString

Func SudokuFill($diff)
    ;#Clear table
    For $x = 1 To 9
        For $h = 1 To 9
            GUICtrlSetData($array[$x][$h], "")
        Next
    Next
    ;#Clear table

    For $d = 1 To $diff
        Do
            $Next = 0
            $RandomClue1 = Random(1, 10)
            $RandomClue2 = Random(1, 10)
            If GUICtrlRead($array[$RandomClue1][$RandomClue2]) = "" Then
                GUICtrlSetData($array[$RandomClue1][$RandomClue2], $SudArray[$RandomClue1][$RandomClue2])
                GUICtrlSetState($array[$RandomClue1][$RandomClue2], $GUI_DISABLE)
                $Next = 1
            EndIf
        Until $Next = 1

    Next
EndFunc   ;==>SudokuFill

Func SudokuCheck()
    $key = 0
    For $o = 1 To 9
        For $a = 1 To 9
            If GUICtrlRead($array[$o][$a]) = $SudArray[$o][$a] Then
                $key = 1
                ContinueLoop

            Else
                MsgBox(0, "Bad Luck!", "Incorrect match at -> " & " Row: " & $o & " Collumn: " & $a & " Square # " & $o * $a)
                $key = 0
                ExitLoop (2)
            EndIf
        Next
    Next
    If $key = 1 Then MsgBox(0, "WINNER!", "You've solved the soduko. !!Good Work!!")
EndFunc   ;==>SudokuCheck

Executable is attached.

Sudoku.exe

Posted

like Shaggi said, I also got two eights in one block:

Posted Image

also, after I clicked "Solve it" and "Generate" none of the edits became ENABLED again, they were all disabled.

Posted

Oyeah its quite buggy =) Found a couple of problems which are tied to the table generation. Fixed it but it took way too long to generate new sudoku tables so i'm thinking of some smart algo that'll solve it. Current way is pretty much pure bruteforce which has issues in itself :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...