Jump to content

Recommended Posts

Posted (edited)

Goal: select the squares to match every row and column results (screenshot below).

you can change the puzzle type (addition or multiplication), grid size (3x3 to 9x9), and other preferences.

Dependency: this app is also a showcase for the WinPose UDF (which was actually developed for it).

Script:

#Region wrapper directives
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_UseX64=N
#AutoIt3Wrapper_Compile_both=N
#AutoIt3Wrapper_Icon=MagicMath.ico
#AutoIt3Wrapper_UseUpx=N
#AutoIt3Wrapper_Res_Description=Magic Math
#AutoIt3Wrapper_Res_Fileversion=1.0.0.0
#AutoIt3Wrapper_Res_ProductVersion=1.0.0.0
#AutoIt3Wrapper_Res_LegalCopyright=Or Ben Shabat (or.ben.shabat@gmail.com)
#AutoIt3Wrapper_Run_Au3Stripper=y
#Au3Stripper_Parameters=/SO /RM
#EndRegion wrapper directives
#NoTrayIcon

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ColorConstants.au3>
#include <FontConstants.au3>
#include <AutoItConstants.au3>
#include <EditConstants.au3>
#include <Math.au3>
#include 'WinPose.au3'

; init
; - general
Global Const $sAppName = 'Magic Math'
; - colors
Global Const $iDefaultSqrSize = 45
Global Const $iDefaultColCount = 5
Global Const $iDefaultRowCount = 5
Global Const $iDefaultResizeSpeed = 50
Global Const $iColor_BoxText = $COLOR_BLACK
Global Const $iColor_BoxTextError = $COLOR_RED
Global Const $iColor_BoxUnchecked = $COLOR_LIGHTGRAY
Global Const $iColor_BoxChecked = $COLOR_LIGHTGREEN
Global Const $iColor_BoxEliminated = $COLOR_DARKGRAY
Global Const $iColor_BoxResult = $COLOR_PEACHPUFF
Global Const $iColor_MenuItem = $COLOR_DARKBLUE
Global Const $iColor_MenuItemPressed = $COLOR_LIGHTGRAY
Global Const $iColor_MenuItemEmphasis = $COLOR_WHITE
Global Const $iColor_Vprimary = $COLOR_LIMEGREEN
Global Const $iColor_Vsecondary = $COLOR_AQUA
; - symbols
Global Const $symSetup = Chr(64) ; Webdings
Global Const $symRenew = Chr(81) ; Wingdings 3
Global Const $symSolve = Chr(97) ; Webdings
Global Const $symHelp = Chr(115) ; Webdings
Global Const $symP = Chr(200) ; Wingdings 2
Global Const $symX = Chr(208) ; Wingdings 2
Global Const $symV = Chr(252) ; Wingdings

; declare variables
Global $bPuzzleTypeIsAddition = True
Global $iColCount = $iDefaultColCount, $iRowCount = $iDefaultRowCount, $iSqrSize = $iDefaultSqrSize, $iResizeSpeed = $iDefaultResizeSpeed
Global $iGUI_Width, $iGUI_Height, $hGUI
Global $aPos, $iWDiff, $iHDiff
; - main GUI
Global $gMenu_New, $gMenu_Solve, $gMenu_Pref, $gMenu_Help, $gdGUIStart, $gdGUIEnd, $gdGridStart, $gdGridEnd
Global Enum $iGrid_iValue, $iGrid_gID, $iGrid_bSelected, $iGrid_bChecked, $iGrid_bEliminated, $iGrid_iDimCount
Global $aGrid[$iColCount][$iRowCount][$iGrid_iDimCount]
Global Enum $iResult_iValue, $iResult_iDiff, $iResult_iDimCount
Global $aResultOfRow[$iRowCount][$iResult_iDimCount]
Global $aResultOfCol[$iColCount][$iResult_iDimCount]
Global $gPuzzleType
Global $bUserInputAllowed
Global $bCheckForVictory
; - pref GUI
Global $gPuzzleType_Addition, $gPuzzleType_Multiplication, _
        $gGridWidth_Down, $gGridWidth_Data, $gGridWidth_Up, _
        $gGridHeight_Down, $gGridHeight_Data, $gGridHeight_Up, _
        $gSqrSize_Down, $gSqrSize_Data, $gSqrSize_Up, _
        $gResizeSpeed_data, _
        $gOK

; GUI
$iGUI_Width = $iSqrSize * ($iColCount + 3) + 2
$iGUI_Height = $iSqrSize * ($iRowCount + 2) + 2
$hGUI = GUICreate($sAppName, $iGUI_Width, $iGUI_Height)
$aPos = WinGetPos($hGUI)
$iWDiff = $aPos[2] - $iGUI_Width
$iHDiff = $aPos[3] - $iGUI_Height
BuildGUI()
GUISetState(@SW_SHOW, $hGUI)
; main loop
Global $msg
Global $aCursorInfo
$bCheckForVictory = False
While True
    $msg = GUIGetMsg()
    Switch $msg
        Case $gMenu_New
            GUICtrlSetBkColor($gMenu_New, $iColor_MenuItemPressed)
            Sleep(100)
            Renew()
            Sleep(100)
            GUICtrlSetBkColor($gMenu_New, $GUI_BKCOLOR_TRANSPARENT)
        Case $gMenu_Solve
            GUICtrlSetBkColor($gMenu_Solve, $iColor_MenuItemPressed)
            Sleep(100)
            Solve()
            Sleep(100)
            GUICtrlSetBkColor($gMenu_Solve, $GUI_BKCOLOR_TRANSPARENT)
        Case $gMenu_Pref
            GUICtrlSetBkColor($gMenu_Pref, $iColor_MenuItemPressed)
            Sleep(100)
            BuildGUI(Pref())
            Sleep(100)
            GUICtrlSetBkColor($gMenu_Pref, $GUI_BKCOLOR_TRANSPARENT)
        Case $gMenu_Help
            GUICtrlSetBkColor($gMenu_Help, $iColor_MenuItemPressed)
            Sleep(100)
            BuildGUI(Help())
            Sleep(100)
            GUICtrlSetBkColor($gMenu_Help, $GUI_BKCOLOR_TRANSPARENT)
        Case $gdGridStart To $gdGridEnd
            If $bUserInputAllowed Then
                For $iRow = 0 To $iRowCount - 1
                    For $iCol = 0 To $iColCount - 1
                        If $msg = $aGrid[$iCol][$iRow][$iGrid_gID] Then
                            $aGrid[$iCol][$iRow][$iGrid_bEliminated] = False
                            $aGrid[$iCol][$iRow][$iGrid_bChecked] = Not $aGrid[$iCol][$iRow][$iGrid_bChecked]
                            GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bChecked] ? $iColor_BoxChecked : $iColor_BoxUnchecked)
                        EndIf
                    Next
                Next
                $bCheckForVictory = True
            EndIf
        Case $GUI_EVENT_SECONDARYUP
            $aCursorInfo = GUIGetCursorInfo()
            If $aCursorInfo[4] > $gdGridStart And $aCursorInfo[4] < $gdGridEnd Then
                If $bUserInputAllowed Then
                    For $iRow = 0 To $iRowCount - 1
                        For $iCol = 0 To $iColCount - 1
                            If $aCursorInfo[4] = $aGrid[$iCol][$iRow][$iGrid_gID] Then
                                $aGrid[$iCol][$iRow][$iGrid_bChecked] = False
                                $aGrid[$iCol][$iRow][$iGrid_bEliminated] = Not $aGrid[$iCol][$iRow][$iGrid_bEliminated]
                                GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bEliminated] ? $iColor_BoxEliminated : $iColor_BoxUnchecked)
                            EndIf
                        Next
                    Next
                    $bCheckForVictory = True
                EndIf
            EndIf
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
    If $bCheckForVictory Then
        If IsVictoryAchieved() Then
            ShowVictory()
            GUICtrlSetBkColor($gMenu_New, $iColor_MenuItemEmphasis)
            Sleep(125)
            Renew()
            Sleep(125)
            GUICtrlSetBkColor($gMenu_New, $GUI_BKCOLOR_TRANSPARENT)
        EndIf
        $bCheckForVictory = False
    EndIf
WEnd

Func Renew($bNew = True)
    $bUserInputAllowed = True
    Local $bNoZeroResults
    GUISetState(@SW_LOCK)
    Do
        $bNoZeroResults = True
        For $iRow = 0 To $iRowCount - 1
            For $iCol = 0 To $iColCount - 1
                If $bNew Then
                    $aGrid[$iCol][$iRow][$iGrid_iValue] = $bPuzzleTypeIsAddition ? Random(1, 9, 1) : Random(2, 9, 1)
                    $aGrid[$iCol][$iRow][$iGrid_bSelected] = (Random() < 0.5)
                    $aGrid[$iCol][$iRow][$iGrid_bChecked] = False
                    $aGrid[$iCol][$iRow][$iGrid_bEliminated] = False
                EndIf
                GUICtrlSetData($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_iValue])
                GUICtrlSetColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxText)
                Select
                    Case $aGrid[$iCol][$iRow][$iGrid_bChecked]
                        GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxChecked)
                    Case $aGrid[$iCol][$iRow][$iGrid_bEliminated]
                        GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxEliminated)
                    Case Else
                        GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxUnchecked)
                EndSelect
                GUICtrlSetCursor($aGrid[$iCol][$iRow][$iGrid_gID], $MCID_ARROW)
            Next
        Next
        Local $iResult
        For $iRow = 0 To $iRowCount - 1
            $iResult = 0
            For $iCol = 0 To $iColCount - 1
                If $aGrid[$iCol][$iRow][$iGrid_bSelected] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue])
            Next
            If $iResult = 0 Then $bNoZeroResults = False
            GUICtrlSetData($aResultOfRow[$iRow][$iResult_iValue], $iResult)
            GUICtrlSetFont($aResultOfRow[$iRow][$iResult_iValue], _Min($iSqrSize / 3, $iSqrSize / StringLen($iResult)))
        Next
        For $iCol = 0 To $iColCount - 1
            $iResult = 0
            For $iRow = 0 To $iRowCount - 1
                If $aGrid[$iCol][$iRow][$iGrid_bSelected] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue])
            Next
            If $iResult = 0 Then $bNoZeroResults = False
            GUICtrlSetData($aResultOfCol[$iCol][$iResult_iValue], $iResult)
            GUICtrlSetFont($aResultOfCol[$iCol][$iResult_iValue], _Min($iSqrSize / 3, $iSqrSize / StringLen($iResult)))
        Next
    Until $bNoZeroResults
    GUISetState(@SW_UNLOCK)
EndFunc   ;==>Renew

Func Accumulate(ByRef $iBase, $iDelta)
    If $iBase = 0 Then Return $iDelta
    If $bPuzzleTypeIsAddition Then
        Return $iBase + $iDelta
    Else
        Return $iBase * $iDelta
    EndIf
EndFunc   ;==>Accumulate

Func Solve()
    $bUserInputAllowed = False
    For $iRow = 0 To $iRowCount - 1
        For $iCol = 0 To $iColCount - 1
            GUICtrlSetBkColor($aGrid[$iCol][$iRow][$iGrid_gID], $aGrid[$iCol][$iRow][$iGrid_bSelected] ? $iColor_BoxChecked : $iColor_BoxEliminated)
            If $aGrid[$iCol][$iRow][$iGrid_bSelected] <> $aGrid[$iCol][$iRow][$iGrid_bChecked] Then GUICtrlSetColor($aGrid[$iCol][$iRow][$iGrid_gID], $iColor_BoxTextError)
            GUICtrlSetCursor($aGrid[$iCol][$iRow][$iGrid_gID], $MCID_NO)
        Next
    Next
EndFunc   ;==>Solve

Func IsVictoryAchieved()
    Local $iResult
    For $iRow = 0 To $iRowCount - 1
        $iResult = 0
        For $iCol = 0 To $iColCount - 1
            If $aGrid[$iCol][$iRow][$iGrid_bChecked] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue])
        Next
        If GUICtrlRead($aResultOfRow[$iRow][$iResult_iValue]) <> $iResult Then Return False
    Next
    For $iCol = 0 To $iColCount - 1
        $iResult = 0
        For $iRow = 0 To $iRowCount - 1
            If $aGrid[$iCol][$iRow][$iGrid_bChecked] Then $iResult = Accumulate($iResult, $aGrid[$iCol][$iRow][$iGrid_iValue])
        Next
        If GUICtrlRead($aResultOfCol[$iCol][$iResult_iValue]) <> $iResult Then Return False
    Next
    Return True
EndFunc   ;==>IsVictoryAchieved

Func ShowVictory()
    Sleep(150)
    Local $gV = GUICtrlCreateLabel($symV, 0, 0, $iGUI_Width, $iGUI_Height, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont($gV, _Min($iGUI_Height, $iGUI_Width) * 0.6, Default, Default, 'Wingdings')
    GUICtrlSetBkColor($gV, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor($gV, $iColor_Vprimary)
    For $i = 1 To 2
        Sleep(125)
        GUICtrlSetColor($gV, $iColor_Vsecondary)
        Sleep(125)
        GUICtrlSetColor($gV, $iColor_Vprimary)
    Next
    Sleep(1000)
    GUICtrlDelete($gV)
    Sleep(250)
EndFunc   ;==>ShowVictory

Func BuildGUI($bNew = True)
    GUISetFont($iSqrSize / 3, Default, Default, 'Tahoma')
    ; GUI contents
    $gdGUIStart = GUICtrlCreateDummy()
    ; - menu
    $gMenu_Pref = GUICtrlCreateLabel($symSetup, 1, 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetTip(-1, 'Set your preferences and construct a new puzzle', 'Setup')
    GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Webdings')
    GUICtrlSetColor(-1, $iColor_MenuItem)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $gMenu_New = GUICtrlCreateLabel($symRenew, 1, $iSqrSize + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetTip(-1, 'Construct a new puzzle using current preferences', 'New Puzzle')
    GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Wingdings 3')
    GUICtrlSetColor(-1, $iColor_MenuItem)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $gMenu_Solve = GUICtrlCreateLabel($symV, 1, $iSqrSize * 2 + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetTip(-1, 'I give up :(', 'Show Solution')
    GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Wingdings')
    GUICtrlSetColor(-1, $iColor_MenuItem)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $gMenu_Help = GUICtrlCreateLabel($symHelp, 1, $iSqrSize * 3 + 1 + $iSqrSize / 2, $iSqrSize * 1.5 - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetTip(-1, 'What do I do here?', 'Instructions')
    GUICtrlSetFont(-1, $iSqrSize / 2, Default, Default, 'Webdings')
    GUICtrlSetColor(-1, $iColor_MenuItem)
    GUICtrlSetCursor(-1, $MCID_HAND)
    ; - grid
    ; -- center part
    ReDim $aGrid[$iColCount][$iRowCount][$iGrid_iDimCount]
    $gdGridStart = GUICtrlCreateDummy()
    For $iRow = 0 To $iRowCount - 1
        For $iCol = 0 To $iColCount - 1
            $aGrid[$iCol][$iRow][$iGrid_gID] = GUICtrlCreateLabel('', ($iCol + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRow * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
        Next
    Next
    $gdGridEnd = GUICtrlCreateDummy()
    ; -- results of rows
    ReDim $aResultOfRow[$iRowCount][$iResult_iDimCount]
    For $iRow = 0 To $iRowCount - 1
        $aResultOfRow[$iRow][$iResult_iValue] = GUICtrlCreateLabel('', ($iColCount + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRow * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
        GUICtrlSetBkColor(-1, $iColor_BoxResult)
    Next
    ; -- results of cols
    ReDim $aResultOfCol[$iColCount][$iResult_iDimCount]
    For $iCol = 0 To $iColCount - 1
        $aResultOfCol[$iCol][$iResult_iValue] = GUICtrlCreateLabel('', ($iCol + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRowCount * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
        GUICtrlSetBkColor(-1, $iColor_BoxResult)
    Next
    ; -- puzzle type
    GUICtrlCreateLabel($bPuzzleTypeIsAddition ? $symP : $symX, ($iColCount + 1) * $iSqrSize + 2 + $iSqrSize / 2, $iRowCount * $iSqrSize + 2 + $iSqrSize / 2, $iSqrSize - 2, $iSqrSize - 2, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, Default, Default, Default, 'Wingdings 2')
    GUICtrlSetBkColor(-1, $iColor_MenuItemEmphasis)
    GUICtrlSetTip(-1, ($bPuzzleTypeIsAddition ? 'Addition' : 'Multplication'), 'Puzzle Type:')
    ; - populate grid
    Renew($bNew)
    ; finalize GUI
    $gdGUIEnd = GUICtrlCreateDummy()
EndFunc   ;==>BuildGUI

Func Pref()
    Local Const $iSUI_W = 200, $iSUI_H = 280, $iSUI_ButtonH = 30, $iSUI_ButtonSpacing = 10
    GUISetFont(9.5, Default, Default, 'Tahoma')
    Local $x = 10, $y = 10, $dy = 25

    ; delete all controls
    DeleteAllControls()

    ; resize GUI
    WinSetTrans($hGUI, '', 192)
    WinSetTitle($hGUI, '', $sAppName & ' Setup')
    _WinPoseAroundCenter($hGUI, $iSUI_W + $iWDiff, $iSUI_H + $iHDiff, $iResizeSpeed)
    WinSetTrans($hGUI, '', 255)

    ; generate controls
    $gdGUIStart = GUICtrlCreateDummy()
    GUICtrlCreateLabel('Puzzle', $x, $y, 85, Default, $SS_CENTERIMAGE)
    GUICtrlSetFont(-1, Default, 600)
    $x += 5
    $y += $dy
    GUICtrlCreateLabel('Puzzle Type:', $x, $y, 80, Default, $SS_CENTERIMAGE)
    $x += 90
    $gPuzzleType_Addition = GUICtrlCreateLabel($symP, $x, $y, 41, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 12, Default, Default, 'Wingdings 2')
    $x += 42
    $gPuzzleType_Multiplication = GUICtrlCreateLabel($symX, $x, $y, 41, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 12, Default, Default, 'Wingdings 2')
    Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication)
    $x -= 42
    $x -= 90
    $y += $dy
    GUICtrlCreateLabel('Columns:', $x, $y, 80, Default, $SS_CENTERIMAGE)
    $x += 90
    $gGridWidth_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x += 25
    $gGridWidth_Data = GUICtrlCreateLabel($iColCount, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE)
    $x += 35
    $gGridWidth_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x -= 35
    $x -= 25
    $x -= 90
    $y += $dy
    GUICtrlCreateLabel('Rows:', $x, $y, Default, Default, $SS_CENTERIMAGE)
    $x += 90
    $gGridHeight_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x += 25
    $gGridHeight_Data = GUICtrlCreateLabel($iRowCount, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE)
    $x += 35
    $gGridHeight_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x -= 35
    $x -= 25
    $x -= 90
    $y += $dy
    $x -= 5
    $y += $dy
    GUICtrlCreateLabel('Appearance', $x, $y, 85, Default, $SS_CENTERIMAGE)
    GUICtrlSetFont(-1, Default, 600)
    $y += $dy
    $x += 5
    GUICtrlCreateLabel('Square Size:', $x, $y, Default, Default, $SS_CENTERIMAGE)
    $x += 90
    $gSqrSize_Down = GUICtrlCreateLabel('-', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x += 25
    $gSqrSize_Data = GUICtrlCreateLabel($iSqrSize, $x, $y, 33, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetBkColor(-1, $COLOR_LIGHTBLUE)
    $x += 35
    $gSqrSize_Up = GUICtrlCreateLabel('+', $x, $y, 23, 22, BitOR($SS_CENTER, $SS_CENTERIMAGE))
    GUICtrlSetFont(-1, 13, 800)
    GUICtrlSetBkColor(-1, $COLOR_LIGHTGRAY)
    GUICtrlSetCursor(-1, $MCID_HAND)
    $x -= 35
    $x -= 25
    $x -= 90
    $y += $dy
    GUICtrlCreateLabel('Resize speed:', $x, $y, Default, Default, $SS_CENTERIMAGE)
    $x += 90
    $gResizeSpeed_data = GUICtrlCreateSlider($x, $y, 80)
    GUICtrlSetLimit(-1, 100)
    GUICtrlSetData(-1, $iResizeSpeed)
    $x -= 90

    #cs
    FUTURE DEVELOPMENT
    ----- BUILD PHASE:
    allow zero sum/prod
    use 8 & 9 in multiplication puzzle (HARDER)
    increase/decrease target squares count (determine probability of a square to participate)
    allow negative numbers (HARDER)
    ----- GAME PHASE:
    show target squares count
    show current/target squares count
    allow marking cols & rows as balanced
    automatically mark cols & rows as balanced
    automatically mark invalid selection (div creating mod, sum/prod exceeding result)
    show diff calc
    allow hints
    allow real-time switch puzzle type
    #ce

    Local $gOK = GUICtrlCreateButton('Done', $iSUI_ButtonSpacing, $iSUI_H - $iSUI_ButtonH - $iSUI_ButtonSpacing, $iSUI_W - $iSUI_ButtonSpacing * 2, $iSUI_ButtonH)
    $gdGUIEnd = GUICtrlCreateDummy()
    ; user interaction
    While True
        $msg = GUIGetMsg()
        Switch $msg
            Case $gPuzzleType_Addition
                If Not $bPuzzleTypeIsAddition Then
                    $bPuzzleTypeIsAddition = True
                    Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication)
                EndIf
            Case $gPuzzleType_Multiplication
                If $bPuzzleTypeIsAddition Then
                    $bPuzzleTypeIsAddition = False
                    Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication)
                EndIf
            Case $gGridWidth_Up
                $iColCount = _Min($iColCount + 1, 9)
                GUICtrlSetData($gGridWidth_Data, $iColCount)
            Case $gGridWidth_Down
                $iColCount = _Max($iColCount - 1, 3)
                GUICtrlSetData($gGridWidth_Data, $iColCount)
            Case $gGridHeight_Up
                $iRowCount = _Min($iRowCount + 1, 9)
                GUICtrlSetData($gGridHeight_Data, $iRowCount)
            Case $gGridHeight_Down
                $iRowCount = _Max($iRowCount - 1, 3)
                GUICtrlSetData($gGridHeight_Data, $iRowCount)
            Case $gSqrSize_Up
                $iSqrSize = _Min($iSqrSize + 5, 60)
                GUICtrlSetData($gSqrSize_Data, $iSqrSize)
            Case $gSqrSize_Down
                $iSqrSize = _Max($iSqrSize - 5, 20)
                GUICtrlSetData($gSqrSize_Data, $iSqrSize)
            Case $gOK, $GUI_EVENT_CLOSE
                $iResizeSpeed = GUICtrlRead($gResizeSpeed_data)
                ExitLoop
        EndSwitch
    WEnd

    ; delete all controls
    DeleteAllControls()

    ; - resize GUI
    WinSetTrans($hGUI, '', 192)
    WinSetTitle($hGUI, '', $sAppName)
    $iGUI_Width = $iSqrSize * ($iColCount + 3) + 2
    $iGUI_Height = $iSqrSize * ($iRowCount + 2) + 2
    _WinPoseAroundCenter($hGUI, $iGUI_Width + $iWDiff, $iGUI_Height + $iHDiff, $iResizeSpeed)
    WinSetTrans($hGUI, '', 255)

    Return True
EndFunc   ;==>Pref

Func Pref_RefreshPuzzleTypeButtons($gPuzzleType_Addition, $gPuzzleType_Multiplication)
    GUICtrlSetBkColor($gPuzzleType_Addition, $bPuzzleTypeIsAddition ? $COLOR_LIGHTBLUE : $COLOR_LIGHTGRAY)
    GUICtrlSetCursor($gPuzzleType_Addition, $bPuzzleTypeIsAddition ? $MCID_ARROW : $MCID_HAND)
    GUICtrlSetBkColor($gPuzzleType_Multiplication, $bPuzzleTypeIsAddition ? $COLOR_LIGHTGRAY : $COLOR_LIGHTBLUE)
    GUICtrlSetCursor($gPuzzleType_Multiplication, $bPuzzleTypeIsAddition ? $MCID_HAND : $MCID_ARROW)
EndFunc   ;==>Pref_RefreshPuzzleTypeButtons

Func Help()
    Local Const $iSUI_W = 410, $iSUI_H = 350, $iSUI_ButtonH = 30, $iSUI_ButtonSpacing = 10
    GUISetFont(9.5, Default, Default, 'Tahoma')
    Local $x = 15, $y = 10, $dy = 25

    ; delete all controls
    DeleteAllControls()

    ; resize GUI
    WinSetTrans($hGUI, '', 192)
    WinSetTitle($hGUI, '', $sAppName & ' Instructions')
    _WinPoseAroundCenter($hGUI, $iSUI_W + $iWDiff, $iSUI_H + $iHDiff, $iResizeSpeed)
    WinSetTrans($hGUI, '', 255)

    ; generate controls
    $gdGUIStart = GUICtrlCreateDummy()
    GUICtrlCreateLabel('WARNING!  This game may be highly addictive :-)' & @CRLF & @CRLF & _
            'Click the gray squares in the middle to select them and paint them green. The selected squares in every row must match the result of the row, displayed on the right-hand side; the selected squares in every column must match the result of the column, displayed at the bottom.' & @CRLF & @CRLF & _
            'If you eliminate a square, you may right-click it to paint it dark gray.' & @CRLF & @CRLF & _
            'The type of the puzzle - Addition or Multiplication - is indicated in the white square at the bottom-right corner.' & @CRLF & @CRLF & _
            'Click the "Tools" icon on the upper-left corner to change puzzle type, grid size and other preferences.' & @CRLF & @CRLF & _
            'Enjoy :-)', $x, $y, $iSUI_W - $x * 2, $iSUI_H - 60)
    $y += $dy
    $gOK = GUICtrlCreateButton('OK, I think I got it...', $iSUI_ButtonSpacing, $iSUI_H - $iSUI_ButtonH - $iSUI_ButtonSpacing, $iSUI_W - $iSUI_ButtonSpacing * 2, $iSUI_ButtonH)
    $gdGUIEnd = GUICtrlCreateDummy()
    ; user interaction
    While True
        $msg = GUIGetMsg()
        Switch $msg
            Case $gOK, $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd

    ; delete all controls
    DeleteAllControls()

    ; resize GUI
    WinSetTrans($hGUI, '', 192)
    WinSetTitle($hGUI, '', $sAppName)
    _WinPoseAroundCenter($hGUI, $iGUI_Width + $iWDiff, $iGUI_Height + $iHDiff, $iResizeSpeed)
    WinSetTrans($hGUI, '', 255)

    Return False
EndFunc   ;==>Help

Func GuiCtrlDeleteAndZero(ByRef $gID)
    If GUICtrlDelete($gID) Then
        $gID = 0
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>GuiCtrlDeleteAndZero

Func DeleteAllControls()
    Local $gIDStart = $gdGUIStart
    Local $gIDEnd = $gdGUIEnd
    For $gID = $gIDStart To $gIDEnd
        GuiCtrlDeleteAndZero($gID)
    Next
EndFunc   ;==>DeleteAllControls

Func _WinPoseAroundCenter($hGUI, $iNewWidth, $iNewHeight, $iSpeed = 0)
    Local $aPos = WinGetPos($hGUI)
    Local $x = $aPos[0] + $aPos[2] / 2 - $iNewWidth / 2
    If $x < 0 Then $x = 0
    If $x + $iNewWidth > @DesktopWidth Then $x = @DesktopWidth - $iNewWidth
    Local $y = $aPos[1] + $aPos[3] / 2 - $iNewHeight / 2
    If $y < 0 Then $y = 0
    If $y + $iNewHeight > @DesktopHeight Then $y = @DesktopHeight - $iNewHeight
    _WinPose($hGUI, '', $x, $y, $iNewWidth, $iNewHeight, $iSpeed)
EndFunc   ;==>_WinPoseAroundCenter

enjoy!

 

P.S. what's with the 12KB size limit on the screenshot? 🤔

MagicMath.jpg

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Posted

Very nice @orbs 👌 .

Looks good, feels good. I enjoy such tiny educational games - thank you!

---------------------
One hint: Please get rid of line 11 which leads to errors 😅 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
16 minutes ago, SOLVE-SMART said:

One hint: Please get rid of line 11 which leads to errors 😅 .

sorry about that, must've been a copy/paste issue. fixed.

b.t.w. who are you calling "tiny"? 🙂 try a larger grid...

 

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Posted
3 minutes ago, orbs said:

b.t.w. who are you calling "tiny"? 🙂 try a larger grid...

🤣 .. I did, it's pretty hard I have to admit. Again, really nice, thanks.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
4 hours ago, orbs said:

P.S. what's with the 12 KB size limit on the screenshot? 🤔

As far as I remember correctly, there is a cumulative storage space for each member (5 MB?). With each attachment posted, this space becomes smaller. The amount of free storage space is displayed at the bottom of each new post. If necessary, you will need to delete attachments from older posts to free up space.

Drag files here to attach, or choose files...
Max total size: 1.64 MB

BTW. : Nice work :thumbsup:

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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...