Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Game Design Theory / Card Game shuffle function

Author
Message
livin 4 rocknroll
19
Years of Service
User Offline
Joined: 19th Oct 2004
Location:
Posted: 19th Oct 2004 22:49
i am making a card game. I have it almost completed all i need to do is figure out how to shuffle the deck properly. I made a function that uses a loop to fill an array with a random number from 1 to 52. This works great except that sometimes (actually most of the time) the seed in DB that generates random numbers will repeat some numbers so i end having for example two aces of spades or 6 jacks of clubs. How can i create a function that checks for repeated numbers and then re-runs the shuffle function until there are no repeated numbers?

Joshua
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 19th Oct 2004 23:10 Edited at: 19th Oct 2004 23:11
Ahh, forget random numbers for now - just set the deck so it has all your cards in any order.

Now instead of using a random number to set a card - you take each of your cards in turn and stick them in the deck at random locations, shuffling them really .

So, say your cards are refered to as images that can be simply stored as a number. Like DECK(52)



And that should leave you with 52 unique and shuffled cards.


Van-B


Muhahahahaha.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 19th Oct 2004 23:39
I use a similar but slightly different method. I simply loop n number of times, and disregard whether I have used the card before.

To give an idea of speed, you can "shuffle" 30,000 + times in under a second on a 1Ghz machine. 100 shuffles will pick up each card and move it around an average of 4 times.

Different method, same result. The reason I mention this is because you can adapt code to introduce an element of predictability and chance, good for setting levels on card games and fruit machines. by reducing the number of loops, you can leave patterns in the sequence.

BatVink
http://biglaugh.co.uk/catalog AMD 3000+ Barton, 512Mb Ram, 120 Gig Drive space, GeForce 5200 FX 128 Mb, Asus A7N8X Mobo.
Terms & Conditions apply
zircher
21
Years of Service
User Offline
Joined: 27th Dec 2002
Location: Oklahoma
Posted: 20th Oct 2004 00:27
Another method that I have used in the past is to have an array of 52 cards and roll 1d52 to select a card, and 1d51 for the next, etc. As I pull a card, I set the array value to 1. When I roll for the next card, I walk the array skipping over any 1's. This has been a fairly successful method for me that always generates a nicely shuffled deck.
--
TAZ

"Do you think it is wise to provoke him?" "It's what I do." -- Stargate SG-1

Login to post a reply

Server time is: 2024-05-06 09:28:15
Your offset time is: 2024-05-06 09:28:15