Page 1 of 5
two cards that open randomly
Posted: Thu Apr 12, 2012 6:39 pm
by HJay
Hi
I have two cards called “FR1” and “FR20” I want them to open 10 times each but in a random order. So far I have this on the Stack …
Code: Select all
on openStack
put random(2) into whichCard
if whichCard = 1 then
go to card "FR1"
else go to card "FR20"
end openStack
… but it just goes to “FR1” every time?
HJay
Re: two cards that open randomly
Posted: Thu Apr 12, 2012 10:53 pm
by sturgis
Might adjust your code like so
Code: Select all
put random(2) into whichCard
if whichcard = 1 then
go to card "FR1"
else
go to card "FR20"
end if
Betting the engine is confused by the lack of end if.
You can also do it as a 1 liner as follows
Code: Select all
go to card (any item of "FR1,FR20")
default item delimiter is comma so any item of "fr1,fr20" will pick one of the 2 items randomly.
Re: two cards that open randomly
Posted: Fri Apr 13, 2012 12:02 am
by Mark
Hi Hjay,
If you want to open a card 10 times, I suppose you want a repeat loop.
This works if your stack has only those 2 cards.
Kind regards,
Mark
Re: two cards that open randomly
Posted: Fri Apr 13, 2012 7:57 am
by HJay
Hi Sturgis and Mark
Thanks for the replys
It didn’t like the end if? Am wondering if I have the code in the right place? Should it be on the stack or should I create another card? I ‘mostly’ understand about messages being dealt with at the lowest point in the hierarchy i.e. buttons then passed up but where do you put the code if what you want to happen isn’t in response to an action by the user, what if you just want that to happen when the stack is opened?
Also I wasn’t clear enough in my random explanation, I need the FR1 card to be accessed exactly 10 times and the FR20 card to be accessed exactly 10 times but these 10 accesses of each need to be done in a random order. I can’t have nine FR1 and eleven FR20’s, which I think is what would happen if I had
Code: Select all
go to card (any item of "FR1,FR20")
Am I correct in thinking this? Also I will eventually have more thank just the two cards
Many thanks
HJay
Re: two cards that open randomly
Posted: Fri Apr 13, 2012 8:42 am
by HJay
hmmm maybe I need a start card with a start button?
HJay
Re: two cards that open randomly
Posted: Fri Apr 13, 2012 2:50 pm
by sturgis
The initial problem I was pointing out is that your script didn't have an end if statement, it looked like this
Code: Select all
if whichCard = 1 then
go to card "FR1"
else go to card "FR20"
so I changed it to this
Code: Select all
put random(2) into whichCard
if whichcard = 1 then
go to card "FR1"
else
go to card "FR20"
end if
While i'm not sure exactly where you're going with this, here is a script that will randomly go through every card the specified number of times in random order with a small delay so that its easier to see the cards change.
I placed this script in the stack. Would work equally well in the card script.
Code: Select all
local tStop --a script local variable that determines if the loop should stop or not
--tstop is used to exit the loop if needed
###pVar is a passed variable that contains the number of times to display each card
command gorandomcards pVar
####sets the initial value of tStop to false (puts true in it then sets it to not true the first time around)
if tStop is empty then put true into tStop
put not tStop into tStop
####I'm just using all cards for this. If you wished to do specific cards you would have to adjust the contents of tCardList
put the cardnames of this stack into tCardList
## if the list of cards is not empty
## and we haven't put true into tStop, the repeat loop will continue
repeat while tCardList is not empty and tStop is not true
### waiting with messages so that you can see
### the cards flip as well as allowing itme
### to do other things (like set tStop to true so that the loop will stop)
wait 100 milliseconds with messages
###get a random number based on the number of cardnames
### that are in the tCardList variable
put random(the number of lines in tCardList) into tLine
###go to the card named in the random
###Line number chosen from above
go card (line tLine of tCardList)
###tCardA is an array variable. We use it to keep track
### of how many times a specific card has been seen
### If card "fred" has just been selected
### we add 1 to tCardA["fred"] and then check to see if the value of tCardA["fred"]
### is greater than the value we passed to the command in pVar
### of course if line 4 of tCardList contains cardname "Fred" then
### the key value of 'tCardA[line tLine of tCardlist]' would be fred. tCardA["fred"]
add 1 to tCardA[line tLine of tCardList]
### if the value is > than pVar delete the card name from the list
if tCardA[line tLine of tCardList] > pVar then
delete line tLine of tCardlist
end if
###eventually, all card names are removed from the list
### so the repeat loop will exit.
end repeat
### ensure that our tStop variable is set to true so that we can start fresh
### next time
put true into tStop
end gorandomcards
To use this command just have a button that calls the command gorandomcards with a number defining the number of times to display each card
Code: Select all
on mouseUp
gorandomcards 5
end mouseup
For more info on arrays and how to use them go here.
http://lessons.runrev.com/s/lessons/tags?tag=array
There are other ways to do this, and its hard to know exactly what you're going for, but hopefully something in here will help.
Re: two cards that open randomly
Posted: Mon Apr 16, 2012 10:34 am
by Mark
Hi,
The script without end if statement is actually correct.
Kind regards,
Mark
Re: two cards that open randomly
Posted: Mon Apr 16, 2012 11:30 am
by HJay
Hi Sturgis
Thank you for your code and detailed explanations of it. I am working through it I hadn’t realised that what I wanted was going to be so complicated eek, will keep working through it. I am going to have to go and look at arrays again. Am sure there will be more questions once I have got my head around it
HJay
Re: two cards that open randomly
Posted: Mon Apr 16, 2012 11:32 am
by HJay
Hi Mark
Thanks you for confirming that, I think I read in the manual or dictionary that you only needed an ‘end if’ if you had more than one ‘else’.
Many thanks
HJay
Re: two cards that open randomly
Posted: Mon Apr 16, 2012 11:40 am
by Mark
Hi Hjay,
Maybe you could explain slightly more extensively what you want to do?
Kind regards,
Mark
Re: two cards that open randomly
Posted: Mon Apr 16, 2012 12:37 pm
by sturgis
Interesting. Yep, my mistake, so now i'm not sure why it wasn't working as per the first post. Thx for pointing this out, have looked at that dictionary entry lots and yet didn't see that else elsestatement with no end if was valid.
Mark wrote:Hi,
The script without end if statement is actually correct.
Kind regards,
Mark
Re: two cards that open randomly
Posted: Wed Apr 18, 2012 8:22 pm
by HJay
Hi Mark
Sorry for the delay in posting I wanted to finish some bits off so it made more sense.
I put a start card/button in which randomly directs the pupil to either 'card FR1' or 'card FR20' both of these cards then have another card to go to before going back to the start button. What I want to occur is that the 'FR1 card' followed by the 'yellow/red FR1 card’ is visited 10 times and the 'FR20 card' followed by the 'blue/green FR20 card' is also visited 10 times. But I want the twenty cards in total to be accessed in a random order.
Have attached what I have got so far
Does this make sense?
HJay
Re: two cards that open randomly
Posted: Wed Apr 18, 2012 8:52 pm
by AtoZ
As they say, there are many ways to do things in LiveCode.
Here is one way:
Code: Select all
local tx, tBucket, tChosen
on openStack
put empty into tBucket
repeat with tx = 1 to 10
put "FR1," after tBucket
put "FR20," after tBucket
end repeat
repeat with tx = 1 to 20
put random(number of items of tBucket) into tChosen
go to card (item tChosen of tBucket)
put item tChosen of tBucket & comma after msg -- TEMP
delete item tChosen of tBucket
wait 250 milliseconds -- OPTIONAL
end repeat
end openStack
You'll note there are the two commented lines in the code that are temporary or optional. They've been included to help show that the script is working correctly.
I've attached a small test stack that shows this script in action.
-- Clint
Re: two cards that open randomly
Posted: Wed Apr 18, 2012 9:59 pm
by AtoZ
My previous post was in reference to your earlier postings, not your most recent. Sorry if it was confusing.
Re: two cards that open randomly
Posted: Wed Apr 18, 2012 10:08 pm
by HJay
Hi AtoZ
I am slowly getting my head round it. LiveCode still seems like magic to me at times

I have put the code on the start button, it seems to be working ok am just testing it now. Will post what I have done in bit. Brill thank you so much.
HJay