Jump to content

10x10 Grid in GUI


James
 Share

Recommended Posts

So the user clicks one button, then we only allow them to click adjacent buttons like:

Button clicked, only allow one to the right, one below, and one to the bottom right to be clicked.

- or -

Click any button and auto-highlight a 4x4 (or whatever we define) area.

Link to comment
Share on other sites

Here is a cleaned up version, since i'm not ready for beta I chose to have a large font on selected buttons:

; Number Grid
; James Brooks
; Calculates difference
;      between numbers selected in gridoÝ÷ Ûú®¢×y©eÊ+l¥u·ºÚ"µÍÈ[XÜYÈZY[ÙXÛ[X]È]ÙÙ]H[YÂÈØ[Ý[]ÈY[ÙBÈ]ÙY[[XÈÙ[XÝY[ÜY

I personally think that's a little more fair <_<

Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

JamesB, come on down! You're the next contestant on...???

With this version, when you click a button, the predefined rectangle size will be selected around said button.

The code is probably confusing but this is how my mind works. <_<

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 

;Global vars
Dim $aDif, $add
Dim $btnArray[10][10][2]
Dim $squaresX = 2, $squaresY = 2

GUICreate("Number Grid - James Brooks - Maths C/W Assistant", 440, 500)
GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUICtrlCreateLabel("Maths C/W Assistant: Click multiple buttons to work out the difference!", 50, 5)
GUICtrlCreateLabel("Algebra Equation:", 10, 440)
$Equation = GUICtrlCreateInput("", 100, 438, 325)
$Difference = GUICtrlCreateLabel("Difference: " & $aDif, 10, 460, 100, 16)
$reset = GUICtrlCreateButton("reset", 200, 470)
GUICtrlSetOnEvent(-1, "reset")

;Generate buttons 10x10 labeled 1-100
$num = 0
For $Y = 0 To 9
    For $X = 0 To 9
        $btnArray[$Y][$X][0] = GUICtrlCreateButton($num + 1, ($X * 40) + 20, ($Y * 40) + 20, 40, 40)
        GUICtrlSetOnEvent(-1, "buttonPress")
        GUICtrlSetFont (-1,9, default)
        $btnArray[$Y][$X][1] = false
        $num += 1
    Next
Next

While 1
  Sleep(10)  ; Idle around
WEnd

Func buttonPress()
    ;Loop through all buttons for match
    For $Y = 0 To 9
        For $X = 0 To 9
            If @GUI_CTRLID = $btnArray[$Y][$X][0] Then
                ;toggle($X, $Y)
                $result = lookAround($X, $Y)
                If $result <> 0 Then
                    selectRectangle($X, $Y, $result)
                Else
                    MsgBox(0,"","Selection area to large for grid")
                EndIf
                Return
            EndIf
        Next
    Next
EndFunc

;Verify there are enough buttons in the vicinity to complete a rectangle $squaresX x $squaresY
Func lookAround($XX, $YY)
    ;Attempt 1: Right + Down
    If ($XX + ($squaresX - 1) < 10) AND ($YY + ($squaresY - 1) < 10) Then
        ;MsgBox(0,"","OK: Right + Down")
        Return 1
    ;Attempt 2: Left + Down
    ElseIf ($XX - ($squaresX - 1) >= 0) AND ($YY + ($squaresY - 1) < 10) Then
        ;MsgBox(0,"","OK: Left + Down")
        Return 2
    ;Attempt 3: Right + Up
    ElseIf ($XX + ($squaresX - 1) < 10) AND ($YY - ($squaresY - 1) >= 0) Then
        ;MsgBox(0,"","OK: Right + Up")
        Return 3
    ;Attempt 4: Left + Up
    ElseIf ($XX - ($squaresX - 1) >= 0) AND ($YY - ($squaresY - 1) >= 0) Then
        ;MsgBox(0,"","OK: Left + Up")
        Return 4
    Else
        Return 0
    EndIf
EndFunc

Func selectRectangle($XX, $YY, $dir)
    reset()
    Switch $dir
        
        ;Right + Down
        Case 1
            For $A = $YY to $YY + ($squaresY - 1)
                For $B = $XX to $XX + ($squaresX - 1)
                    toggle($B, $A)
                Next
            Next
        ;Left + Down
        Case 2
            For $A = $YY to $YY + ($squaresY - 1)
                For $B = $XX to $XX - ($squaresX - 1) Step -1
                    toggle($B, $A)
                Next
            Next
        ;Right + Up
        Case 3
            For $A = $YY to $YY - ($squaresY - 1) Step -1
                For $B = $XX to $XX + ($squaresX - 1)
                    toggle($B, $A)
                Next
            Next
        ;Left + Up
        Case 4
            For $A = $YY to $YY - ($squaresY - 1) Step -1
                For $B = $XX to $XX - ($squaresX - 1) Step -1
                    toggle($B, $A)
                Next
            Next
    EndSwitch
EndFunc

Func toggle($XX, $YY)
    ;If button already selected, deselect it
    If $btnArray[$YY][$XX][1] Then

        GUICtrlSetFont ($btnArray[$YY][$XX][0],9, default)

        $btnArray[$YY][$XX][1] = False
        ;$add = ($add - ($element + 1))
        ;GUICtrlSetData($Difference, "Difference = " & $add)
    Else

        GUICtrlSetFont ($btnArray[$YY][$XX][0],20, 600)

        $btnArray[$YY][$XX][1] = True
        ;$add += ($element + 1)
        ;GUICtrlSetData($Difference, "Difference = " & $add)
    EndIf
EndFunc   ;==>toggle

Func reset()
    For $Y = 0 to 9
        For $X = 0 To 9
            $btnArray[$Y][$X][1] = False
            GUICtrlSetFont ($btnArray[$Y][$X][0],9, default)
        Next
    Next
    ;$add = 0
    ;GUICtrlSetData($Difference, "Difference = 0")
EndFunc

Func CLOSEClicked()
    Exit
EndFunc
Link to comment
Share on other sites

I found out how the equation works here:

http://www.gcsemaths.net/pb/wp_9f768eb7/wp_9f768eb7.html

So here is a new version:

Posted Image

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode

;Global vars
Dim $aDif, $add
Dim $gridWidth = 10, $gridHeight = 10
Dim $btnArray[$gridHeight][$gridWidth][3]
Dim $squaresX = 2, $squaresY = 2

GUICreate("Number Grid - James Brooks - Maths C/W Assistant", 440, 500)
GUISetState(@SW_SHOW)

GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")

GUICtrlCreateLabel("Maths C/W Assistant: Click a button to work out the difference!", 50, 5)
GUICtrlCreateLabel("Algebra Equation:", 10, 440)
$Equation = GUICtrlCreateInput("", 100, 438, 325)

$reset = GUICtrlCreateButton("reset", 200, 470)
GUICtrlSetOnEvent(-1, "reset")

GUICtrlCreateLabel("Selection Size:", 10, 475, 70, 16)
$selectSize = GUICtrlCreateCombo ( "", 90, 470, 40, 40)
GUICtrlSetOnEvent(-1, "updateSelectSize")
GUICtrlSetData(-1,"2x2|3x3|4x4|5x5|6x6","2x2")

;Generate buttons 10x10 labeled 1-100
$num = 1
For $Y = 0 To $gridHeight - 1
    For $X = 0 To $gridWidth - 1
        $btnArray[$Y][$X][0] = GUICtrlCreateButton($num, ($X * 40) + 20, ($Y * 40) + 20, 40, 40)
        GUICtrlSetOnEvent(-1, "buttonPress")
        GUICtrlSetFont (-1,9, default)
        
        ;Deselected by default
        $btnArray[$Y][$X][1] = false
        
        ;Assign numerical value
        $btnArray[$Y][$X][2] = $num
        $num += 1
    Next
Next

While 1
  Sleep(10)  ; Idle around
WEnd

Func buttonPress()
    ;Loop through all buttons for match
    For $Y = 0 To $gridHeight - 1
        For $X = 0 To $gridWidth - 1
            If @GUI_CTRLID = $btnArray[$Y][$X][0] Then
                ;toggle($X, $Y)
                $result = lookAround($X, $Y)
                If $result <> 0 Then
                    selectRectangle($X, $Y, $result)
                    updateEquation()
                Else
                    MsgBox(0,"","Selection area too large for grid")
                EndIf
                Return
            EndIf
        Next
    Next
EndFunc

;Verify there are enough buttons in the vicinity to complete a rectangle $squaresX x $squaresY
Func lookAround($XX, $YY)
    ;Attempt 1: Right + Down
    If ($XX + ($squaresX - 1) < 10) AND ($YY + ($squaresY - 1) < 10) Then
        Return 1
    ;Attempt 2: Left + Down
    ElseIf ($XX - ($squaresX - 1) >= 0) AND ($YY + ($squaresY - 1) < 10) Then
        Return 2
    ;Attempt 3: Right + Up
    ElseIf ($XX + ($squaresX - 1) < 10) AND ($YY - ($squaresY - 1) >= 0) Then
        Return 3
    ;Attempt 4: Left + Up
    ElseIf ($XX - ($squaresX - 1) >= 0) AND ($YY - ($squaresY - 1) >= 0) Then
        Return 4
    Else
        Return 0
    EndIf
EndFunc

Func selectRectangle($XX, $YY, $dir)
    Local $TL, $TR, $BL, $BR
    reset()
    Switch $dir
        ;Right + Down
        Case 1
            For $A = $YY to $YY + ($squaresY - 1)
                For $B = $XX to $XX + ($squaresX - 1)
                    toggle($B, $A)
                Next
            Next
        ;Left + Down
        Case 2
            For $A = $YY to $YY + ($squaresY - 1)
                For $B = $XX to $XX - ($squaresX - 1) Step -1
                    toggle($B, $A)
                Next
            Next
        ;Right + Up
        Case 3
            For $A = $YY to $YY - ($squaresY - 1) Step -1
                For $B = $XX to $XX + ($squaresX - 1)
                    toggle($B, $A)
                Next
            Next
        ;Left + Up
        Case 4
            For $A = $YY to $YY - ($squaresY - 1) Step -1
                For $B = $XX to $XX - ($squaresX - 1) Step -1
                    toggle($B, $A)
                Next
            Next
    EndSwitch
EndFunc

;Scan array for grid corners
Func updateEquation()
    Local $TL, $TR, $BL, $BR
    Local $theEquation
    
    $countCol = 0
    For $Y = 0 To $gridHeight - 1
        $countRow = 0
        For $X = 0 To $gridWidth - 1
            If $btnArray[$Y][$X][1] Then
                If $countRow = 0 Then
                    If $countCol = 0 Then
                        $TL = $btnArray[$Y][$X][2]
                    EndIf
                    $BL = $btnArray[$Y][$X][2]
                Else
                    If $countCol = 0 Then
                        $TR = $btnArray[$Y][$X][2]
                    EndIf
                    $BR = $btnArray[$Y][$X][2]
                EndIf
                $countRow += 1
                
            EndIf
        Next
        If $CountRow > 0 Then
            $countCol += 1
        EndIf
    Next
    
    $theEquation = "(" & $TR & "*" & $BL & ") - (" & $TL & "*" & $BR & ")"
    GUICtrlSetData($Equation, $theEquation & " = " & Execute($theEquation))
    ;MsgBox(0,"",$TL & "," & $TR & "," & $BL & "," & $BR)
EndFunc

;Grab new selection dimensions from combobox
Func updateSelectSize()
    Switch GUICtrlRead($selectSize)
        Case "2x2"
            $squaresX = 2
            $squaresY = 2
        Case "3x3"
            $squaresX = 3
            $squaresY = 3
        Case "4x4"
            $squaresX = 4
            $squaresY = 4
        Case "5x5"
            $squaresX = 5
            $squaresY = 5
        Case "6x6"
            $squaresX = 6
            $squaresY = 6
    EndSwitch
EndFunc

;Toggle selected button
Func toggle($XX, $YY)
    ;If button already selected, deselect it
    If $btnArray[$YY][$XX][1] Then
        GUICtrlSetFont ($btnArray[$YY][$XX][0],9, default)
        $btnArray[$YY][$XX][1] = False
    Else
        GUICtrlSetFont ($btnArray[$YY][$XX][0],20, 600)
        $btnArray[$YY][$XX][1] = True
    EndIf
EndFunc   ;==>toggle

;Clear all selections
Func reset()
    For $Y = 0 To $gridHeight - 1
        For $X = 0 To $gridWidth - 1
            ;Deselected by default
            $btnArray[$Y][$X][1] = False
            GUICtrlSetFont ($btnArray[$Y][$X][0],9, default)
        Next
    Next
    GUICtrlSetData($Equation, "")
EndFunc

;EXIT
Func CLOSEClicked()
    Exit
EndFunc
Edited by weaponx
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...