Jump to content
Sign in to follow this  
Underdogger

Guess Number

Recommended Posts

Underdogger

I don't know what wrong with my code is?

When I start guessing, but it appeared "Must be in the Range of..." looping .

I don't know what the problem with my logic is ?

While 1
Local $setMax = InputBox("","Input Max Num:")
Local $setMin = InputBox("","Input Min Num:")
Local $MXtemp = $setMax
Local $MNtemp = $setMin
$RandomNum = Random($setMin, $setMax, 1)
;debug MsgBox(0, "", $RandomNum)
Do
$Guess = InputBox("","Guess Number:")
If @error <> 1 Then
If $Guess < $setMax And $Guess > $setMin Then
If $Guess < $RandomNum Then
$setMin = $Guess
$MNtemp = $setMin
MsgBox(0, "", "Too high..." & @CRLF & $setMin & "-" & $setMax)
ElseIf $Guess > $RandomNum Then
$setMax = $Guess
$MXtemp = $setMax
MsgBox(0, "", "Too low..." & @CRLF & $setMin & "-" & $setMax)
Else
MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
ExitLoop
EndIf
Else
$setMax = $MXtemp
$setMin = $MNtemp
MsgBox(0, "", "Must be in the Range of" & @CRLF & $setMin & " and "& $setMax)
EndIf
Else
ExitLoop
EndIf
Until $Guess == $RandomNum
$again = MsgBox(1, "", "Continue?")
If $again == 2 Then ExitLoop
WEnd

And this is the 1st ver. of this code...

It can be run normally..↓

While 1
Dim $High = 100, $MXtemp = 100, $Low = 1, $MNtemp = 1
$RandomNum = Random(1, 100, 1)
Do
$Guess = InputBox("", "Guess Number:")
If @error <> 1 Then
If $Guess < $High And $Guess > $Low Then
If $Guess < $RandomNum Then
$Low = $Guess
$MNtemp = $Low
MsgBox(0, "", "Too low..." & @CRLF & $Low & "-" & $High)
ElseIf $Guess > $RandomNum Then
$High = $Guess
$MXtemp = $High
MsgBox(0, "", "Too high..." & @CRLF & $Low & "-" & $High)
Else
MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
ExitLoop
EndIf
Else
$High = $MXtemp
$Low = $MNtemp
MsgBox(0, "", "Must be in the Range of" & @CRLF & $Low & " and " & $High)
EndIf
Else
ExitLoop
EndIf
Until $Guess == $RandomNum
$again = MsgBox(1, "", "Continue?")
If $again == 2 Then ExitLoop
WEnd
Edited by Underdogger

Share this post


Link to post
Share on other sites
Melba23

Underdogger,

What does it not do that you think it should? :huh:

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Underdogger

Underdogger,

What does it not do that you think it should? :huh:

M23

When I start guessing, but it appeared "Must be in the Range of..." looping .

I don't know what the problem with my logic is ?

My 1st ver. it can run normally..

but while I added two inputboxs on top, It got error..

Edited by Underdogger

Share this post


Link to post
Share on other sites
Melba23

Underdogger,

Your logic was seriously flawed, but rather than go into huge detail about why I have rewritten it for you:

While 1
    Local $setMax = InputBox("", "Input Max Num:")
    Local $setMin = InputBox("", "Input Min Num:")

    $RandomNum = Random($setMin, $setMax, 1)

    While 1
        $Guess = InputBox("", "Guess Number:")
        If @error <> 1 Then
            If $Guess = $RandomNum Then
                MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
                If MsgBox(1, "", "continue?") = 2 Then
                    Exit
                Else
                    ExitLoop
                EndIf
            Else
                Select
                    Case $Guess > $setMax Or $Guess < $setMin
                        MsgBox(0, "", "Must be in the Range of" & @CRLF & $setMin & " and " & $setMax)
                    Case $Guess < $RandomNum
                        MsgBox(0, "", "Too low..." & @CRLF & $setMin & "-" & $setMax)
                    Case $Guess > $RandomNum
                        MsgBox(0, "", "Too high..." & @CRLF & $setMin & "-" & $setMax)
                EndSelect
            EndIf
        EndIf
    WEnd
WEnd

Can you follow that logic? Please ask if not. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Underdogger

Why are you doing this in your logic?:

$setMin = $Guess
$MNtemp = $setMin

Avoiding who set wrong number out of range...

Share this post


Link to post
Share on other sites
Underdogger

Underdogger,

Your logic was seriously flawed, but rather than go into huge detail about why I have rewritten it for you:

While 1
Local $setMax = InputBox("", "Input Max Num:")
Local $setMin = InputBox("", "Input Min Num:")

$RandomNum = Random($setMin, $setMax, 1)

While 1
$Guess = InputBox("", "Guess Number:")
If @error <> 1 Then
If $Guess = $RandomNum Then
MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
If MsgBox(1, "", "continue?") = 2 Then
Exit
Else
ExitLoop
EndIf
Else
Select
Case $Guess > $setMax Or $Guess < $setMin
MsgBox(0, "", "Must be in the Range of" & @CRLF & $setMin & " and " & $setMax)
Case $Guess < $RandomNum
MsgBox(0, "", "Too low..." & @CRLF & $setMin & "-" & $setMax)
Case $Guess > $RandomNum
MsgBox(0, "", "Too high..." & @CRLF & $setMin & "-" & $setMax)
EndSelect
EndIf
EndIf
WEnd
WEnd

Can you follow that logic? Please ask if not. :)

M23

But I want to let other know the range of two numbers,

Exp. The correct random Num is 45 (min1 - max100).

while I input 55, it will tell other "1-55" and start guessing between those two numbers...

Edited by Underdogger

Share this post


Link to post
Share on other sites
Melba23

Underdogger,

Understood - so this should do the trick: :)

While 1
    Local $setMax = InputBox("", "Input Max Num:")
    Local $setMin = InputBox("", "Input Min Num:")
    Local $GuessMax = $setMax
    Local $GuessMin = $setMin
    $RandomNum = Random($setMin, $setMax, 1)

    While 1
        $Guess = InputBox("", "Guess Number:")
        If @error <> 1 Then
            If $Guess = $RandomNum Then
                MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
                If MsgBox(1, "", "continue?") = 2 Then
                    Exit
                Else
                    ExitLoop
                EndIf
            Else
                Select
                    Case $Guess > $setMax Or $Guess < $setMin
                        MsgBox(0, "", "Must be in the Range of" & @CRLF & $setMin & " and " & $setMax)
                    Case $Guess < $RandomNum
                        If $Guess > $GuessMin Then
                            $GuessMin = $Guess
                        EndIf
                        MsgBox(0, "", "Too low..." & @CRLF & $GuessMin & "-" & $GuessMax)
                    Case $Guess > $RandomNum
                        If $Guess < $GuessMax Then
                            $GuessMax = $Guess
                        EndIf
                        MsgBox(0, "", "Too high..." & @CRLF & $GuessMin & "-" & $GuessMax)
                EndSelect
            EndIf
        EndIf
    WEnd
WEnd

Now we reset the limits with each incorrect guess. :)

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______Open Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Underdogger

Underdogger,

Understood - so this should do the trick: :)

While 1
Local $setMax = InputBox("", "Input Max Num:")
Local $setMin = InputBox("", "Input Min Num:")
Local $GuessMax = $setMax
Local $GuessMin = $setMin
$RandomNum = Random($setMin, $setMax, 1)

While 1
$Guess = InputBox("", "Guess Number:")
If @error <> 1 Then
If $Guess = $RandomNum Then
MsgBox(0, "", "Excellent!" & @CRLF & "The Number is:" & $RandomNum)
If MsgBox(1, "", "continue?") = 2 Then
Exit
Else
ExitLoop
EndIf
Else
Select
Case $Guess > $setMax Or $Guess < $setMin
MsgBox(0, "", "Must be in the Range of" & @CRLF & $setMin & " and " & $setMax)
Case $Guess < $RandomNum
If $Guess > $GuessMin Then
$GuessMin = $Guess
EndIf
MsgBox(0, "", "Too low..." & @CRLF & $GuessMin & "-" & $GuessMax)
Case $Guess > $RandomNum
If $Guess < $GuessMax Then
$GuessMax = $Guess
EndIf
MsgBox(0, "", "Too high..." & @CRLF & $GuessMin & "-" & $GuessMax)
EndSelect
EndIf
EndIf
WEnd
WEnd

Now we reset the limits with each incorrect guess. :)

M23

Sorry for my bad expression to let you misunderstand..

It works pretty...

Share this post


Link to post
Share on other sites
Underdogger

.. but you are overriding the original range (Min and Max) and loose your original values.

Sorry for my bad expression...

Melba's version is running pretty!

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  

×