< previous page page_1099 next page >

Page 1099
void CardDeck::Shuffle( /* in */ int numberOfShuffles )

// Rearranges the deck (the list of DECK_SIZE components) into a
// different order.  The list is divided into two parts, which are
// then merged.  The process is repeated numberOfShuffles times

// Precondition:
//     Length of deck == DECK_SIZE
//  && numberOfShuffles is assigned
// Postcondition:
//     The order of components in the deck has been rearranged
//     numberOfShuffles times, randomly

{
    CardType tempCard;    // Temporary card
    int      count 1;     // Loop counter
    int      count 2;     // Loop counter
    int      sizeOfCut;   // Size of simulated cut

    for (count1 = 1; count1 <= numberOfShuffles; count1++)
    {
        CardPile halfA;    // Half of the list, initially empty
        CardPile halfB;    // Half of the list, initially empty

        sizeOfCut = rand() % DECK_SIZE + 1;

        // Divide deck into two parts

        for (count2 = 1; count2 <= sizeOfCut; count2++)
        {
            RemoveTop(tempCard);
            halfA.InsertTop(tempCard);
        }
        for (count2 = sizeOfCut+1; count2 <= DECK_SIZE; count2++)
        {
            RemoveTop(tempCard);
            halfB.InsertTop(tempCard);
        }
        if (sizeOfCut <= HALF_DECK)
            Merge(halfA, halfB);
        else
            Merge(halfB, halfA);
    }
}

 
< previous page page_1099 next page >