views:

66

answers:

1

Hi, i know that there are many ways to program a deck of poker cards where value is important after searching on the net.

My problem is that my "deck" of cards is a simple array with value as "card name" (example 1 = Ace of Spades)

Do i randomize my current Array with NSMutableArray and then remove index 0 everytime the card is being "drawn" til the last card?"

+3  A: 

The standard algorithm is something like this:

a) Populate an array of 52 cards (order not important)
b) Set size variable to 52
c) Pick a random number r in range 0 <= r < size
d) Store the picked card, card[r],  as the result
e) Swap card[r] and card[size - 1]
f) decrement size
g) goto c
anon
Thank you so much!!! i did exactly what you say except that i removeObjectAtIndex:r instead, any caveat to that?
zerlphr
That should work, but it means you have to re-populate the array when you have dealt all the cards. In my version, you just need to set size back to 52.
anon
oh that's a good point.. Thank you so much, Neil!
zerlphr