Sign in to follow this  
Followers 0
nf67

Inserting a random number in an edit box

9 posts in this topic

#1 ·  Posted (edited)

Hi there,

I wanted to make a simple game in which you should press a certain button within a second, after you've pressed it, the program should give you the next button to press ( within one second ).

I've created a simple GUI with 3 buttons ( named 1, 2 and 3 ) and encountered some problems with "the program should give you the next button to press".

Have a look at my code with the description of my problem in it:

#include <GUIConstants.au3>

$Beepor = GUICreate("Beepor", 284, 439, 193, 115)
$Button1 = GUICtrlCreateButton("1", 15, 360, 80, 60, 0)
$Button2 = GUICtrlCreateButton("2", 104, 360, 80, 60, 0)
$Button3 = GUICtrlCreateButton("3", 192, 360, 80, 60, 0)
$Whattodo = GUICtrlCreateEdit("", 8, 16, 265, 337)
GUICtrlSetData(-1, "Press a button!")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1 
            soundplay ( "./Beep1.wav" ) 
            GUICtrlSetData($Whattodo, '-RANDOMNUMBER-'&@crlf, 1)        ;How can I make it insert a random number in $Whattodo ?
    ;????????                       ; How can I make it say WRONG ( in $Whattodo ) if you don't press the number within a second or press a different number than randomly generated one?
        Case $Button2 
            soundplay ( "./Beep2.wav" ) 
            GUICtrlSetData($Whattodo, '-RANDOMNUMBER-'&@crlf, 1)
    ;????????
        Case $Button3 
            soundplay ( "./Beep3.wav" ) 
            GUICtrlSetData($Whattodo, '-RANDOMNUMBER-'&@crlf, 1)
    ;????????
    EndSwitch
WEnd
Edited by nf67

Share this post


Link to post
Share on other sites



Use Random() function.


When the words fail... music speaks

Share this post


Link to post
Share on other sites

Use Random() function.

That would be:

GUICtrlSetData($Whattodo, Random( 1, 3 ) &@crlf, 1)

It works, but it gives a number with a lot of decimals.... I only want 1 2 or 3.. :)

Please help

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

That would be:

GUICtrlSetData($Whattodo, Random( 1, 3 ) &@crlf, 1)

It works, but it gives a number with a lot of decimals.... I only want 1 2 or 3.. :)

Please help

Random(1,3,1) the last parameter force the function to return an integer.

Like this:

#include <GUIConstants.au3>

$Beepor = GUICreate("Beepor", 284, 439, 193, 115)
$Button1 = GUICtrlCreateButton("1", 15, 360, 80, 60, 0)
$Button2 = GUICtrlCreateButton("2", 104, 360, 80, 60, 0)
$Button3 = GUICtrlCreateButton("3", 192, 360, 80, 60, 0)
$Whattodo = GUICtrlCreateEdit("", 8, 16, 265, 337)
GUICtrlSetData(-1, "Press a button!"&@CRLF)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)        ;How can I make it insert a random number in $Whattodo ?
    ;????????                        ; How can I make it say WRONG ( in $Whattodo ) if you don't press the number within a second or press a different number than randomly generated one?
        Case $Button2
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
    ;????????
        Case $Button3
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
    ;????????
    EndSwitch
WEnd
Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

Thanks Andreik, but I still have the other problem left, I described it below:

Case $Button1 
            soundplay ( "./Beep1.wav" ) 
            GUICtrlSetData($Whattodo, Random( 1, 3, 1 ) &@crlf, 1)
        ; Do step 2 and 3, unless the number pressed corresponds with the last number in $Whattodo ( Simplified: DO NOT CONTINUE IF NUMBER CORREPONDS WITH LAST $Whattodo ENTRY ) 
            GUICtrlSetData($Whattodo, "Wrong" &@crlf, 1);Step 2
            GUICtrlSetData($Whattodo, Random( 1, 3, 1 ) &@crlf, 1); Step 3

Share this post


Link to post
Share on other sites

Anyone?

Share this post


Link to post
Share on other sites

Anyone?

Like this? (I`m not a very good english speaker)

#include <GUIConstants.au3>
Global $LAST = 0
$Beepor = GUICreate("Beepor", 284, 439, 193, 115)
$Button1 = GUICtrlCreateButton("1", 15, 360, 80, 60, 0)
$Button2 = GUICtrlCreateButton("2", 104, 360, 80, 60, 0)
$Button3 = GUICtrlCreateButton("3", 192, 360, 80, 60, 0)
$Whattodo = GUICtrlCreateEdit("", 8, 16, 265, 337)
GUICtrlSetData(-1, "Press a button!"&@CRLF)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $LAST <> 1 Then
            Soundplay ( "./Beep1.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
            $LAST = 1
            EndIf
        Case $Button2
            If $LAST <> 2 Then
            Soundplay ( "./Beep2.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
            $LAST = 2
            EndIf
        Case $Button3
            If $LAST <> 3 Then
            Soundplay ( "./Beep3.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
            $LAST = 3
            EndIf
    EndSwitch
WEnd

When the words fail... music speaks

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Mm that's not really it, it seems like this script locks the button after you've pressed it?

If I press 1, I can't press it again until I've pressed a different button.

_____________________________________________________________________

What I need is this:

_____________________________________________________________________

Timer element needs to be created here

Case $Button 1

-check if button = last entry in $Whattodo

---if it is NOT:

GUICtrlSetData($Whattodo, "WRONG" &@crlf, 1)

GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)

Reset timer

---if it is:

GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)

Reset timer

-check if button was pressed within 1 second

---if it is NOT:

GUICtrlSetData($Whattodo, "WRONG" &@crlf, 1)

GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)

Reset timer

---if it is:

GUICtrlSetData($Whattodo, "WRONG" &@crlf, 1)

GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)

Reset timer

_____________________________________________________________________

Button check elements are italic and underlined

Timer check elements are bold

_____________________________________________________________________

( Sorry, I'm not a good programmer )

Edited by nf67

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I used a bit of your code Andreik and I've made progress :)

But I've encountered yet another problem.

I need two randoms to be equal. They should be random, but equal to eachother, like Random1=Random2.

Here's the code:

#include <GUIConstants.au3>
Global $LAST = 1
$Beepor = GUICreate("Beepor", 284, 439, 193, 115)
$Button1 = GUICtrlCreateButton("1", 15, 360, 80, 60, 0)
$Button2 = GUICtrlCreateButton("2", 104, 360, 80, 60, 0)
$Button3 = GUICtrlCreateButton("3", 192, 360, 80, 60, 0)
$Whattodo = GUICtrlCreateEdit("", 8, 16, 265, 337)
GUICtrlSetData(-1, "Press a button!"&@CRLF)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If $LAST = 1 Then 
            Soundplay ( "./Beep1.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1); ----------------------This random needs to be equal to
            $LAST = Random(1,3,1);-------------------------------------------------- this random
            else 
            GUICtrlSetData($Whattodo, "WRONG"&@crlf, 1)
            $LAST = Random(1,3,1)
            EndIf
        Case $Button2
            If $LAST = 2 Then
            Soundplay ( "./Beep2.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
            $LAST = Random(1,3,1)
            else 
            GUICtrlSetData($Whattodo, "WRONG"&@crlf, 1)
            $LAST = Random(1,3,1)
            EndIf
        Case $Button3
            If $LAST = 3 Then
            Soundplay ( "./Beep3.wav" )
            GUICtrlSetData($Whattodo, Random(1,3,1)&@crlf, 1)
            $LAST = Random(1,3,1)
            else 
            GUICtrlSetData($Whattodo, "WRONG"&@crlf, 1)
            $LAST = Random(1,3,1)
            EndIf
    EndSwitch
WEnd
Edited by nf67

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