Example #1
0
        /// <summary>
        /// Deals a number of cards based on the number specified.
        /// </summary>
        /// <param name="numberOfCards">The number of cards you want dealt</param>
        /// <returns>A list of the cards dealt</returns>
        public List <Card> Deal(int numberOfCards)
        {
            List <Card> DealtCards = new List <Card>(); // create a holding list

            for (int i = 0; i < numberOfCards; i++)     // loop through as many times as cards that are needed
            {
                Card drawn = DeckofCards.First();       // draw a card

                DealtCards.Add(drawn);                  // put it in the holding list

                DeckofCards.Remove(drawn);              // remove it from the deck
            }

            return(DealtCards);  // return the holding list to the player
        }
Example #2
0
        /// <summary>
        /// Shuffles the deck using Fisher-Yates algorithm.
        /// </summary>
        /// <returns>A shuffled deck of cards</returns>
        public List <Card> Shuffle()
        {
            // place all of the cards back into the deck;
            DeckofCards.AddRange(DiscardedCards);

            // Use Fisher-Yates algorithm to evenly shuffle deck
            Random r = new Random((int)DateTime.Now.ToBinary()); // ensure the seed is changes
            int    n = DeckofCards.Count;                        // number of cards left in the deck

            while (n > 1)                                        // loop the deck until all cards have been touched
            {
                int k = r.Next(n);                               // grab a random number where 0 <= k < n
                n--;                                             // take one card out of shuffle
                Card temp = DeckofCards[n];                      // grab a card
                DeckofCards[n] = DeckofCards[k];                 // store it in deck[n]'s spot
                DeckofCards[k] = temp;                           // store the temp card in deck[k]'s spot
            }
            return(DeckofCards);                                 // return the shuffled deck
        }