Computer Science 173
Intermediate Computer Programming

Denison


Homework 3

Overall homework comments:

Program grading will not be based solely on whether or not the programs "works." It will also be based on the organization, structure, and documentation (in the form of program comments) for the submitted work.

Be sure and document your code well. Include:

  1. Your name in a comment block at the top of every source file.
  2. A high level description of the problem solved by the program.
  3. Descriptive comments for each function.

Please see the C++ Programming Style Guide and follow the conventions described there. Up to 15% of the grade for this homework will be based on following these conventions and practicing good documentation.

Program

This program will utilized three different user-defined class types as well as a client program.  The classes to be defined include the Card, Deck, and PokerHand classes discussed during classtime.  The main program will exercise these classes by creating object instances of the types in a manner like a game of Poker.  This program will give you a basic idea of how a Poker game might be constructed.

The main program will create an object for a Deck of cards, shuffle the deck, and then deal out two five-card Hands, one for a first player, and another for a second player.  The main program will then invoke a series of functions from the PokerHand class looking for the highest level Poker category the players' hand conforms to -- from Royal Flush to Straight Flush, etc.  As soon as a match is found, the program will simply report the category and then output the cards in the hand.  This will be repeated for both player 1 and player 2.  Note that the program does not need to evaluate the "winner" -- (i.e. it does not need to compare the hands of the two players to determine which is better).

Recall that an individual card has the notion of rank, from 1 (ace) up to 13 (king), along with a suit.

Poker Hand Categories, from best to worst, are as follows:

To assure consistency and to help move forward with well documented interface files, I include the header files for the Card, Deck, and PokerHand classes. These must be used without modification. The implementation files must be named Card.cpp, Deck.cpp, and PokerHand.cpp. The driver program will be called poker.cpp. You should also submit a main program file called testPoker.cpp which demonstates the correct operation (through individual tests) of as much of the functionality of your classes as you can show. In your submission archive, you will thus have 8 files, two for each of the three classes, and two program driver files.

Tips and Suggestions for proceeding:

  1. Start small! Begin by simply coding the remainder of the Card.cpp implementation file. There are four simple functions remaining that you need to write. I have started you out with the definitions of the two constructors for the Card class. This starter file is at the Card.cpp link. As you write each of the functions, check/verify your correct syntax by just compiling the Card.cpp implementation file with g++ -c Card.cpp.
  2. Once you have Card.cpp completed, write a small client program that uses just your new Card class and creates some object instances and tests out each of your function members. This test .cpp file would #include the Card.h interface file and, in the main function, declare some Card object variables. Start with a declaration like `Card c1' which would invoke the default constructor. Then try a declaration like `Card c2(13, Card::DIAMONDS)' to create a card for the king of diamonds. Then invoke the function members on both of these object instances. This is exactly how my own cardTest.cpp file began, with the first few lines whose purpose was to test the Card class. Yourf own version of a cardTest file (for example mycardTest.cpp) can be combined with your Card.cpp file into an executable by compiling them with
    g++ -o mycardTest mycardTest.cpp Card.cpp
    .
  3. Next begin development of the Deck class. Start by creating a Deck.cpp file that simply has a single function definition -- that of the Deck constructor, in which you loop through and create the 52 cards in the Card array for the deck. Note that you can compile this Deck.cpp file by itself to check syntax, and you can create executables without writing all the remaining functions of the Deck class, and would create the executable with what you have at this point with g++ -o mycardTest mycardTest.cpp Card.cpp Deck.cpp and can incrementally add tests (like creating a Deck object) in your evolving mycardTest.cpp client.
  4. The shuffle function could be the next for you to develop. If we consider the set of cards by their index in the deck array from 0 to 51. One algorithm to shuffle the cards is described as follows:

    1. For each position (except the last), call it pos, from 0 to 50:
      1. Generate a random number, r, in the inclusive range (pos .. 51).
      2. Swap the card at position pos with the card at position r.

The library named cstdlib has a function random() that returns an integer from 0 to RAND_MAX (a large constant). This can be incorporated into a program by doing a #include <cstdlib> and then simply invoking the function in your program. Tyoically, the value returned is constrained to a range 0 to some limit - 1, by taking the value returned modulo the limit.