Comp 1572, Winter 2002
Programming Assignment 3
Assigned February 1, 2002
Due Wednesday, February 13, 2002

Submission guidelines

As always, all code must be written individually and independently. Mail your source code (header file and .cpp file or files) to comp1572@cs.du.edu before class on the due date. The subject line should say Programming Assignment 3. The source code should be sent as attachments, and should be named yourname.cpp. This will help the grader keep track of whose is whose. Turn in a hard copy of the header files and source code at the beginning of class on the due date. Also turn in a hard copy of a separate document which explains your program - this should include an explanation of the input and output of your program, the structure of your program (including a brief explanation of each function, the variables you are using in your program, etc.)

At the top of each file, put comments with your name, the date, course number, and assignment number. As always, write the program with a well organized structure, format it consistently, and document your program by naming functions and variables in a useful way, and by writing comments that would help someone understand how the code works so they could modify, enhance, or debug it. Functions should perform a specific and well-defined task that is described in comment lines above the function header. The logical flow of your program should be simple and easy to follow.

The problem

This assignment is to write a program that will play poker with you. The game is called 5-card draw, but we're not including any betting! There are many variations on the rules, but we will use the rules described below.

The program will "shuffle" a standard deck of 52 cards*, and will "deal" 5 cards to itself and 5 cards to you (these are called "hands"). It will display the 5 cards in your hand. It will ask you how many cards you want to discard (up to 3), and which ones they are (you can respond with 1, 2, 3, 4, 5 to identify which cards). Then it will replace these with 3 more from the deck (different from the ones already dealt, obviously). The program will then show you your new hand and classify it (for example, it will say "You have two pairs"). Then it will reveal its own hand and classify it, determine which one is better, and tell you who wins. It will then ask you if you want to play another hand, and continue playing hands with you until you say no. When you say no, it will tell you how many hands it won and how many you won, then exit.

The dealer (the computer) should get a chance to discard and draw just as you did. After giving you your new cards, make a call to a "stub" function (which, as always, has a name that accurately describes its function). You can leave the function empty (so the dealer always keeps its original hand), but this gives you a location to insert that code later. If you are interested, you can come up with an algorithm for deciding how many cards the dealer should discard, then insert it into this function, along with the code for replacing those cards. Filling in the code for this more challenging function is optional.

Poker hands

You'll now need to know how to classify hands, and how to determine which are better. The best hand is a straight flush. It has five cards in order (such as 5-6-7-8-9), all of the same suit. Aces can be high or low, but not both (A-2-3-4-5 is fine, so is 10-J-Q-K-A, but K-A-2-3-4 is not). If both players have a straight flush, then the one with the highest cards wins (the high card in a A-2-3-4-5 straight is the 5). If the two high cards have the same rank, then the one with the better suit wins.

The next best hand is four-of-a-kind, which has four cards of the same rank such as 5-5-5-5-8. If both players have this hand, then the hand with the higher rank of the foursome wins.

Next is a full-house, which is three of a kind together with a pair, such as K-K-K-5-5. If the two players both have a full-house, the one with the higher rank of the triple wins.

After that is the flush, in which all five cards are of the same suit. If both players have flushes, follow the rule for high-card.

The next best hand after the flush is the straight, which is 5 cards in order, but not all in the same suit. As before, aces are high or low, but can't wrap around. Ties are broken by the high card rank in the straight, and if the high cards are of the same rank, then the one of higher suit wins.

After the straight comes three-of-a-kind; three cards are of the same rank, and the remaining two cards are not a pair. Ties are broken by the rank of the triple.

Next is two-pairs, which is two distinct pairs of cards, and a 5th card different from the rest. Ties are broken by the rank of the high pair.

Next we have one-pair - a matched pair and three other distinct cards. Ties are broken by the rank of the pair.

The worst hand is a hand which holds none of the above. Ties in this last case are broken by the high card. The card with higher rank wins, and if their ranks are the same, then the one with a higher suit wins.

Shuffling and Dealing

You will need to shuffle and deal the cards. Section 5.10 of Deitel and Deitel shows an algorithm for doing this. You should begin by reading this algorithm. The shuffling and dealing function you write will be an improvement of this technique. Problem 5.16 of Deitel and Deitel describes the shuffling and dealing algorithm you should use. You don't need to do problem 5.16 - just use the algorithm it describes. The functions you design will have a different interface than those given in the text, because you won't want to deal all 52 cards at once. Also, don't feel obligated to store the deck or the strings representing the card ranks and card suits in the same way as the text does.

_______________
* A standard deck of 52 cards has a 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (J), Queen (Q), King (K) and Ace (A) in each of 4 suits (Clubs, Diamonds, Hearts and Spades). The cards are ordered - to tell if one card is higher than another, look at its rank (they're listed above in increasing order). If the two cards have the same rank, then look at the suit (they're listed above in increasing order)