|
-
Sep 9th, 2001, 08:41 AM
#1
You horny little thing, you!
Fast, clean, non-duplicating array randomiser. This rules.
Code:
shuffle(int cards)
{
int x,tmp;
x=cards;
while(x--) CARDS[x]=x%52;
while(cards)
{
x=random(cards--);
tmp=CARDS[cards];
CARDS[cards]=CARDS[x];
CARDS[x]=tmp;
}
}
Courtesy: J Stadolnik.
-
Sep 9th, 2001, 09:24 PM
#2
It also ruled when published in Algol 45 years ago. See Knuth's 'The Art of Programming'
-
Sep 10th, 2001, 05:55 PM
#3
Frenzied Member
Do you have that code in VB by any chance?
You just proved that sig advertisements work.
-
Sep 11th, 2001, 01:26 PM
#4
Re: You horny little thing, you!
I'll try and do a quick translation, I haven't tested it though...
Code:
public CARDS(0 to 51) as integer
sub shuffle(byval cards as integer)
dim x as integer,tmp as integer
for x = 0 to cards - 1
CARDS(x) = x mod 52
next x
while cards > -1
'pick random card
x = int(rnd*cards)
cards = cards - 1
'swap cards
tmp = CARDS(cards)
CARDS(cards) = CARDS(x)
CARDS(x) = tmp
loop
End Sub
After calling this, the CARDS() array should be re-arranged to a new sequence of numbers.
As i said its not tested so it might be a bit buggy, just compare the code line for line with the C++ version to see the translation.
-
Sep 11th, 2001, 05:58 PM
#5
Registered User
If you just want to shuffle a 1D string array maybe this will work?
VB Code:
Private Sub Command1_Click()
Dim s(9) As String, i&, randomIndex&, tempVal$
For i = 0 To 9
s(i) = "Cogigate " & i & " times"
Next
For i = 9 To 0 Step -1
Do
Randomize
randomIndex = Int(10 * Rnd)
Loop Until randomIndex <> i
'Now we have the current index and a random index that are different
'Swap the values of the random index and the current index
tempVal = s(i)
s(i) = s(randomIndex)
s(randomIndex) = tempVal
Next
For i = 0 To 9
Debug.Print s(i)
Next
End Sub
-
Sep 13th, 2001, 02:33 PM
#6
I did a bunch of work on this type of thing about 20 years ago. I don't have any code, BUT this advice:
If you are really dealing cards (say for a bridge game) generally, the next thing you have to do is to sort them in each hand.
Instead of dealing the cards, deal the SEATS. Then, when you construct the hands the cards are in order.
-
Sep 14th, 2001, 02:24 PM
#7
-
Sep 14th, 2001, 02:49 PM
#8
If you start out with 1-52 cards and you have to deal
without replacement you end up with a random placement of
cards in a string/array/whatever. If you have dealt them to 4
players in a card game and you are going to have human
interaction on the screen, you then have to sort them by suit and
denomination. Very time consuming and somewhat tricky.
Assuming that you are dealing to 4 putative players they can be
labelled N,E,W,S (north, east,west,south). In this case, you
randomly determine which of the 4 (or however manyplayers) get
the CARD.
When you end up you have the original deck 1-52 in order, and a
string or array of WHO GETS THE CARD. To build the presentation
hand, you then only have to walk down the list assigning each.
There are also some significant gains in the actual mechanics of
the logic of computer play (ex: second hand low, cover an honor
with an honor, .....)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|