sorting cards randomly in a stack

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

Post Reply
micamd
Posts: 5
Joined: Wed May 25, 2016 8:27 pm

sorting cards randomly in a stack

Post by micamd » Wed May 25, 2016 8:56 pm

Hey guys

Firstly, thanks for an excellent development language - for someone with no prior coding experience, LiveCode has been quite forgiving!

I am trying to develop an "image ratings" app which has the following sequence of cards

Card 1 (Instructions) --> Cards 2-20 (Images rated with a slider) --> Card 21 (Conclusion)

There are two points here that I would greatly appreciate any help that you can provide

1. In Card 1, there will be a button that goes to next card. When pressed, I would like Cards 2-20 to be shuffled so that the sequence which the rater will have to undergo will be randomized (e.g., Card 2, 7, 4, 9.... or Card 5, 19, 4....). This will continue until all 19 cards have been responded to, and then the stack will progress to Card 21 (conclusion)

2. For each Card in Cards 2-20, providing the rating puts a value into a container once a button has been pressed ('tValues'). The code for the button looks like this...

Code: Select all

global tValues
on mouseUp
   put "S" & the number of this card & tab & fld "ValenceValue"  & tab & fld "ArousalValue"\
   & tab & short date after fld "tValueField"
   put return & fld "tValueField" after tValues
   put empty into fld "tValueField"
    end mouseUp
At the last card (21) of the stack, the container tValues is displayed in a hidden field (fld tValues). I would like a button which would output the values in this field in a text file to a specific directory.

I've been unable to find both these topics addressed within the LiveCode forums. I hope you will be able to help.

Thanks in advance!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: sorting cards randomly in a stack

Post by dunbarx » Wed May 25, 2016 9:36 pm

Hi.

Think of this:

Code: Select all

sort the cards of stack "yourStack" by random(1000)
But this mixes up the all-important first and last cards, eh? So we have to distinguish them somehow, and I would do that by naming them. Maybe "firstCard" and "lastCard"?

And now you must know that one may set the number property of a card, so that it can be placed precisely among its neighbors. Is this enough to get you started? Read the dictionary for both the sort command and the number property.

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: sorting cards randomly in a stack

Post by jacque » Thu May 26, 2016 3:46 pm

Another way to do the sort is to sort only the marked cards, which will ignore the cards you don't want to move.

In the property inspector for each card, set its mark property. Then you can do :

Code: Select all

sort marked cards of this stack by random(1000)
BTW, the random number is arbitrary. There are lots of ways to sort randomly.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: sorting cards randomly in a stack

Post by dunbarx » Thu May 26, 2016 5:51 pm

Jacques method is clever, in that if you mark all the "middle" cards the sort will not affect the position of the extreme two, that Is, the first and last.

Craig

micamd
Posts: 5
Joined: Wed May 25, 2016 8:27 pm

Re: sorting cards randomly in a stack

Post by micamd » Thu May 26, 2016 7:34 pm

Thanks guys! Got the issue sorted. I've pasted the final handler

So the stack is now Card 1 (unmarked) -- Cards 2 - 17 (marked) -- card 18 (unmarked)

i've placed the following handler on a button in card 1,

Code: Select all

on mouseUp
   sort marked cards of this stack by random(15)
   set the number of card "lastCard" to 18
   wait 30 ticks
   go to next card
end mouseUp 

cubist
Posts: 5
Joined: Tue May 17, 2016 1:42 pm

Re: sorting cards randomly in a stack

Post by cubist » Fri May 27, 2016 7:04 am

Hmmm. Does that stack have a stable number of cards, or can there be more or less cards at whichever time? And is there any chance of cards becoming marked or unmarked for whatever arbitrary reason? If you think that idiotproofing might be a prudent thing to do:

Code: Select all

on ScrambleEm
  put the number of cards in this stack into Fred
  unmark card 1 of this stack
  unmark card Fred of this stack
  repeat with K1 = 2 to (Fred - 1)
    mark card Fred of this stack
  end repeat
  sort marked cards of this stack by random (2 * Fred)
end ScrambleEm

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: sorting cards randomly in a stack

Post by dunbarx » Fri May 27, 2016 4:06 pm

The point about a change in the number of cds is well taken. That is why my first instinct, to explicitly define the two cards of interest, by naming them, cuts through that issue. Of course this in and of itself assumes two and only two "special" cards, and that their special places in the order are fixed.

So separating cards by marking them is adorable, but may not be as robust as the old-fashioned way.

Craig.

micamd
Posts: 5
Joined: Wed May 25, 2016 8:27 pm

Re: sorting cards randomly in a stack

Post by micamd » Fri May 27, 2016 7:15 pm

There will be a fixed number of cards in the stack so the adorable way is fine for now. But future versions will require updating card quantities, so this would be more useful. Thanks!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: sorting cards randomly in a stack

Post by dunbarx » Fri May 27, 2016 11:03 pm

Fair enough.

Note that when adding new cards, one can always mark them. But this still is not as robust as some form of explicit reference that is immutable, and wholly apart from the suite of shuffle-able cards.

Craig.

Post Reply