Sign in to follow this  
Followers 0
crashdemons

Word Unscrambler

3 posts in this topic

Notes: Requires a "dict.txt" file with words null-delimited.

Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Word Unscrambler", 159, 78, 193, 115)
GUISetOnEvent(-3,'_exit')
$Label1 = GUICtrlCreateLabel("Letters", 1, 1, 154, 17, 1)
$Input1 = GUICtrlCreateInput("deeccsu", 1, 19, 154, 21, BitOR(1,128))
$Label2 = GUICtrlCreateLabel("succeed", 1, 41, 154, 17, 1, 0x00020000)
$Button1 = GUICtrlCreateButton("Unscramble", 1, 59, 154, 17, 0)
GUICtrlSetOnEvent($Button1,'_unscamble')
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $DictData=FileReadFull('dict.txt')
$DictData=chr(0)&$DictData&chr(0)


Global $Permute=False
Global $PermutationsT=1
Global $PermutationsI=1
Global $PermuteTimer=0
While 1
    Sleep(300)
    If $Permute Then
        $PermuteTimer=TimerInit()
        _permutations(GUICtrlRead($Input1),-1)
        $PermuteTimer=0
        $Permute=False
    EndIf
WEnd

Func _unscamble()
    Switch $Permute
        Case True
            GUICtrlSetState($Input1,128)
            GUICtrlSetState($Button1,128)
            $Permute=False
            $PermuteTimer=0
            GUICtrlSetData($Button1,'Unscramble')
            GUICtrlSetState($Button1,64)
            GUICtrlSetState($Input1,64)
        Case False
            GUICtrlSetState($Input1,128)
            GUICtrlSetState($Button1,128)
            $Permute=True
            GUICtrlSetData($Button1,'Stop')
            GUICtrlSetState($Button1,64)
    EndSwitch
EndFunc
Func _exit()
    GUISetState(@SW_HIDE)
    $Permute=False
    Sleep(500)
    Exit
EndFunc
Func _permutations($string,$max=-1,$pos=1,$pre='')
    If $Permute=False Then Return
    ;NOTES: do NOT use the $pos and $pre parameters - they are interally-specfic.
    Local $ch
    $sl=StringLen($string)
    If $max=-1 Then $max=$sl
    If $pos=1 Then
        $PermutationsT=_Possibilities($sl)
        $PermutationsI=0
        ConsoleWrite("Starting "&$PermutationsT&" Permutations List."&@CRLF)
    EndIf
    For $i=1 To $sl
        $ch=StringMid($string,$i,1)
        If $pos<$max Then
            $sstring=$string
            $sstring=_stringrc($string,$i)
            _permutations($sstring,$max,$pos+1,$pre&$ch)
        Else
            $Permutation=$pre&$ch
            $PermutationsI+=1
            UpdatePump($PermutationsI,$PermutationsT,$Permutation)
            $isword=_IsWord($Permutation)
            If $isword Then
                ConsoleWrite($Permutation&'@'&$isword&@CRLF)
                GUICtrlSetData($Label2,$Permutation)
                Return _unscamble()
            EndIf
        EndIf
    Next
    If $pos=1 And $Permute Then
        MsgBox(0,'Word Unscramble','None of the permutations were listed in the dictionary.')
        Return _unscamble()
    EndIf
EndFunc
Func UpdatePump($i,$x,$w)
    Global $UpdatePump
    Global $UpdateOldB
    Global $UpdateOldL
    If TimerDiff($UpdatePump)>100 Then
        Local $p=0
        If $x>=1 Then $p=_ToPercent($i, 0, $x, 0)
        If $x<1 Then $p='??'
        Local $estim=_estimatedtime($i,$x,$PermuteTimer)
        Local $updateB='Stop ['&$p&'%; '&$estim&' remaning]'
        If $UpdateOldB<>$updateB Then
            GUICtrlSetData($Button1,$updateB)
            $UpdateOldB=$updateB
        EndIf
        GUICtrlSetData($Label2,$w)
        If $UpdateOldL<>$w Then
            GUICtrlSetData($Label2,$w)
            $UpdateOldL=$w
        EndIf
        $UpdatePump=TimerInit()
    EndIf
EndFunc

Func _estimatedtime($i,$x,$init_timer)
    Local $t=TimerDiff($init_timer)/1000
    Local $cps=$i/$t
    Local $s=($x-$i)/$cps
    $s=Round($s,0)
    Local $m=Int($s/60)
    $s-=$m*60
    Local $h=Int($m/60)
    $m-=$h*60
    Local $out=''
    Return $h&':'&$m&':'&$s
EndFunc


Func _stringrc($string,$pos)
    Local $out=''
    For $i=1 To StringLen($string)
        If $i<>$pos Then $out&=StringMid($string,$i,1)
    Next
    Return $out
EndFunc



Func _Possibilities($length)
    If $length<1 Then Return 0
    $Value=1
    For $i=$length To 1 Step -1
        $Value*=$i
    Next
    Return $Value
EndFunc


Func _IsWord($Word)
    $pattern=Chr(0)&$Word&Chr(0)
    $match=StringInStr($DictData,$pattern,0)
    If $match Then
        ;I have to double-check because for some reason  r_stare_ will match _rstare_ (where _ are Chr(0) )
        $simword=StringMid($DictData,$match+1,StringLen($pattern)-2)
        If $simword=$Word Then Return $match
    EndIf
    Return 0
EndFunc

Func _ToPercent($i, $n, $x, $round = -1)
    Dim $round
    ;converts a number in a range of numbers
    ;proportionally to number in a range from 0 to 100
    ;despite whether the numbers are negative or not
    $p = 100 * (($i - $n) / ($x - $n));
    If $round > -1 Then $p = Round($p, $round)
    Return $p
EndFunc   ;==>_ToPercent

Func FileReadFull($file)
    $fh = FileOpen($file, 16)
    $d = BinaryToString(FileRead($fh))
    FileClose($fh)
    Return $d
EndFunc   ;==>FileReadFull

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites



:P Really good work, too bad your logic is really hard to follow (or maybe i'm just a bit thickheaded today :unsure:)

All it did was re-arrange the letters of the input according to all of the different possibilites and check each one against a dictionary file until it found a match. (or reached the last possibility)

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
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
Sign in to follow this  
Followers 0