Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 HELP Unique card shuffling
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
turingnoob




PostPosted: Sat Jan 17, 2009 1:58 pm   Post subject: HELP Unique card shuffling

We have a game which needs to shuffle a deck of 52 cards, and we need all of the card to be unique (can't have the same card showing up more than once)

This is our code, which just shuffles from to 52

Turing:
procedure shuffleCard
    card := Rand.Int (1, 52)
end shuffleCard


but we need to know how to randomize them, so they're not the same. This code just picks out any card


Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
chrisbrown




PostPosted: Sat Jan 17, 2009 2:08 pm   Post subject: RE:HELP Unique card shuffling

Think about what your code is actually doing. All it does is assign a random value between 1 and 52 to card. Unless you've taken this into account elsewhere, you'll need an array of cards that are initialized with the values 1 .. 52 (think "for" loop). To shuffle them, you'll need to repeatedly swap cards.

Take a look:
http://compsci.ca/v3/viewtopic.php?t=20031
The_Bean




PostPosted: Sat Jan 17, 2009 2:24 pm   Post subject: Re: HELP Unique card shuffling

Theres 2 methods to doing this that I know of.

1) create a second array of type boolean, which i will call 'alreadyUsed' and make them all set to false.
make a for statement that loops through all 52 cards, and inside of that have a loop
make a variable thats = to a random number between 1,52 check to see if that element in the array alreadyUsed is = to false.
If it's false then card (i):= the random number and exit the loop.
If alreadyUsed= true then do nothing and go back to the beginning of the loop and pick another random number number.

This method can slow because on the 52 card you have to find the last card that isn't used.


Method 2
start off by making a for loop that goes through all 52 cards and make card(i):=i
this way all cards are now in order and only used once.
Now to scramble them around.
Make a second array of 52 elements, and loop through it making each element= Rand.Int(-maxint,maxint)
now you have a random array of 52 and a sorted array of 52, heres where we switch them.
Use a bubble sort and go through the random array sorting it out.
When you switch the elements in the random array, also switch the elements in the cards array.
This will leave with the random array sorted, and the cards array random.

This method is mainly dependent on the amount of time it takes to bubble sort.
jbking




PostPosted: Sat Jan 17, 2009 2:48 pm   Post subject: Re: HELP Unique card shuffling

"More Random than Random" from ITToolbox has a bit of a discussion on the problem of how to shuffle a deck of cards in a few ways. Although the code is in C# and not Turing, the ideas presented in terms of how the code should work should prove useful to you.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: