/** * Method for copying cards into another Cards instance, like for Deck.Shuffle() * Assumes that source and target collections are the same size */ public void CopyTo(Cards target) { for (int index = 0; index < this.Count; index++) { target[index] = this[index]; } }
public void Shuffle() { //The temporary deck Cards shuffledDeck = new Cards(); //Whether the card at index has already been added to the temporary deck bool[] assigned = new bool[m_cards.Count]; Random r = new Random(); for (int index = 0; index < m_cards.Count; index++) { int picked = -1; bool available = false; //Loop until an available card index is picked, // inefficient but simple and not problematic due to limited size of range while (!available) { //Get a random number in the range [0,SIZE_OF_DECK) picked = r.Next(SIZE_OF_DECK); available = !assigned[picked]; } assigned[picked] = true; shuffledDeck.Add(m_cards[picked]); } //replace the ordered deck with the shuffled deck shuffledDeck.CopyTo(m_cards); }