Jump to content
Sign in to follow this  
LithiumLi

Unfinished Loop?

Recommended Posts

LithiumLi

I am making a hangman game so far so good, I think I may be over tired though.

When I press "H" and "A" it get's it correct but when I push other letters it catches it because it doesn't write to the console but does not display the letter on the gaming board?

Shouldn't StringMid be catching all the letters?

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

$Word = "HELLOA"
Global $Letter[14] , $Button[91]

GUICreate("Hang", 650, 600)
;GUISetBkColor(0)

$LabelLeft = 0
For $i = 1 To 13 Step +1
    $Letter[$i] = GUICtrlCreateLabel("", $LabelLeft, 0, 50, 50)
    $LabelLeft+= 50
    GUICtrlSetFont($Letter[$i] , 24, 400, 0, "MS Sans Serif")
    GUICtrlSetColor($Letter[$i], 0xFF0000)
    ;GUICtrlSetBkColor($Letter[$i], 0x000000)
Next

$ButtonLeft = 0
For $i = 65 To 90 Step +1
    $Button[$i] = GUICtrlCreateButton(Chr($i) , $ButtonLeft , 600 - 20 , 25 , 20)
    $ButtonLeft+= 25
Next

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    For $i = 65 to 90
        Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button[$i]
            _IsInString($i , $Word)
        EndSwitch
    Next
WEnd

Func _IsInString($iNum , $iWord)
    GUICtrlSetState($Button[$iNum] , $GUI_DISABLE)
    If StringInStr($iWord , Chr($iNum)) Then 
        For $i = 1 To StringLen($iWord) Step +1
            ;If StringLeft($iWord , $i) = Chr($iNum) Then
            ;   GUICtrlSetData($Letter[$i] , Chr($iNum))
            ;EndIf
            If StringMid($iWord , $i , $i) = Chr($iNum) Then
                ConsoleWrite(StringMid($iWord , $i , $i) & @CRLF)
                GUICtrlSetData($Letter[$i] , Chr($iNum))
            EndIf
        Next
    Else
        ConsoleWrite("Error @ " & $iNum & " Convert = " & Chr($iNum) & @CRLF)
    EndIf
EndFunc
Edited by LithiumLi

Share this post


Link to post
Share on other sites
Tomb

this doesn't fix your problem, but it really shortens the while loop.

While 1
$Msg = GUIGetMsg()
    For $o = 65 to 90

        Switch $Msg
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $Button[$o]
                    _IsInString($o , $Word)
        EndSwitch
    Next
WEnd

Share this post


Link to post
Share on other sites
LithiumLi

this doesn't fix your problem, but it really shortens the while loop.

While 1
$Msg = GUIGetMsg()
    For $o = 65 to 90

        Switch $Msg
                Case $GUI_EVENT_CLOSE
                    Exit
                Case $Button[$o]
                    _IsInString($o , $Word)
        EndSwitch
    Next
WEnd
I should of thought of that, thanks very much ;) hmm you got any clue why it is being a pain for me lol.

Main Problem still not fixed for those of you who read this ^_^

Edited by LithiumLi

Share this post


Link to post
Share on other sites
smashly

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

Global $Word = "HELLOA", $sTmp = $Word
Global $Letter[14], $Button[91], $lX, $bX

$hGui = GUICreate("Hang", 650, 600)
For $i = 1 To 90
    If $i = 14 Then $i = 65
    If $i < 14 Then 
        $Letter[$i] = GUICtrlCreateLabel("", $lX, 0, 50, 50)
        GUICtrlSetFont(-1 , 24, 400, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)     
        $lX += 50
    ElseIf $i > 64 Then
        $Button[$i] = GUICtrlCreateButton(Chr($i) , $bX, 600 - 20 , 25 , 20)
        $bX += 25
    EndIf
Next    
GUISetState(@SW_SHOW, $hGui)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button[65] To $Button[90]
            $sTmp = _IsInString($Msg , $sTmp)
        Case Else
            StringReplace($sTmp, "*", "*")
            Local $Chk = @extended
            If $Chk = StringLen($sTmp) Then WinSetTitle($hGui, "", "Hang - Game Over - You Win!")
    EndSwitch
WEnd

Func _IsInString($id , $iWord)
    ;GUICtrlSetState($id , $GUI_DISABLE) ;Can't do words with 2 letters eg: 'LL' in "HELLOA"
    Local $sLetter, $IsInWord
    $sLetter = GUICtrlRead($id)
    $IsInWord = StringInStr($iWord, $sLetter)
    If $IsInWord Then 
        GUICtrlSetData($Letter[$IsInWord], $sLetter)
        Return StringReplace($iWord, $sLetter, "*", 1) ;Just in case 2 letters are the same.
    EndIf
    Return $iWord
EndFunc

Share this post


Link to post
Share on other sites
LithiumLi

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

Global $Word = "HELLOA", $sTmp = $Word
Global $Letter[14], $Button[91], $lX, $bX

$hGui = GUICreate("Hang", 650, 600)
For $i = 1 To 90
    If $i = 14 Then $i = 65
    If $i < 14 Then 
        $Letter[$i] = GUICtrlCreateLabel("", $lX, 0, 50, 50)
        GUICtrlSetFont(-1 , 24, 400, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)     
        $lX += 50
    ElseIf $i > 64 Then
        $Button[$i] = GUICtrlCreateButton(Chr($i) , $bX, 600 - 20 , 25 , 20)
        $bX += 25
    EndIf
Next    
GUISetState(@SW_SHOW, $hGui)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button[65] To $Button[90]
            $sTmp = _IsInString($Msg , $sTmp)
        Case Else
            StringReplace($sTmp, "*", "*")
            Local $Chk = @extended
            If $Chk = StringLen($sTmp) Then WinSetTitle($hGui, "", "Hang - Game Over - You Win!")
    EndSwitch
WEnd

Func _IsInString($id , $iWord)
    ;GUICtrlSetState($id , $GUI_DISABLE) ;Can't do words with 2 letters eg: 'LL' in "HELLOA"
    Local $sLetter, $IsInWord
    $sLetter = GUICtrlRead($id)
    $IsInWord = StringInStr($iWord, $sLetter)
    If $IsInWord Then 
        GUICtrlSetData($Letter[$IsInWord], $sLetter)
        Return StringReplace($iWord, $sLetter, "*", 1) ;Just in case 2 letters are the same.
    EndIf
    Return $iWord
EndFunc
This is great, but is it possible to find doubles of letters and do them all at once, if not this project that I had in my mind is ruined ^_^ thanks very much for your code ill be sure you learn off all of your codes.

Share this post


Link to post
Share on other sites
smashly

This is great, but is it possible to find doubles of letters and do them all at once, if not this project that I had in my mind is ruined ^_^ thanks very much for your code ill be sure you learn off all of your codes.

Hi again,

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

Global $Word = "HELLOA", $sTmp = $Word
Global $Letter[14], $Button[91], $lX, $bX

$hGui = GUICreate("Hang", 650, 600)
For $i = 1 To 90
    If $i = 14 Then $i = 65
    If $i < 14 Then 
        $Letter[$i] = GUICtrlCreateLabel("", $lX, 0, 50, 50)
        GUICtrlSetFont(-1 , 24, 400, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)     
        $lX += 50
    ElseIf $i > 64 Then
        $Button[$i] = GUICtrlCreateButton(Chr($i) , $bX, 600 - 20 , 25 , 20)
        $bX += 25
    EndIf
Next    
GUISetState(@SW_SHOW, $hGui)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button[65] To $Button[90]
            $sTmp = _IsInString($Msg , $sTmp)
        Case Else
            StringReplace($sTmp, "*", "*")
            Local $Chk = @extended
            If $Chk = StringLen($sTmp) Then WinSetTitle($hGui, "", "Hang - Game Over - You Win!")
    EndSwitch
WEnd

Func _IsInString($id , $iWord)
    GUICtrlSetState($id , $GUI_DISABLE)
    Local $sLetter, $IsInWord, $SR = $iWord
    $sLetter = GUICtrlRead($id)
    $IsInWord = StringInStr($iWord, $sLetter)
    If $IsInWord Then
        Do
            $IsInWord = StringInStr($SR, $sLetter)
            GUICtrlSetData($Letter[$IsInWord], $sLetter)
            $SR = StringReplace($SR, $sLetter, "*", 1)
        Until Not $IsInWord
        Return $SR
    EndIf
    Return $iWord
EndFunc

Share this post


Link to post
Share on other sites
LithiumLi

Hi again,

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

Global $Word = "HELLOA", $sTmp = $Word
Global $Letter[14], $Button[91], $lX, $bX

$hGui = GUICreate("Hang", 650, 600)
For $i = 1 To 90
    If $i = 14 Then $i = 65
    If $i < 14 Then 
        $Letter[$i] = GUICtrlCreateLabel("", $lX, 0, 50, 50)
        GUICtrlSetFont(-1 , 24, 400, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)     
        $lX += 50
    ElseIf $i > 64 Then
        $Button[$i] = GUICtrlCreateButton(Chr($i) , $bX, 600 - 20 , 25 , 20)
        $bX += 25
    EndIf
Next    
GUISetState(@SW_SHOW, $hGui)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button[65] To $Button[90]
            $sTmp = _IsInString($Msg , $sTmp)
        Case Else
            StringReplace($sTmp, "*", "*")
            Local $Chk = @extended
            If $Chk = StringLen($sTmp) Then WinSetTitle($hGui, "", "Hang - Game Over - You Win!")
    EndSwitch
WEnd

Func _IsInString($id , $iWord)
    GUICtrlSetState($id , $GUI_DISABLE)
    Local $sLetter, $IsInWord, $SR = $iWord
    $sLetter = GUICtrlRead($id)
    $IsInWord = StringInStr($iWord, $sLetter)
    If $IsInWord Then
        Do
            $IsInWord = StringInStr($SR, $sLetter)
            GUICtrlSetData($Letter[$IsInWord], $sLetter)
            $SR = StringReplace($SR, $sLetter, "*", 1)
        Until Not $IsInWord
        Return $SR
    EndIf
    Return $iWord
EndFunc
oh a loop I should of thought of that doh, Your really great really good coder to learn from.

Share this post


Link to post
Share on other sites
smashly

oh a loop I should of thought of that doh, Your really great really good coder to learn from.

Actually you taught me ^_^

I liked your way of putting the letters on the buttons

$Button[$i] = GUICtrlCreateButton(Chr($i) ......
.I'd never thought to do it that way:D

Share this post


Link to post
Share on other sites
Bowmore

If you have not already found the problem with your original code, you made one small mistake with StringMid. The third parameter should be 1 not $i as you only want to compare 1 letter at once with Chr($inum). As written in your original function StringMid would return the following for "HANG MAN"

'H'

'AN'

'NG '

'G MA'

' MAN'

'MAN'

'AN'

'N'

As you can see on the first and last iteration of the for loop could ever match a single letter.

Func _IsInString($iNum , $iWord)
    GUICtrlSetState($Button[$iNum] , $GUI_DISABLE)
    If StringInStr($iWord , Chr($iNum)) Then
        For $i = 1 To StringLen($iWord) Step +1
            ;If StringLeft($iWord , $i) = Chr($iNum) Then
            ;   GUICtrlSetData($Letter[$i] , Chr($iNum))
            ;EndIf
            If StringMid($iWord , $i , 1) = Chr($iNum) Then
                ConsoleWrite(StringMid($iWord , $i , 1) & @CRLF)
                GUICtrlSetData($Letter[$i] , Chr($iNum))
            EndIf
        Next
    Else
        ConsoleWrite("Error @ " & $iNum & " Convert = " & Chr($iNum) & @CRLF)
    EndIf
EndFunc
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.