Page 1 of 1

give roles randomly

Posted: Tue Apr 14, 2020 12:58 pm
by kelyanok
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.

Re: give roles randomly

Posted: Tue Apr 14, 2020 1:40 pm
by jmburnod
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

Re: give roles randomly

Posted: Tue Apr 14, 2020 4:03 pm
by dunbarx
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

Re: give roles randomly

Posted: Tue Apr 14, 2020 5:37 pm
by SparkOut
Or, sort the original or copy of the original list randomly, then assign line 1 to field 1, line 2 to field 2 etc

Re: give roles randomly

Posted: Tue Apr 14, 2020 6:35 pm
by dunbarx
Sparkout.

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

Craig

Re: give roles randomly

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

Re: give roles randomly

Posted: Tue Apr 14, 2020 8:37 pm
by dunbarx
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

Re: give roles randomly

Posted: Tue Apr 28, 2020 7:37 am
by Xero
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

Re: give roles randomly

Posted: Tue Apr 28, 2020 2:28 pm
by dunbarx
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

Re: give roles randomly

Posted: Wed Apr 29, 2020 2:14 am
by Xero
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