Hello Readers,
As this is my first blog post I thought I would go for something relatively straightforward. I don't think that my solution to the following problem is the best way to solve the problem, but I found it very hard to figure out the way to do this at first.
So let's assume that you have a class called Card and a list of type Card. If you don't understand what I just said, I recommend reading this first: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx. I have a function called "create deck" which adds 52 cards to the list (not using jokers) and I would like to create a function that will shuffle the order of the list.
Here is the function:
public List<Object> ShuffleDeck(List<Object> myDeck)
{
List<Object> outputDeck = new List<Object>();
Random rand = new Random();
while (myDeck.Count > 0)
{
int grabIndex = rand.Next((myDeck.Count));
outputDeck.Add(myDeck[grabIndex]);
myDeck.Remove(myDeck[grabIndex]);
}
return outputDeck;
}
And you call it like this:
//where myDeck already exists
myDeck = ShuffleDeck(myDeck);
The only catch is if you are using a List<objectNameHere>, for example List<Card> (as in my application), you must replace the part "List<Object>" in the function with "List<objectNameHere>" and you're good to go!
good luck,
Zero Gravity Chimp