Card Lab in C++


see in lab activity too.

The prelab activity involves the classes and program below.

The main program is in play.cpp, the other files provide a class Card and a class Deck for playing cards.

To create an executable you'll need to put all the .cpp files into one project.

The output will go by very quickly. To see the output one screen at a time, pipe the output to a pager; the common paging programs in Unix are named more and less. You pipe the output of play to the pager using the syntax below. Press the space bar to see a new screen-full, the return key to see one new line of output, and the 'b' key to back up in the output. Run the program and try the keys in the pager (you can't always back up when using more).

    play | less

Prelab Problems

Joker's Wild

Add the following loop before the code that deals 5 cards to the vector player but after the deck is printed. The loop removes all the cards from the Deck d. for(k=0; k < 52; k++) { d.GetCard(); } What is the output? (write it below). Why is this the output?

Counting Cards

Write a function named IsFlush that takes a vector of cards as a parameter and returns true if all the cards are the same suit (this is called a flush in Poker.) bool IsFlush(const tvector<Card>& hand) // pre: hand.size() is # cards in hand // post: return true iff all cards in hand are the same suit To tell if all cards are the same suit, use the following idea: Test this code by writing code like this in main Card c = d.GetCard(); // get one card player.push_back(c); // add same card to player five times player.push_back(c); player.push_back(c); player.push_back(c); player.push_back(c); You'll probably want to try some more testing, when you're sure that IsFlush works modify the code in main so that 10,000 five-card hands are dealt to player. Shuffle the Deck d before dealing each hand. You won't want to print each hand, that will take too long. Check each five-card hand to see if it's a flush and print the number of flushes that are dealt in 10,000 hands.

When you've tested IsFlush, write a function named FourAces that returns true if a hand contains four aces. The function Card::GetRank() returns the rank of a card, an ace has rank 1 in the class Card.

Test both IsFlush and FourAces by generating 100,000 hands and testing them. Print the hands of four aces to check your output, and accumulate a count of how many hands contain four aces as well as how many hands are flushes.

Extra Credit

For extra credit write a function IsFullHouse that returns true if a hand is a full house, that is it contains one pair and three of a kind. For example, two aces and three tens is a full house as is three nines and two sixes. This extra credit can take the place of a missed prelab.

Bring a printed copy of play.cpp to your lab.

In Lab

You will be given problems to work on during lab.

Each lab is worth three points. A TA must check each of the points. It is your responsibility to ensure that a TA awards each point. After a lab is over you cannot receive credit for the first two points.

Owen L. Astrachan
Last modified: Sun Nov 21 20:01:20 EST 1999