Jump to content

Sudoku Problems


 Share

Recommended Posts

I'm having trouble with my sudoku script. I have seen other scripts on the forum, but this script is strictly for me to learn. So far, it works when I click the button to check if there are invalid values in the boxes. When I click the button a second time, my error message comes up. I think the problem is with the array. How do I fix this problem?

#include <GuiConstants.au3>
#include <Array.au3>

Dim $Box[5][5],$Input[17],$Row1[1],$Row2[1],$Row3[1],$Row4[1],$Column1[1],$Column2[1],$Column3[1],$Column4[1]
Dim $Cell1[1],$Cell2[1],$Cell3[1],$Cell4[1], $Cell1Array[5],$Cell2Array[5],$Cell3Array[5],$Cell4Array[5]
Dim $Col1Array[5],$Col2Array[5],$Col3Array[5],$Col4Array[5],$Row1Array[5],$Row2Array[5],$Row3Array[5],$Row4Array[5]

Opt("TrayIconDebug", 1)

$Form1 = GUICreate("Sudoku", 633, 447, 193, 125)
$Input[1] = GUICtrlCreateInput("", 128, 88, 30, 30)
$Input[2] = GUICtrlCreateInput("", 157, 88, 30, 30)
$Input[5] = GUICtrlCreateInput("", 128, 117, 30, 30)
$Input[6] = GUICtrlCreateInput("", 157, 117, 30, 30)
$Input[3] = GUICtrlCreateInput("", 200, 88, 30, 30)
$Input[4] = GUICtrlCreateInput("", 229, 88, 30, 30)
$Input[7] = GUICtrlCreateInput("", 200, 117, 30, 30)
$Input[8] = GUICtrlCreateInput("", 229, 117, 30, 30)
$Input[9] = GUICtrlCreateInput("", 128, 160, 30, 30)
$Input[10] = GUICtrlCreateInput("", 157, 160, 30, 30)
$Input[13] = GUICtrlCreateInput("", 128, 189, 30, 30)
$Input[14] = GUICtrlCreateInput("", 157, 189, 30, 30)
$Input[11] = GUICtrlCreateInput("", 200, 160, 30, 30)
$Input[12] = GUICtrlCreateInput("", 229, 160, 30, 30)
$Input[15] = GUICtrlCreateInput("", 200, 189, 30, 30)
$Input[16] = GUICtrlCreateInput("", 229, 189, 30, 30)
$Button1 = GUICtrlCreateButton("Auto", 152, 256, 65, 33, 0)
$a = 0
For $x = 1 To 16
    GUICtrlSetLimit($Input[$x], 1)
Next
GUISetState(@SW_SHOW)

While 1
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then Exit
        If $msg = $Button1 Then
            Values()
            If Check() = 0 Then
                ExitLoop
            Else
                Solve()
            EndIf
        EndIf
    WEnd
WEnd
Func Values();write values in each box to an array
    $z = 0
    For $x = 1 To 4;row
        For $y = 1 To 4;column
            $z = $z+1
            $Box[$x][$y] = GUICtrlRead($Input[$z])
        Next
    Next
;row values
    For $x = 1 To 4
        _ArrayInsert($Row1, $x, $Box[1][$x])
        _ArrayInsert($Row2, $x, $Box[2][$x])
        _ArrayInsert($Row3, $x, $Box[3][$x])
        _ArrayInsert($Row4, $x, $Box[4][$x])
    Next
;column values
    For $x = 1 To 4
        _ArrayInsert($Column1, $x, $Box[$x][1])
        _ArrayInsert($Column2, $x, $Box[$x][2])
        _ArrayInsert($Column3, $x, $Box[$x][3])
        _ArrayInsert($Column4, $x, $Box[$x][4])
    Next
;box 1 values
    _ArrayInsert($Cell1, 1, $Box[1][1])
    _ArrayInsert($Cell1, 2, $Box[1][2])
    _ArrayInsert($Cell1, 3, $Box[2][1])
    _ArrayInsert($Cell1, 4, $Box[2][2])
;box 2 values
    _ArrayInsert($Cell2, 1, $Box[1][3])
    _ArrayInsert($Cell2, 2, $Box[1][4])
    _ArrayInsert($Cell2, 3, $Box[2][3])
    _ArrayInsert($Cell2, 4, $Box[2][4])
;box 3 values
    _ArrayInsert($Cell3, 1, $Box[3][1])
    _ArrayInsert($Cell3, 2, $Box[3][2])
    _ArrayInsert($Cell3, 3, $Box[4][1])
    _ArrayInsert($Cell3, 4, $Box[4][2])
;box 4 values
    _ArrayInsert($Cell4, 1, $Box[3][3])
    _ArrayInsert($Cell4, 2, $Box[3][4])
    _ArrayInsert($Cell4, 3, $Box[4][3])
    _ArrayInsert($Cell4, 4, $Box[4][4])
EndFunc
Func Check();Makes sure there isn't two numbers in a single row, box, or column
    For $x = 1 To 4
        $Cell1Array[$x]     = _ArrayFindAll($Cell1, $x)
        $Cell2Array[$x]     = _ArrayFindAll($Cell2, $x)
        $Cell3Array[$x]     = _ArrayFindAll($Cell3, $x)
        $Cell4Array[$x]     = _ArrayFindAll($Cell4, $x)
        If UBound($Cell1Array[$x])>1 Or UBound($Cell2Array[$x])>1 Or UBound($Cell3Array[$x])>1 Or UBound($Cell4Array[$x])>1 Then
            MsgBox(0, "Error", "You have entered an invalid value " & $x & " into a cell.")
            Return 0
        EndIf
        $Row1Array[$x]      = _ArrayFindAll($Row1, $x)
        $Row2Array[$x]      = _ArrayFindAll($Row2, $x)
        $Row3Array[$x]      = _ArrayFindAll($Row3, $x)
        $Row4Array[$x]      = _ArrayFindAll($Row4, $x)
        If UBound($Row1Array[$x])>1 Or UBound($Row2Array[$x])>1 Or UBound($Row3Array[$x])>1 Or UBound($Row4Array[$x])>1 Then
            MsgBox(0, "Error", "You have entered an invalid value into a row.")
            Return 0
        EndIf
        $Col1Array[$x]      = _ArrayFindAll($Column1, $x)
        $Col2Array[$x]      = _ArrayFindAll($Column2, $x)
        $Col3Array[$x]      = _ArrayFindAll($Column3, $x)
        $Col4Array[$x]      = _ArrayFindAll($Column4, $x)
        If UBound($Col1Array[$x])>1 Or UBound($Col2Array[$x])>1 Or UBound($Col3Array[$x])>1 Or UBound($Col4Array[$x])>1 Then
            MsgBox(0, "Error", "You have entered an invalid value into a column.")
            Return 0
        EndIf
    Next
    Return 1
EndFunc
Func Solve();solves the puzzle
;progressing
EndFunc
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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