|
Objectives
|
DecksIn assignments 17 and 18, you worked with arrays of integers. In this lab, you will create an array of playing cards, initialize it, shuffle it, and sort it. |
Consider the way humans shuffle, which is usually by dividing the deck in two and then reassembling the deck by choosing alternately from each deck. This method works because humans usually don't shuffle perfectly, and the deck is fairly randomized after about seven iterations. A computer program has the annoying property of doing a perfect shuffle every time, so this method is not at all random. In fact, after eight perfect shuffles, the order of the deck is back to the same order as the beginning. For a discussion of this claim, take a look at this website (http://www.wiskit.com/marilyn/craig.html).
An alternative shuffling algorithm more suited to the computer is to go through a deck from start to finish and for each card, choose another random card from the deck and swap them. Here is a pseudocode outline of how this algorithm works. (Recall that pseudocode is just a combination of C++ statements and English words.)
for( i, starting at zero and going to DECKSIZE ) {
// choose a random number between i and the DECKSIZE
// swap the ith card and the randomly-chosen card
}
|
randomInt(...) that generates a random
integer between the parameters low and high used to
choose a card and
swapCards(...) that takes two indices and switches the
cards at the indicated positions.
It is unnecessary for a computer program to deal the cards in this way because it will perform a completely random shuffle (provided that it was encoded correctly). Therefore, how the program deals hands is up to you.
In Assignment 15 we use the
enumerated type to build a class of playing cards.
In this lab, you will build a deck of playing cards and then shuffle it.
Make a copy of YourLastName_306A15.cpp and rename it
YourLastName_306L8.cpp for this lab.
|
|
Card
class-- you should not be using any integers for these types,
only the named constants.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Lab #8: Put a sentence about what your program does here.
/* Put what your program does here */
When you have completed your program and have it working to your satisfaction, drop the source code and your Microsoft Word Lab write-up YourLastNames_306L8.doc into the CSC306_L08 dropbox on the Academic server.