Пример #1
0
        private Card[] deckOCards;// I suppose I could already specify the size of this Card array but I chose to do that in the deck constructor.

        //=======CLASS CONSTRUCTORS CREATE STANDARD 52 CARD DECK=========

        /// <summary>
        /// Deck object constructor, contains 52 individually constructed Card objects, one for each suit and face combination.
        /// Constructor DOES 'shuffle' the deck by default, placing all 52 cards in a randomly generated order.
        /// </summary>
        /// <param name="deckId">number passed to simply distinguish one deck from another.  could be particularily useful
        /// if more than one deck is "in play"</param>
        public Deck(int deckId)
        {
            deckNumber = deckId;
            Card[] tempDeck1 = new Card[52];

            //Creates 52 card objects representing standard deck (IN ORDER)
            int c = 0;

            foreach (Card.Suit suit_value in Enum.GetValues(typeof(Card.Suit)))
            //for hearts, diamonds, clubs, and spades
            {
                foreach (Card.Face face_value in Enum.GetValues(typeof(Card.Face)))
                {//create a card of 2, 3, 4, 5, ....to 14 (Ace)
                    tempDeck1[c] = new Card(suit_value, face_value);
                    ++c;
                }
            }
            //End of creating 52 card objects

            cardsInDeck = c;                           //establishes Deck field value to be equal to "52" at instantiation
            deckOCards  = new Card[52];                //holds 52 card objects to be represented in deck - is the Deck object field
            //Card[] tempDeck2 = new Card[52];
            List <Card> cardList = tempDeck1.ToList(); //puts deck that was created IN ORDER to a list that will be used to 'pull from' when randomizing the order

            int a = 0;

            while (a < 2)                  //"shuffle" twice to ensure "random-ness"
            {
                Random rnd = new Random(); //random object used to generate random number used in "shuffle"
                int    n   = 52;
                while (n > 0)
                {
                    --n;
                    int k = rnd.Next(n);//k is a random number whose value is between 0 and n
                    //n is decreased each time so that the random number dynamically reflects the number of Card objects
                    //in the cardList.
                    deckOCards[n] = cardList[k]; //place the Card object from the cardList into the 'final'
                    //Card array which will represent the shuffled deck.
                    cardList.RemoveAt(k);        //removes Card from cardList so the Card object cannot be 'chosen' again
                }
                ++a;
                if (a == 1)                         //only happens after the 'first' shuffle
                {
                    cardList = deckOCards.ToList(); //puts the now shuffled array back into List form to be 'shuffled' again
                }
            }
        }