Exemplo n.º 1
0
        // The shuffle function will instantiate a new deck of cards.  It will then copy the cards from the existing card
        // array and place into the new array at random (dependant on the random number it generated). The sole function of
        // this shuffle array is to loop 52 times from 0 to 51.  Each cycle will produce a number between 0 and 51 using the random
        // function. Then, using the .Next(x) method, the function will generate a random number between 0 and x. This number will
        // then assign that id to slot in the new array.


        // WHAT DO I WANT THIS TO DO?

        // REPEAT STEP ONE AN ARBITRARY NUMBER OF TIMES (SAY 5?)

        // STEP ONE:
        // while deck (!empty)
        // PICK A RANDOM INDEX
        // PICK A RANDOM NUMBER OF CARDS TO PULL FROM THE OLD DECK
        // MAKE SURE THE NUMBER OF CARDS YOU WANT TO PULL DOESNT EXCEED THE SIZE OF THE OLD DECK (CARDS TO PULL < DECK.SIZE).  IF SO, THEN
        // ADD THE LAST CARDS FROM THE OLD DECK TO THE TOP OF THE NEW DECK
        // ADD # OF CARDS TO PULL TO THE TOP OF NEW DECK LIST AND REMOVE FROM OLD DECK LIST
        // STEP TWO:
        // MOVE ALL CARDS FROM NEW DECK LIST BACK TO OLD DECK LIST IN ORDER (FOR LOOP TO ITERATE THROUGH THE NEW DECK LIST AND FILL THE OLD DECK LIST)


        public void Shuffle()
        {
            BunchOfCards newDeck = new BunchOfCards();  // ArrayList called newDeck to hold a Cards object (essentially a bunch f card objects)

            //Card[] newDeck = new Card[52];  // temporary card array for original deck of cards to be copied into
            bool[] assigned  = new bool[this.Count];         // array of boolean variables to flag whether a card exists at that index
            Random sourceGen = new Random();                 // instantated random number using Random class

            for (int i = 0; i < this.Count; i++)             // iterate through all 52 card objects...
            {
                int sourceCard = 0;                          // iterator
                //int destCard = 0;  // container to hold the randomly generated number used for the index
                bool foundCard = false;                      // flag to see if the card exists, defaulted to false
                while (foundCard == false)                   // while the flag has not found a card in that slot
                {
                    sourceCard = sourceGen.Next(this.Count); // iterate through 52 cards and assign a random number
                    if (assigned[sourceCard] == false)       // if there is no card in that slot in the boolean array
                    {
                        foundCard = true;                    // set the found statement to true
                    }
                    //destCard = sourceGen.Next(52);  // generate a random number to 52
                    //if (assigned[destCard] == false)  // if the spot in the assigned array is empty at the index of the randomly generated number
                    //  foundCard = true;  // raise that index spots flag to true
                }
                assigned[sourceCard] = true;   // once the boolean array all equals true.....
                newDeck.Add(this[sourceCard]); // add the cards iterating through all 52 card objects and add them tot he ArrayList
                //assigned[destCard] = true; // set the assigned card array to true
                //newDeck[destCard] = cards[i];  // slot that created card object into the cards array at that index
            }

            //newDeck.CopyTo(cards, 0);  // once all the cards have been created, copy the cards from the temp array to the cards array
            newDeck.CopyTo(this);  //copy new deck to cards deck for use in play
        }
Exemplo n.º 2
0
 /// <summary>
 /// Utility method for copying card instances into another Cards
 /// instance—used in Deck.Shuffle(). This implementation assumes that
 /// source and target collections are the same size.
 /// </summary>
 public void CopyTo(BunchOfCards targetCards)
 {
     for (int index = 0; index < this.Count; index++)
     {
         targetCards[index] = this[index];
     }
 }