give roles randomly

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

give roles randomly

Post by kelyanok » Tue Apr 14, 2020 12:58 pm

i have a game where theres 9 players and i need to give 9 roles randomly to all of them. i know that there is already a thread about that but i cant convert it to what i really want.
i could make a whole 1000 lines code that checks every time if a player has a role check what is it but i am sure that theres a better way than my idea just dont know what it is, so if you do know pls help thanks

udate: i found a code on another forum, which is

Code: Select all

put "1,2,3,4,5" into theList
sort items of theList by random(10000)
repeat with N = 1 to 5
   set label of button ("button" & N) to item N of theList
end repeat
it is working, but i just need to use fields instead of buttons; i changed it to this:

Code: Select all

on giveRole
   put "1,2,3,4,5,6,7,8,9" into theList
   sort items of theList by random(10000)
   repeat with N = 1 to 9
      put item N of theList into fld ("playerRole" & N)
   end repeat
end giveRole
put "theres no such object". ive put playerRole" space and all numbers from 1 to 9.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: give roles randomly

Post by jmburnod » Tue Apr 14, 2020 1:40 pm

Hi,
You may check if there is a fld like this

Code: Select all

on giveRole
   put "1,2,3,4,5,6,7,8,9" into theList
   sort items of theList by random(10000)
   repeat with N = 1 to 9
      if there is not a fld ("playerRole" & N) then --fld exists or not
         answer "playerRole" & N
         exit repeat
      end if
      put item N of theList into fld ("playerRole" & N)
   end repeat
end giveRole
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: give roles randomly

Post by dunbarx » Tue Apr 14, 2020 4:03 pm

Hi the large sort value 1000 will still not guarantee no duplicates. You need to log the selections, and then check to ensure that each new one does not already exist.

An old fashioned way is:

Code: Select all

on mouseUp
   repeat with y = 1 to 10
      put y into line y of temp
   end repeat
   
   repeat 5
   get any line of temp
   put it & return after accum
   delete line lineOffset(it,temp) of temp
   end repeat
end mouseUp
You get five unique values. This is verbose, but makes sure that you never "accidentally" select a duplicate.

Craig

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: give roles randomly

Post by SparkOut » Tue Apr 14, 2020 5:37 pm

Or, sort the original or copy of the original list randomly, then assign line 1 to field 1, line 2 to field 2 etc

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: give roles randomly

Post by dunbarx » Tue Apr 14, 2020 6:35 pm

Sparkout.

Sure, many ways to skin a cat in LC. You just then peel off the lines one by one, never looking back.

Craig

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: give roles randomly

Post by SparkOut » Tue Apr 14, 2020 7:58 pm

dunbarx wrote:
Tue Apr 14, 2020 6:35 pm
many ways to skin a cat in LC.
Hey, that's my line.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: give roles randomly

Post by dunbarx » Tue Apr 14, 2020 8:37 pm

Oh, do you mean "Who's line is it, anyway?"

No?

The earliest reference on the forum i could find was Klaus back in 2007. He did say "skin a rev-cat", however, but this soon became modernized, Like HC -> LC.

Craig

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: give roles randomly

Post by Xero » Tue Apr 28, 2020 7:37 am

kelyanok wrote:
Tue Apr 14, 2020 12:58 pm

Code: Select all

on giveRole
   put "1,2,3,4,5,6,7,8,9" into theList
   sort items of theList by random(10000)
   repeat with N = 1 to 9
      put item N of theList into fld ("playerRole" & N)
   end repeat
end giveRole
put "theres no such object". ive put playerRole" space and all numbers from 1 to 9.
Is the problem that it's not getting to the field?
Is your field labelled: playerRole 1, playerRole 2, etc? (i.e. has space)
Or, is it playerRole1, playerRole2, etc.? (i.e. has no space)
("playerRole" & N) will come out as playerRoleN (where N is your number) - note no space
("playerRole" & space & N) will come out as playerRole N (where N is your number)- note the space.

Also, if you're worried about players getting multiple roles (i.e.Player 1 and 2 getting role 5), you may be able to run a loop that checks the latest selection against a list of previous selections. TBH, I am not sure about the sort command by random 10000. The dictionary doesn't have a great explanation of it. If I were doing it, I would have 1-9 in a list, select a random number from 1 to 9, assign that number to player 1, then delete that item of the list. Repeat with random (8) to player 2, delete that item number,repeat with random (7) to player 3,etc. until you have one player left and 1 item in the list. It's the same theory that you would use if you were dealing a deck of cards in random order- select it at random, take it out of the potential choices.

Hope that helps.
X

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: give roles randomly

Post by dunbarx » Tue Apr 28, 2020 2:28 pm

Xero.
I would have 1-9 in a list, select a random number from 1 to 9, assign that number to player 1, then delete that item of the list.
See my handler offering, the third post in the thread. I do just that, not relying on a large sortKey. I have done it that way for decades.

Craig

Xero
Posts: 152
Joined: Sat Jun 23, 2018 2:22 pm

Re: give roles randomly

Post by Xero » Wed Apr 29, 2020 2:14 am

Xero.
I would have 1-9 in a list, select a random number from 1 to 9, assign that number to player 1, then delete that item of the list.
See my handler offering, the third post in the thread. I do just that, not relying on a large sortKey. I have done it that way for decades.

Craig
Clearly reading was not one of my strong points! Yes, that will do it, and probably less verbose than what I would have done. Thanks!
X

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”