Creating a randomizer that does not repeat picks
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 2
- Joined: Sun Dec 03, 2017 3:55 pm
Creating a randomizer that does not repeat picks
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?
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?
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Creating a randomizer that does not repeat picks
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":
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.
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
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.
Re: Creating a randomizer that does not repeat picks
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.
what it looks like -
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
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Creating a randomizer that does not repeat picks
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...
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...
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Creating a randomizer that does not repeat picks
I dunno, I think I would take 15 Granny Smith... Oh, you said "Mary Smith" 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
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.
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
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.
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: Creating a randomizer that does not repeat picks
Nice.
Your message contains 5 characters.
You need to enter at least 10 characters.
Your message contains 5 characters.
You need to enter at least 10 characters.
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: Creating a randomizer that does not repeat picks
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,
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.
*Edit - After playing with BvG's thing for a while, I re-read the OP's question,
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.ShadowAquarius wrote: ↑Sun Dec 03, 2017 4:11 pm1. 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.
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.
Re: Creating a randomizer that does not repeat picks
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#####
########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: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
-
- Posts: 2
- Joined: Sun Dec 03, 2017 3:55 pm
Re: Creating a randomizer that does not repeat picks
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
P.S.: I don't think i understand any of the codes... i might have to do much research
Re: Creating a randomizer that does not repeat picks
You mean the code above of the examples posted?ShadowAquarius wrote: ↑Fri Dec 15, 2017 5:12 amI don't think i understand any of the codes... i might have to do much research
Re: Creating a randomizer that does not repeat picks
I'll explain my code:ShadowAquarius wrote: ↑Fri Dec 15, 2017 5:12 amDoes 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
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: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
Re: Creating a randomizer that does not repeat picks
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).
.. "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 -
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 -
As it would apply to these specific questions -ShadowAquarius wrote: ↑Sun Dec 03, 2017 4:11 pm1. 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?
.. "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
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 -
-
- Posts: 627
- Joined: Wed Apr 24, 2013 4:53 pm
- Contact:
Re: Creating a randomizer that does not repeat picks
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.
-
- Posts: 1
- Joined: Sat Feb 13, 2021 5:56 pm
Re: Creating a randomizer that does not repeat picks
Hi I'm not only new to liveCode I'm also new to this website so I replied to this post.
I'm making a flags game and I'm trying to make the fags randomized and not repeated so I'm trying to make a randomizer that doesn't repeat itself how do I do that?
I'm making a flags game and I'm trying to make the fags randomized and not repeated so I'm trying to make a randomizer that doesn't repeat itself how do I do that?
Re: Creating a randomizer that does not repeat picks
Welcome to the site Hashim_553
There are a few code examples above, which parts of those are not clear to you?
There are a few code examples above, which parts of those are not clear to you?