472,993 Members | 3,146 Online

Hello!

Below is an example of an easy way to shuffle a Deck I must admit it's not
the most efficient way of doing it but the array is small so the time it's
take is hardy noticed at all.
But now to my question:
At the bottom of method Shuffle I have this statement:
"newDeck.CopyTo(cards, 0);"
which copy each Deck back into the cards array
I just wonder if I instead do this following
cards = newDeck; the result would be the same.

First cards is pointing to the array cards where each index in this array
cards is pointing to a Card object.
The same is true for the array newDeck as for cards.
So if I do cards = newDeck it would mean that cards is new pointing to the
newDeck array with all the random Card objects.
So as a summary it would be just the same as doing "newDeck.CopyTo(cards,
0);"
I just want to check with you if you agree with me.
So is it any point of doing this "newDeck.CopyTo(cards, 0);" insted of this
"cards = newDeck;"
It would be much faster to do this "cards = newDeck;"
insted of this
"newDeck.CopyTo(cards, 0);"

public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}

//Tony
Jun 27 '08 #1
1 1101
Tony Johansson wrote:
Hello!

Below is an example of an easy way to shuffle a Deck I must admit it's not
the most efficient way of doing it but the array is small so the time it's
take is hardy noticed at all.
But now to my question:
At the bottom of method Shuffle I have this statement:
"newDeck.CopyTo(cards, 0);"
which copy each Deck back into the cards array
I just wonder if I instead do this following
cards = newDeck; the result would be the same.

First cards is pointing to the array cards where each index in this array
cards is pointing to a Card object.
The same is true for the array newDeck as for cards.
So if I do cards = newDeck it would mean that cards is new pointing to the
newDeck array with all the random Card objects.
So as a summary it would be just the same as doing "newDeck.CopyTo(cards,
0);"
I just want to check with you if you agree with me.
So is it any point of doing this "newDeck.CopyTo(cards, 0);" insted of this
"cards = newDeck;"
It would be much faster to do this "cards = newDeck;"
insted of this
"newDeck.CopyTo(cards, 0);"

public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}

//Tony
Yes, you can safely replace the reference of the array instead of
copying the items to the array, provided of course that the reference is
the only reference to the array that you use.

public void Shuffle() {
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++) {
int destCard;
do {
destCard = rand.Next(52);
} while(assigned[destCard]);
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
cards = newDeck;
}
--