two cards that open randomly

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

two cards that open randomly

Post by HJay » Thu Apr 12, 2012 6:39 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: two cards that open randomly

Post by sturgis » Thu Apr 12, 2012 10:53 pm

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: two cards that open randomly

Post by Mark » Fri Apr 13, 2012 12:02 am

Hi Hjay,

If you want to open a card 10 times, I suppose you want a repeat loop.

Code: Select all

repeat 10
  go any cd
end repeat
This works if your stack has only those 2 cards.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Fri Apr 13, 2012 7:57 am

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

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Fri Apr 13, 2012 8:42 am

hmmm maybe I need a start card with a start button?

HJay

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: two cards that open randomly

Post by sturgis » Fri Apr 13, 2012 2:50 pm

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.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: two cards that open randomly

Post by Mark » Mon Apr 16, 2012 10:34 am

Hi,

The script without end if statement is actually correct.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Mon Apr 16, 2012 11:30 am

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

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Mon Apr 16, 2012 11:32 am

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

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: two cards that open randomly

Post by Mark » Mon Apr 16, 2012 11:40 am

Hi Hjay,

Maybe you could explain slightly more extensively what you want to do?

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: two cards that open randomly

Post by sturgis » Mon Apr 16, 2012 12:37 pm

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

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Wed Apr 18, 2012 8:22 pm

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
Attachments
Matching Numbers.zip
(4.31 KiB) Downloaded 255 times

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: two cards that open randomly

Post by AtoZ » Wed Apr 18, 2012 8:52 pm

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
Attachments
Random Cards.zip
(675 Bytes) Downloaded 244 times
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

AtoZ
Posts: 64
Joined: Tue Mar 06, 2012 8:31 am

Re: two cards that open randomly

Post by AtoZ » Wed Apr 18, 2012 9:59 pm

My previous post was in reference to your earlier postings, not your most recent. Sorry if it was confusing.
Using LiveCode 5.5 on Windows 7 Home Premium Edition, Service Pack 1

Image
On my way from A TO Z, I often end up AT OZ.

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: two cards that open randomly

Post by HJay » Wed Apr 18, 2012 10:08 pm

Hi AtoZ

I am slowly getting my head round it. LiveCode still seems like magic to me at times :shock: 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

Post Reply