I have 4 players and I need to give randomly 52 cards to all four of them (13 cards per each player). Does anyone have an idea of a perfect algorithm.
Printable View
I have 4 players and I need to give randomly 52 cards to all four of them (13 cards per each player). Does anyone have an idea of a perfect algorithm.
I don't know how much of a help this is, but you can get Cards32.dll from Microsoft (it's free ;) ). I have never personally used it, but I have seen some other programs that use it (the code that is), and it doesn't seem that hard. It can even generate playing cards (if that's what you want!).
It was used in games like Microsoft Solitaire.
This is air code, so it might need a tweak or two, but it should get you close
VB Code:
Sub DealCards dim i as integer dim j as integer dim k as integer dim Card(51) as integer ' 52 cards dim fFound as Boolean dim Player(3, 12) as integer ' 4 players, 13 cards each dim iRnd as integer for i=0 to 12 for j = 0 to 3 Do fFound = False ' reset flag iRnd = int(rnd * 52) for k = 0 to 51 if Card(k) then fFound=true ' Set flag Exit For End if next k if not ffound then Card(iRnd) = 1 Player(j, i) = Card(iRnd) Exit Do end if Loop Next j Next i
makes sense to me and it's sounds a lot better than what I did. I'll try your way next time I need to do something similar