Jump to content

Randomizing order of Mouse Clicks


Recommended Posts

                 mouseclick("left",10,10,2,2)
                 mouseclick("left",20,20,2,2)
                 mouseclick("left",30,30,2,2)
                 mouseclick("left",40,40,2,2)
 
I am currently using a script that clicks these locations in this order. Is there any way to make it so it clicks those 4 locations in a random order? So on the first iteration, it will click 2, 4, 3, 1 and on the next iteration, it will click 3, 1, 4, 2, etc?
 
I was looking at arrays but am unsure of how to place a mouseclick in an array and then use an ArrayShuffle function to randomize the order.
Link to comment
Share on other sites

You put the values of the X and Y coordinates in the array, not the mouseclick command.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

so I have an array set up:

Global $array[4] = [10,20,30,40]

How do I use element 0 for the first mouseclick, element 1, for the second mouseclick, etc?

                 mouseclick("left",$array[0],10,2,2)
                 mouseclick("left",$array[1],10,2,2)
                 mouseclick("left",$array[2],10,2,2)
                 mouseclick("left",$array[3],10,2,2)
 
(only trying to get the x coordinates working. If I can figure out how to use the array, should be the same process for y, I'm guessing)
This is what I tried. 
 
Once I get the mouseclicks to call the array coordinates, I'm pretty sure I can just use the _ArrayShuffle function to create a new array with the order and just make the calls to that array instead.
Link to comment
Share on other sites

$X = Random(0, 3, 1)
MouseClick("left", $array[$X], 10, 2, 2)
; For a 2D array it would be done like this, assuming the coordinates are X = [0] and Y = [1]
MouseClick("left", $array[$X][0], $array[$X][1], 2, 2)
 

Or if you're looking to make the code shorter, but harder to read, you could do it this way.

 

MouseClick("left", $array[Random(0, 3, 1)], 10, 2, 2)
If you make the array a 2D array the second method wouldn't work, because you don't store the location in the array anywhere so you couldn't use it to get the Y coordinate.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

$X = Random(0, 3, 1)
MouseClick("left", $array[$X], 10, 2, 2)
; For a 2D array it would be done like this, assuming the coordinates are X = [0] and Y = [1]
MouseClick("left", $array[$X][0], $array[$X][1], 2, 2)
 

Or if you're looking to make the code shorter, but harder to read, you could do it this way.

 

MouseClick("left", $array[Random(0, 3, 1)], 10, 2, 2)
If you make the array a 2D array the second method wouldn't work, because you don't store the location in the array anywhere so you couldn't use it to get the Y coordinate.

 

 

I think I can ignore the y coordinates since what I'm using will have the same y coordinates for all 4 clicks so I just need to figure out how to click locations 1, 2, 3, 4 in a random order per set. 

JohnOne, That could possibly pick location 1 multiple times.

Basically, I have 4 boxes all next to each other. I want the script to be able to click the 4 boxes in a random order but not repeat any of them.

I currently just have it set to click box 1, box 2, box 3, box 4. I think I can use an array for the x coordinates and then shuffle the coordinates but I don't know how to have the x coordinates = an array element.

So in MouseClick() how do I have the x coordinate equal element 0 of an array, second click to equal element 1, third click to equal element 2, and fourth click to equal element 3.

I'm using to shuffle the function. I got the shuffle to work, I just don't know what the proper format is to call each element in the mouseclick.

Func _ArrayShuffle($a_array,$s_start=0,$e_end=-1)
 
        $t_array = $a_array
        $t_start = $s_start
 
        If ($e_end<>-1) Then
                $t_end = $e_end
        Else
                $t_end = UBound($t_array)
        EndIf
 
        For $ii = $t_start to $t_end
                _ArraySwap($t_array[Random($t_start,$t_end)],$t_array[Random($t_start,$t_end)])
        Next
 
        Return $t_array
 
EndFunc
Edited by DarksSaber
Link to comment
Share on other sites

DarksSaber,

This  

but not repeat any of them

 

adds a new wrinkle...give me a moment...

edit: Try this...

hotkeyset("{ESC}","_exit")
hotkeyset("{`}","_click")

local $yCoord = 300
local $aXCoords = [600,700,800,900]

while 1
    sleep(10)
wend

Func _click()
    local static $on = false, $lastclicked = random(0,ubound($aXCoords) - 1,1)
    local $idx
    $on = not $on
    consolewrite( ( ($on = true ) ? 'Running' : 'Paused' ) & @crlf)
    while $on
        while $idx = $lastclicked
            $idx = random(0,ubound($aXCoords) - 1,1)
        wend
        $lastclicked = $idx
        MouseClick("left",$aXCoords[$idx],$yCoord)
        ConsoleWrite('> Clicked at x = ' & $aXCoords[$idx] & ' y = ' & $yCoord  & @CRLF)
        sleep(1000)
    wend
endfunc

Func _exit()
    exit
EndFunc

kylomas

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

Global $array[4] = [10,20,30,40]

mouseclick("left",$array[0],10,2,2)
mouseclick("left",$array[1],10,2,2)
mouseclick("left",$array[2],10,2,2)
mouseclick("left",$array[3],10,2,2)
 
Let me try to simplify what I'm asking since I think the original way I said it is probably making it more complicated than it needs to be. 
I already have figured out how to use _ArrayShuffle to be able to randomize the order of array[4] and create a new new randomized array. Now I just need to figure out how to make the x coordinate for the mouseclick go to each element in the array.
 
With the above example, am I making the array calls to the global array correctly? I have a feeling that I'm just not using the syntax right for making array calls within the mouseclick. With the above example, I'd be trying to get the script to click (10, 10), (20,10), (30, 10), (40,10) in that order.
Link to comment
Share on other sites

The array syntax looks OK.  Is it not working?

edit:  The code I posted is an example showing syntax and an alternative way of getting random coordinates, not repeating, without having to recreate the array (possibly in a loop).  Since you didn't post any runnable code, I made up my own (from previous post).

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

Global $fPaused = False

HotKeySet("{ESC}", "Terminate")
HotKeySet("+1", "Slot1")

While 1
    Sleep(1)
WEnd

;slot 1
Func Slot1()
    $fPaused = Not $fPaused
    While $fPaused
         sleep(150)
                 mouseclick("left",542,266,2,2)
                 mouseclick("left",602,266,2,2)
                 mouseclick("left",660,266,2,2)
                 mouseclick("left",721,266,2,2)

        WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit
EndFunc

This is the original code. Shisft+1 starts and pauses it. It clicks in the 4 locations, then repeats. Esc stops it.

Global $fPaused = False

#include <Array.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("+1", "Slot1")

Func _ArrayShuffle($a_array,$s_start=0,$e_end=-1)

        $t_array = $a_array
        $t_start = $s_start

        If ($e_end<>-1) Then
                $t_end = $e_end
        Else
                $t_end = UBound($t_array)
        EndIf

        For $ii = $t_start to $t_end
                _ArraySwap($t_array[Random($t_start,$t_end)],$t_array[Random($t_start,$t_end)])
        Next

        Return $t_array

EndFunc

While 1
    Sleep(1)
WEnd

Global $array[4] = [542,602,660,721]

 
                 ;$new_array = _ArrayShuffle($array)
;slot 1
Func Slot1()
    $fPaused = Not $fPaused
    While $fPaused
         sleep(150)

         mouseclick("left",$array[0],266,2,2)
                 mouseclick("left",$array[1],266,2,2)
                 mouseclick("left",$array[2],266,2,2)
                 mouseclick("left",$array[3],266,2,2)

        WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit
EndFunc

The ArrayShuffle isn't used at the moment until I figure out how to make it click through the array spots. Once I figure that out, then I can have ArrayShuffle make a new array with the 4 x coordinates randomized. Looking at this, it should be using element 0 for the first x coordinate, element 1 for the second, etc. Right now I'm getting a variable used without being declared error. I'm pretty sure I made the array correctly but I guess not.

Link to comment
Share on other sites

If the shuffle routine is implemented like your previous post there is nothing to guarantee that you will not click the same button on successive calls to the suffle routine.  That is why I proposed a method like...

Global $fPaused = False

#include <Array.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("+1", "Slot1")

Global $array[4] = [542,602,660,721]

While 1
    Sleep(100)
WEnd

Func Slot1()
    local static $lastclicked = random(0,ubound($array) - 1)
    local $idx
    $fPaused = Not $fPaused
    While $fPaused
        while $lastclicked = $idx
            $idx = random(0,ubound($array) - 1,1)
        wend
         sleep(150)
         mouseclick("left",$array[$idx],266,2,2)
        $lastclick = $idx
        WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit
EndFunc

which does guarantee that the same button is not clicked twice in a row.

Good Luck,

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

If the shuffle routine is implemented like your previous post there is nothing to guarantee that you will not click the same button on successive calls to the suffle routine.  That is why I proposed a method like...

Global $fPaused = False

#include <Array.au3>

HotKeySet("{ESC}", "Terminate")
HotKeySet("+1", "Slot1")

Global $array[4] = [542,602,660,721]

While 1
    Sleep(100)
WEnd

Func Slot1()
    local static $lastclicked = random(0,ubound($array) - 1)
    local $idx
    $fPaused = Not $fPaused
    While $fPaused
        while $lastclicked = $idx
            $idx = random(0,ubound($array) - 1,1)
        wend
         sleep(150)
         mouseclick("left",$array[$idx],266,2,2)
        $lastclick = $idx
        WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit
EndFunc

which does guarantee that the same button is not clicked twice in a row.

Good Luck,

kylomas

 

I think its doing what I need it to do. It shuffles the array, goes through all 4 clicks, then shuffles it again. I'll doublecheck it again while I'm running it. Since I called it to shuffle the array once before it does all the clicks, it shouldn't reshuffle them again until the loop repeats, right?

Link to comment
Share on other sites

Exactly...the shuffle routine does not guarantee that whatever is at element 0 in the current shuffle was not at element 3 for the last shuffle (repeating click). 

edit: incidentally, there is an _arrayshuffle UDF

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

Exactly...the shuffle routine does not guarantee that whatever is at element 0 in the current shuffle was not at element 3 for the last shuffle (repeating click). 

edit: incidentally, there is an _arrayshuffle UDF

 

It's fine. I just need it to not repeat clicks in the same set. If it hits element 0 again on the next set, that's ok.

When I was looking for the arrayshuffle funtion, one of the threads said it was supposed to be included in the next update of AutoIt. That post was back in May or something and there's been updates since. Where can I find the udf?

Edited by DarksSaber
Link to comment
Share on other sites

JohnOne, That could possibly pick location 1 multiple times.

I acknowledge that it might not be what you want, but the possibility of clicking the same spot multiple times is the nature of random.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...