471,337 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

about array

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 1052
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;
}
--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by J. Campbell | last post: by
3 posts views Thread by Joe C | last post: by
5 posts views Thread by Tony Johansson | last post: by
3 posts views Thread by J Wang | last post: by
13 posts views Thread by agentxx04 | last post: by
15 posts views Thread by copx | last post: by
14 posts views Thread by ablock | last post: by
15 posts views Thread by SM | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.