T
Tony Johansson
Hello!
I have read in a book the following.
The last line of this method Shuffle shown below uses the CopyTo() method of
the System.Array class(used whenever you create an array) to copy each of
the Cards in newDeck back into cards. This means that you are using the same
set of cards object
in the same cards object rather than creating any new instances. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
Now to my question is it safe to replace newDeck.CopyTo(cards, 0);
with cards = newDeck in the last row in the method Shuffle below..
The text above is mentioned something about not being safe. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
So what is the author trying to say with this This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
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;
}
newDeck.CopyTo(cards, 0);
}
//Tony
I have read in a book the following.
The last line of this method Shuffle shown below uses the CopyTo() method of
the System.Array class(used whenever you create an array) to copy each of
the Cards in newDeck back into cards. This means that you are using the same
set of cards object
in the same cards object rather than creating any new instances. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
Now to my question is it safe to replace newDeck.CopyTo(cards, 0);
with cards = newDeck in the last row in the method Shuffle below..
The text above is mentioned something about not being safe. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
So what is the author trying to say with this This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
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;
}
newDeck.CopyTo(cards, 0);
}
//Tony