Creating a randomizer that does not repeat picks

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
ShadowAquarius
Posts: 2
Joined: Sun Dec 03, 2017 3:55 pm

Creating a randomizer that does not repeat picks

Post by ShadowAquarius » Sun Dec 03, 2017 4:11 pm

Me and my brother were working on a card-based game on livecode, and we are stuck at the card-dealing system. We planned for our game to go like this:

1. The player has a set of 60 cards, receiving 5 random cards from the pile at the beginning.

2. Every round, the player will take one card from the pile.

How do we create the randomizer part for the game?

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

Re: Creating a randomizer that does not repeat picks

Post by dunbarx » Sun Dec 03, 2017 5:55 pm

So much fun.

The task you set for yourself seems deceptively simple. There are traps, and I wonder if you posted here because of them.

Anyway, make a button and two fields on a new card. Name one field "deck" and the other "hand". Name the button "deal". Make the height of fld "deck" 15 lines.

Put this in the script of button "deal":

Code: Select all

on mouseUp
   put "" into fld "hand"
   repeat with y = 1 to 15
      put y into line y of  fld "deck"
   end repeat
   
   repeat 5
      put any line of fld "deck" into temp
      put temp & return after fld "hand"
      delete line lineOffset(temp,fld "deck") of fld "deck"
   end repeat
   sort fld "hand" numeric
end mouseUp
Much of what I said above derives from the fact that it is just a little tricky to delete chunks from a container while keeping track of the contents and their place within that container. This may mean nothing to you; never mind.

Now step through the handler (remove the comment dashes in the "breakpoint" line)and watch the field contents. When you have all that, we can talk about the rest of your game.

Craig Newman

EDIT. By the way, how far along are you in learning LC? I assumed you were familiar with the basics.

bogs
Posts: 1599
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a randomizer that does not repeat picks

Post by bogs » Sun Dec 03, 2017 7:46 pm

Here is another example from the older dictionary. This actually puts names into lists and shows the sort functions. I did not test it, use at your own discretion.

Code: Select all

on mouseUp
  put empty into field "Names1"
  put empty into field "Names2"
# generate 15 names using the random function
  repeat for 15 times
    put item random(5) of "Mary,John,Gary,June,Tom"\
      && item random(5) of "Smith,Brown,White,May,Bell"\
      & return after field "Names1"
  end repeat
# sort the names, first by first name, then by last name
  sort lines of field "Names1" by word 1 of each
  sort lines of field "Names1" by word 2 of each
# find the duplicated names
   put 0 into nDup    # nDup contains the number of duplicate names
   repeat with i=2 to 15
     if line i of field "Names1" is line i-1 of field "Names1" then
       add 1 to nDup
       put i into item nDup of dupList   # save line numbers of duplicate
     end if
   end repeat
# highlight the duplicated names
  if nDup > 0
  then set the hilitedlines of field "Names1" to dupList

# find out if the user wants to continue with a different sorting technique
  answer "Found" && the number of items in dupList && "duplicates."\
       & return & "Continue to the next searching example ?"\
       with "Yes" or "No"
  if it is "No" then exit mouseUp  # quit if user does not want to continue 

# make a copy of names in field "Names1" and put it in field "Names2"
  put field "Names1" into field "Names2"
# put name list into a randomly sorted order. Use sort lines and the random funciotn
  sort lines of field "Names2" by random(number of lines in field "Names2")
  put 0 into nDup
  put empty into dupList
# initialize variables to improve performance
  put field "Names2" into Names2
  put number of lines in field "Names2" into nLines
  repeat with i = 1 to nLines
# skip i lines and look for a duplicate
    put lineOffset(line i of Names2, Names2, i) into whichLine
    if whichLine > 0 then
      add 1 to nDup
      put whichLine + i into item nDup of dupList
    end if
  end repeat
# highlight the duplicated names
  if nDup > 0 then set the hilitedlines of field "Names2" to dupList
end mouseUp
what it looks like -
Image
ImageImage

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2669
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Creating a randomizer that does not repeat picks

Post by mwieder » Wed Dec 06, 2017 9:08 pm

bogs-

I'm not sure at all that the older dictionary example is applicable here.
For instance, the first repeat loop could possibly result in 15 lines of "Mary Smith". Complications result from there on...

bogs
Posts: 1599
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a randomizer that does not repeat picks

Post by bogs » Thu Dec 07, 2017 3:49 am

I dunno, I think I would take 15 Granny Smith... Oh, you said "Mary Smith" :oops: Never mind!

Aside from the random in the example using names vs. numbers, as well as showing other options like sorting and so forth, is there something inherently wrong with it? It seems to demonstrate random well enough, and is pretty well commented.

I suppose we *could* discuss the relative merits of excessive commenting in a programming language this readable, but that is a discussion for another day :D

Back to the topic, the OP might also want to look at 'randomSeed' in the dictionary.

*Edit - stumbled across this previous discussion on the subject. BvG actually had a great take on it.

*Edit - Here is a shot of BvG's method, each text box getting moved to the next for comparison.
Image
ImageImage

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2669
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Creating a randomizer that does not repeat picks

Post by mwieder » Thu Dec 07, 2017 6:32 pm

Nice.
Your message contains 5 characters.
You need to enter at least 10 characters.

bogs
Posts: 1599
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a randomizer that does not repeat picks

Post by bogs » Fri Dec 08, 2017 2:56 am

Yah, I like looking at BvG's stuff, it always makes me feel so.... humble.

*Edit - After playing with BvG's thing for a while, I re-read the OP's question,
ShadowAquarius wrote:
Sun Dec 03, 2017 4:11 pm
1. The player has a set of 60 cards, receiving 5 random cards from the pile at the beginning.
2. Every round, the player will take one card from the pile.
And came across any in the dictionary, as in any line in a field. Actually, it really means any random member of a set. Lines in a field, numbers from an array, objects on a card, etc.

Filling a field with 1 to 60, then repeating for 5 times choosing any line from the field gave pretty good random results. For the individual card draws, it works again (as long as you are decreasing the list by whatever is chosen).

In the case of a card game, though, you would have to check for duplicates of the numbers chosen.
ImageImage

MaxV
Posts: 1467
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Creating a randomizer that does not repeat picks

Post by MaxV » Tue Dec 12, 2017 1:42 am

This is my solution:
########CODE to copy and paste#######

on createDeck
put "A,B,C,D" into cardSuits
repeat for each item tItem in cardSuits
repeat with i=1 to 15
put i & tItem & comma after temp #cards are like: 1A,2A, ..., 1B, ...
end repeat
end repeat
delete the last char of temp
set the deck of this stack to temp
end createDeck

on shuffleDeck
put the deck of this stack into temp
put the number of items of temp into temp2
sort items of temp by random(temp2)
set the deck of this stack to temp
end shuffleDeck

function PickCard nCards
put the deck of this stack into temp
put item 1 to nCards of temp into picked
delete item 1 to nCards of temp
set the deck of this stack to temp
return picked
end PickCard
#####END OF CODE generated by http://tinyurl.com/j8xf3xq with livecode 9.0.0-dp-6#####
Last edited by MaxV on Fri Dec 15, 2017 8:47 am, edited 2 times in total.
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

ShadowAquarius
Posts: 2
Joined: Sun Dec 03, 2017 3:55 pm

Re: Creating a randomizer that does not repeat picks

Post by ShadowAquarius » Fri Dec 15, 2017 5:12 am

Does anyone know about 'Pokemon Trading Card Game Online'? We were trying to accomplish a system like the game.

P.S.: I don't think i understand any of the codes... i might have to do much research

bogs
Posts: 1599
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a randomizer that does not repeat picks

Post by bogs » Fri Dec 15, 2017 6:11 am

ShadowAquarius wrote:
Fri Dec 15, 2017 5:12 am
I don't think i understand any of the codes... i might have to do much research
You mean the code above of the examples posted?
ImageImage

MaxV
Posts: 1467
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Creating a randomizer that does not repeat picks

Post by MaxV » Fri Dec 15, 2017 8:42 am

ShadowAquarius wrote:
Fri Dec 15, 2017 5:12 am
Does anyone know about 'Pokemon Trading Card Game Online'? We were trying to accomplish a system like the game.

P.S.: I don't think i understand any of the codes... i might have to do much research
I'll explain my code:

createDeck: it creates a text list representing all the cards you asked. I imagined 4 types from 1 to 15, so the list is: 1A, 2A, 3A, ..., 14A, 15A, 1B, 2B, ..., 14B, 15B, 1C, ... , 15C, 1D, ... , 15D.
Then the list is saved as a custom property of the stack, so you can retrieve it in any part of your program.
You can customize it as you need.

shuffleDeck: mix the card deck. It mix with the current number of cards, so it works both with 60 cards or just 3.

PickCard: it returns you the first nCards of the current deck, and remove them from the current deck.

Your work is just to associate the number of the card list to card images of the program.
Livecode Wiki: http://livecode.wikia.com
My blog: http://livecodeitalia.blogspot.it
To post code use this: http://tinyurl.com/ogp6d5w

bogs
Posts: 1599
Joined: Sat Feb 25, 2017 10:45 pm

Re: Creating a randomizer that does not repeat picks

Post by bogs » Fri Dec 15, 2017 4:16 pm

In the example of BvG's that I linked to, which could be obscure for the unitiated, I'll also try to explain what is going on as it relates to the game you are trying to reproduce (as far as your initial description).
ShadowAquarius wrote:
Sun Dec 03, 2017 4:11 pm
1. The player has a set of 60 cards, receiving 5 random cards from the pile at the beginning.
2. Every round, the player will take one card from the pile.
How do we create the randomizer part for the game?
As it would apply to these specific questions -
.. "The player has a set of 60 cards" - you would put all 60 cards into a field, 1 per line.
.. "5 random cards from the pile at the beginning" - you sort that field by random(60), then take 5 lines from that field and that is your beginning deal.
.. "Every round, the player will take one card from the pile" - each deal, you select one more line from the original list, deal it to the player, delete it from the original list.

The code would look like this for the deal -

Code: Select all

on mouseUp
   put the number of lines of field "Field" into tNum
   sort field "Field" by random(tNum)
   put field "Result" into field "LastResult"
   put line 1 to 5 of field "Field" into field "Result"
   delete line 1 to 5 of field "Field"
end mouseUp
For the single cards, you would change [delete line 1 to 5 of field "Field"] to delete only the single line your dealing.

On a new game, of course, you would put your original list of cards back into the first field. After 3 'deals', this is what I got for a result -
Image
ImageImage

PaulDaMacMan
Posts: 162
Joined: Wed Apr 24, 2013 4:53 pm

Re: Creating a randomizer that does not repeat picks

Post by PaulDaMacMan » Thu Jan 04, 2018 10:29 pm

The examples already posted here of creating a virtual card deck(s) list, random sorting the list, and then removing items from the list so they aren't dealt again, are all very similar to the scripts in my BlackJack Stack I've been meaning to share on LiveCode Share. I was going to try to redo the card image bits with vector path data now that LC9dp11 has color SVG support. I actually started this stack in HC around 1989 using playing card "font" for rendering and it's still not finished LOL. Still need to add a betting system, splits, etc.
Screen Shot 2018-01-04 at 4.25.48 PM.png

Post Reply

Return to “Talking LiveCode”