Jump to content

Logging results given by Random() function


Hylia
 Share

Go to solution Solved by kylomas,

Recommended Posts

Hi, what I want to do with AutoIT is the following: I want to roll a dice then I write the result into an array and then I want the result to be shown in the big text box; for one result this is easy to do, however, I want the next result to appear above or underneath it so I can see both results after each other and I want to keep doing this for all the rolls that are next.

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>



$Form1 = GUICreate("Form1", 353, 438, 192, 124)

$TextWindow = GUICtrlCreateEdit("", 8, 8, 225, 417)

GUICtrlSetData(-1, "TextWindow")

$bDice = GUICtrlCreateButton("Roll the Dice!", 256, 384, 81, 33)

$lResult1 = GUICtrlCreateLabel("Result1", 248, 16, 40, 17)

$lResult2 = GUICtrlCreateLabel("Result2", 248, 64, 40, 17)

$lResult3 = GUICtrlCreateLabel("Result3", 248, 112, 40, 17)

$Result1 = GUICtrlCreateInput("", 304, 16, 33, 21)

$Result2 = GUICtrlCreateInput("", 304, 64, 33, 21)

$Result3 = GUICtrlCreateInput("", 304, 112, 33, 21)

GUISetState(@SW_SHOW)



Global $D

Global $R[10]

Global $i = 0



While 1

    $nMsg = GUIGetMsg()

    Switch $nMsg

        Case $GUI_EVENT_CLOSE

            Exit

         Case $bDice

            Roll()

    EndSwitch

WEnd



Func Roll()



$D = Random (1, 6, 1)

$R[$i] = $D



GUICtrlSetData($TextWindow, $R[$i] & $R[$i+1]) ;This doesn't do anything

;GUICtrlSetData($TextWindow, $R[$i] & $R[$i+1] &) nor this

;GUICtrlSetData($TextWindow, $R[$i], $R[$i+1]) or this



GUICtrlSetData($Result1, $R[0])

GUICtrlSetData($Result2, $R[1])

GUICtrlSetData($Result3, $R[2])





$i = $i + 1



EndFunc

So, basicly what I've managed to achieve is I can only display one number at a time in the comment box, or I have to manually add all the boxes I want to display the previous results, I would think there's an easier way for this, can anyone tell me what I've missed?

Link to comment
Share on other sites

I don't sure. did you mean this:

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>



$Form1 = GUICreate("Form1", 353, 438, 192, 124)

$TextWindow = GUICtrlCreateEdit("", 8, 8, 225, 417)

;~ GUICtrlSetData(-1, "TextWindow")

$bDice = GUICtrlCreateButton("Roll the Dice!", 256, 384, 81, 33)

$lResult1 = GUICtrlCreateLabel("Result1", 248, 16, 40, 17)

$lResult2 = GUICtrlCreateLabel("Result2", 248, 64, 40, 17)

$lResult3 = GUICtrlCreateLabel("Result3", 248, 112, 40, 17)

$Result1 = GUICtrlCreateInput("", 304, 16, 33, 21)

$Result2 = GUICtrlCreateInput("", 304, 64, 33, 21)

$Result3 = GUICtrlCreateInput("", 304, 112, 33, 21)

GUISetState(@SW_SHOW)



Global $D

Global $R[10]

Global $i = 0



While 1

    $nMsg = GUIGetMsg()

    Switch $nMsg

        Case $GUI_EVENT_CLOSE

            Exit

         Case $bDice

            Roll()

    EndSwitch

WEnd



Func Roll()



$D = Random (1, 6, 1)

$R[$i] = $D



GUICtrlSetData( $TextWindow , GUICtrlRead($TextWindow) & $R[$i] & $R[$i+1] & @CRLF) ;This doesn't do anything

;GUICtrlSetData($TextWindow, $R[$i] & $R[$i+1] &) nor this

;GUICtrlSetData($TextWindow, $R[$i], $R[$i+1]) or this



GUICtrlSetData($Result1, $R[0])

GUICtrlSetData($Result2, $R[1])

GUICtrlSetData($Result3, $R[2])





$i = $i + 1



EndFunc

Saludos

Link to comment
Share on other sites

  • Solution

Hylia,

This may give you some ideas...

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

local $gui      =   guicreate('Roll The Dice',200,200)
local $display  =   guictrlcreateedit('',10,10,100,160, BitOR($WS_VSCROLL, $ES_READONLY))

; create a group to hold the frequency count controls
local $g1       =   guictrlcreategroup('Frequency',115,05,80,160)

; create an array to hold the frequency count control id's
local $aRolls[6]

; create static controls for frequency counts
for $1 = 1 to 6
    guictrlcreatelabel('#' & $1 & ' - ',130,$1*15+15,20,15)
    $aRolls[$1-1] = guictrlcreatelabel('',145,$1*15+15,20,15,bitor($SS_SUNKEN,$SS_CENTER))
next

local $roller   =   GUICtrlCreateButton("Roll the Dice!" & @CRLF, 10,180,180,20)

; do roll routine if the enter key is pressed
local $Enter    =   GUICtrlCreateDummy()

                    GUISetState(@SW_SHOW)

; allows Enter key to actuate a control ($Enter)
Dim $aAccelKeys[1][2] = [["{ENTER}", $Enter]]
GUISetAccelerators($aAccelKeys)

While 1

    switch guigetmsg()
        Case $GUI_EVENT_CLOSE
            Exit
        ; do roll routine if either Roll button is clicked or the Enter key is pressed
        case $roller, $Enter
            _Roll()
    EndSwitch

WEnd

func _Roll()

    static $roll_number = 1         ; will persist between calls
    local $tRoll = random(1,6,1)    ; will change between calls

    ; display the current roll
    guictrlsetdata($display,'Roll # ' & stringformat('%03i',$roll_number) & ' = ' & $tRoll & @CRLF,1)
    $roll_number += 1

    ; update the frequency count by adding 1 to the matching control
    guictrlsetdata($aRolls[$tRoll-1], guictrlread($aRolls[$tRoll-1]) + 1)

endfunc

kylomas

edit: added ENTER key as accelerator

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hylia,

This may give you some ideas...

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

local $gui      =   guicreate('Roll The Dice',200,200)
local $display  =   guictrlcreateedit('',10,10,100,160, BitOR($WS_VSCROLL, $ES_READONLY))

; create a group to hold the frequency count controls
local $g1       =   guictrlcreategroup('Frequency',115,05,80,160)

; create an array to hold the frequency count control id's
local $aRolls[6]

; create static controls for frequency counts
for $1 = 1 to 6
    guictrlcreatelabel('#' & $1 & ' - ',130,$1*15+15,20,15)
    $aRolls[$1-1] = guictrlcreatelabel('',145,$1*15+15,20,15,bitor($SS_SUNKEN,$SS_CENTER))
next

local $roller   =   GUICtrlCreateButton("Roll the Dice!" & @CRLF, 10,180,180,20)

; do roll routine if the enter key is pressed
local $Enter    =   GUICtrlCreateDummy()

                    GUISetState(@SW_SHOW)

; allows Enter key to actuate a control ($Enter)
Dim $aAccelKeys[1][2] = [["{ENTER}", $Enter]]
GUISetAccelerators($aAccelKeys)

While 1

    switch guigetmsg()
        Case $GUI_EVENT_CLOSE
            Exit
        ; do roll routine if either Roll button is clicked or the Enter key is pressed
        case $roller, $Enter
            _Roll()
    EndSwitch

WEnd

func _Roll()

    static $roll_number = 1         ; will persist between calls
    local $tRoll = random(1,6,1)    ; will change between calls

    ; display the current roll
    guictrlsetdata($display,'Roll # ' & stringformat('%03i',$roll_number) & ' = ' & $tRoll & @CRLF,1)
    $roll_number += 1

    ; update the frequency count by adding 1 to the matching control
    guictrlsetdata($aRolls[$tRoll-1], guictrlread($aRolls[$tRoll-1]) + 1)

endfunc

kylomas

edit: added ENTER key as accelerator

Absolutely perfect, exactly what I wanted, thanks!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...