Page 1 of 1

shuffle an Array

Posted: Thu Jun 11, 2015 11:39 am
by oneup
Hi,

I have an array filled with the numbers 1-10
Now i want to shuffle those numbers every time i press a specific button.

i'm a beginner and i need a very clear explanation.

Thats my code so far:

Code: Select all

on mouseUp
   put 0 into field "Score"
   enable button count
   
   repeat with x= 1 to 10
      put x into tArray[x]
   end repeat
   
   put 1 into gVar
   put tArray[gVar] into field "A"

Re: shuffle an Array

Posted: Thu Jun 11, 2015 12:58 pm
by Klaus
Hi oneup,

1, welcome to the forum! :D

2. In your example a simple:
...
put random(10) into fld "A"
...
will do. 8)

But here is how to "shuffle" your array, or better the KEYS of your array:
...
put the keys of YourArray into tKeys
put ANY line of tKeys into tRandomKey
put YourArray[tRandomKey] into fld "A"
...
Lookup KEYs and ANY and RANDOM in the dictionary!

Here some great learning resources:
http://www.hyperactivesw.com/revscriptc ... ences.html


Best

Klaus

Re: shuffle an Array

Posted: Thu Jun 11, 2015 1:12 pm
by oneup
Hey Klaus,

first of, thanks for the nice and friendly welcome.

I should've mentioned that i want to use every KEY of the Array once and only once.

e.g.:

first time i Press - 3
second time - 10
third time - 1
fourth - 8
...
and so on

No number shall be used twice. Would it still work with the example u gave me?

Re: shuffle an Array

Posted: Thu Jun 11, 2015 1:17 pm
by Klaus
Hi oneup,

ah, I see, in that case I would simply DELETE the last "used" key like this:
...
put the keys of YourArray into tKeys
put ANY line of tKeys into tRandomKey
put YourArray[tRandomKey] into fld "A"
DELETE variable YourArray[tRandomKey]
...
Of course now you would first need to check it YourArray is EMPTY before proceeding, know what I mean?


Best

Klaus

Re: shuffle an Array

Posted: Thu Jun 11, 2015 2:04 pm
by dunbarx
Hi.

What Klaus said.

But since you are a new LC user, I want to make sure you know there are usually many ways to do things. Please make the following experiment: On a new card, make a field and a button. Put the numbers 1-10 into the field. Put this in the button script:

Code: Select all

on mouseUp
   sort fld 1 by random(the number of lines of fld 1)
   answer line 1 of fld 1
   delete line 1 of fld 1
end mouseUp
Press the button now and then. Now in fact, this approach is silly. But it works, and is cute in its way. Oh yes, did I mention that there are many ways to do things in LC?

Craig Newman

Re: shuffle an Array

Posted: Sat Jun 13, 2015 12:38 am
by Da_Elf
if i have a group of buttons in a group A-H and the group is called "myButtons" and another button outside the group and i put into each button of grp "myButtons"

Code: Select all

on mouseUp
   answer the short name of me
   set the backgroundColor of me to red
end mouseUp
i currently have in the outside button this

Code: Select all

on mouseUp
   send "mouseUp" to any button of grp "myButtons"
end mouseUp
but i want it to be something more like

Code: Select all

on mouseUp
   send "mouseUp" to any button of grp "myButtons" where backgroundColor of it <> red
end mouseUp
would there be a correct way to word this? naturally this doesnt work

if i use something like this i would get caught in an endless loop when all are red

Code: Select all

on mouseUp
   put the short name of any button of grp "myButtons" into theName
   if the backgroundColor of btn theName of grp "myButtons" <> red then
      send "mouseUp" to btn theName of grp "myButtons" in 0
   else
      send "mouseUp" to me in 0
   end if
end mouseUp

Re: shuffle an Array

Posted: Sat Jun 13, 2015 9:29 am
by bn
Hi da_elf,

you could do something like this

Code: Select all

on mouseUp
   repeat with i = 1 to the number of  buttons of group "myGroup"
      if the backgroundColor of button i of group "myGroup" <> "red" then put the short name of btn i of group "myGroup" & cr after tCollect
   end repeat
   if tCollect = "" then
      answer "All buttons have been clicked"
      exit mouseUp
   end if
   put any line of tCollect into tButtonToCall
   send "mouseUp" to button tButtonToCall of group "myGroup"
end mouseUp
kind regards
Bernd