Page 1 of 1

sorting cards randomly in a stack

Posted: Wed May 25, 2016 8:56 pm
by micamd
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!

Re: sorting cards randomly in a stack

Posted: Wed May 25, 2016 9:36 pm
by dunbarx
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

Re: sorting cards randomly in a stack

Posted: Thu May 26, 2016 3:46 pm
by jacque
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.

Re: sorting cards randomly in a stack

Posted: Thu May 26, 2016 5:51 pm
by dunbarx
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

Re: sorting cards randomly in a stack

Posted: Thu May 26, 2016 7:34 pm
by micamd
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 

Re: sorting cards randomly in a stack

Posted: Fri May 27, 2016 7:04 am
by cubist
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

Re: sorting cards randomly in a stack

Posted: Fri May 27, 2016 4:06 pm
by dunbarx
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.

Re: sorting cards randomly in a stack

Posted: Fri May 27, 2016 7:15 pm
by micamd
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!

Re: sorting cards randomly in a stack

Posted: Fri May 27, 2016 11:03 pm
by dunbarx
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.