CSC 306 Introduction to Programming with C++

Shuffling the Card Class


http://people.csail.mit.edu/dshin/www/cards.GIF

Objectives

  • To gain more confidence in the software design process.

  • To make use of pseudo-random numbers in shuffling a deck of playing cards.

  • To sort a hand of cards so that they are easily used by the player.

Decks

In 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.

Shuffling

Most card games require shuffling the deck to introduce a random element into the game. We already know how to generate random numbers, but it is not completely obvious how to use them to shuffle a deck of cards.

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
}
Pseudocode is used to make clear what the purpose each function has for the program as a whole. Designing a program this way makes sure you know exactly what you need. In this case, this code fragment tells you that you have a loop, and for each iteration, you need functions such as

  1. randomInt(...) that generates a random integer between the parameters low and high used to choose a card and
  2. swapCards(...) that takes two indices and switches the cards at the indicated positions.

Dealing a Card Hand

A dealer distributes a certain number of cards to each person playing a card game, and the set a person is dealt is called their "hand". A common way to deal cards is to give one to each person in a round robin fashion---if there are 3 people, give the first card to person A, second card to person B, third card to person C, fourth card to person A, and so forth. Why deal cards this way? The average person does not realize that the reason is because the dealer does not do a completely random shuffle, and this method of dealing cards reduces the chance of the dealer being able to cheat.

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.


Lab Specifics

This lab is to be done individually.

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.
  • Using an array of cards, create a full deck of 52 playing cards.
  • Create a function that can shuffle the deck of cards.
  • Create a function that will output to the console the entire deck of cards for testing purposes. Call this function once when the deck is still in order and again after it has been shuffled.
  • There are 4 players in this program. Ask the user how many cards will be in each hand. (You may assume that there will be no more than 13 cards per hand.) Deal a set of this number of cards to each of the four players.
    Note: There cannot be any duplicates between the hands for each player.
  • Use the display function defined above to show the contents of each player's hand.
  • Finally sort each of the four hands in some reasonable fashion, and write out a list of the four players with each of their hands in sorted order.
http://www.seps.org/images/giant%20playing%20cards.jpg
Be sure to:

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.


Back to Introduction to Computer Programming with C++ Homepage