CSE 4208 Lab 2: Evaluating and Ranking Hands

Due by Monday October 27, 2025, at 11:59 pm
Final grade percentage: 15 percent

Objective:

This lab is intended to further your experience with procedural, functional, object-oriented, and generic programming styles, and with additional features of the C++ language and libraries, including:

You will again use procedural, functional, object-oriented, and generic programming paradigms in the C++ language and libraries (as of the C++17 standard) to extend your common and game-specific abstractions for (Partnership Auction) Pinochle and (Texas hold 'em) Poker. To do this, you will add new features to your CardSet class template to facilitate examining different combinations of cards within each player's hand (in the Pinochle game) or between each player's hand and the common board of cards (in the Hold 'Em game). You will then add new enumerations, a nested struct, and different evaluation functions so that each game will evaluate the players hands based on those game-specific evaluations.

As you develop your code, please use good modularity: for example, if a function's logic has several separable parts (or is long enough to fill the editor's window) you may want to break it up into other smaller functions.

Note: some details of the assignment again are intentionally slightly under-specified, leaving you some room to choose what you think is the best way to implement them, as long as what you do is reasonable and you explain your design decisions in comments in the code and in your readme file.

Assignment:

    Part I - Readings:

  1. The following readings in the required text book may be useful as reference material while working on this lab assignment, and though we'll touch on some of the issues in the next studio sessions, selectively looking up topics and/or reading ahead (on demand as you encounter issues while working on the lab) in the text book (or looking up topics in the on-line C++ Reference) to refresh your familiarity with C++ may be necessary.

  2. Please again review the CSE 4208 Programming Guidelines, and follow them as you develop your solution to each lab assignment this semester.

    Part II - Program Design and Implementation:

  3. Log into one of the Linux Lab machines via qlogin, and (as you did in Studio 0) confirm that the correct version (8.x) of the g++ compiler is installed in your environment there.

  4. In your directory for this course this semester, please create another directory where you will keep your files for your solution to this lab assignment, and cd into it.
  5. In that directory create a readme file (e.g., named readme.txt), in which you will record your observations, design decisions, and other information pertaining to the lab assignment as you develop your solution.
  6. Also copy over the CMakeLists.txt and source (.cpp) and header (.h) files from your previous lab assignment into that directory - you will use (and in some cases modify) them along with additional code you will write, in this lab assignment. If appropriate, you may want to rename some files and update your CMakeLists.txt accordingly (e.g., so that lab1.cpp becomes lab2.cpp as a starting point for this assignment).
  7. In the header and source files for your Card class template, add two non-member function templates that are parameterized with rank and suit types, take two const references to Card objects parameterized with those type parameters, and return a bool value. The first of those functions should return true if the first card's rank is less than the second card's rank, or if they are of the same rank if the first card's suit is less than the second card's suit; otherwise it should return false. The second of those functions should return true if the first card's suit is less than the second card's suit, or if they are of the same suit if the first card's rank is less than the second card's rank; otherwise it should return false.
  8. Ensure your CardSet class template has a public copy constructor that takes a const reference to another CardSet with the same type parameterization, and performs a deep copy of that object. That is, the copy constructor should (without modifying the original object from which it is being constructed) duplicate the cards that are in the original object's vector within the vector of the object that is being constructed.

    If you declare (and define) that copy constructor (rather than using the one the compiler supplies for you by default), you will need to ask the compiler explicitly to synthesize a default constructor for that class template by declaring (but not defining) a default constructor followed by = default in the class template declaration in your header file.

    In your readme file, please document why you added your own copy constructor if that is what you did, or why you instead are using the one the compiler supplies (i.e., why that one is sufficient).

  9. Also add a static public member function to your CardSet class template, which returns a pointer to the class' protected vector data member. This static public member function provides a "controlled violation of encapsulation" that can be used to access, sort, etc. different combinations of cards within the card set (e.g., to identify melds in the Pinochle game and to rank and compare hands in the Hold 'Em poker game).
  10. Declare a public enum class PinochleMelds enumerated type in the header file where your PinochleGame class is declared, with monotonically increasing values for the following labels: dix, offsuitmarriage, fortyjacks, pinochle, insuitmarriage, sixtyqueens, eightykings, hundredaces, insuitrun, doublepinochle, fourhundredjacks, sixhundredqueens, eighthundredkings, thousandaces, and insuitdoublerun.

    Also declare and initialize a public static member variable of the PinochleGame class, which associates the following point values with each of the labels in the PinochleMelds enumerated type: dix is worth 10 points, offsuitmarriage is worth 20 points, fortyjacks is worth 40 points, pinochle is worth 40 points, insuitmarriage is worth 40 points, sixtyqueens is worth 60 points, eightykings is worth 80 points, hundredaces is worth 100 points, insuitrun is worth 150 points, doublepinochle is worth 300 points, fourhundredjacks is worth 400 points, sixhundredqueens is worth 600 points, eighthundredkings is worth 800 points, thousandaces is worth 1000 points, and insuitdoublerun is worth 1500 points.

    One easy way to do this is to exploit the numbering of enum class labels starting at 0 and use them as indices into an array of unsigned integers that has as many positions as there are labels, and initialize the array with the appropriate point values.

  11. Declare and define a left shift operator (operator<<) that takes a reference to an ostream and a const reference to a variable of the PinochleMelds enumerated type, which prints out an appropriate string for the name of the label and the points value associated with the label as an unsigned decimal integer (with a space in between them), and then returns a reference to the ostream that was passed to it.
  12. Declare and define a private suit_independent_evaluation member function in your PinochleGame class, which takes a const reference to a CardSet parameterized with the PinochleRank and Suit enumerated types (for that player's hand), and a non-const reference to a vector parameterized with the PinochleMelds enumerated type.

    The member function should declare a local variable of type CardSet parameterized with the PinochleRank and Suit enumerated types and initialize that local variable with the passed reference to the player's hand (i.e., invoking the copy constructor to make a deep copy that the function can manipulate as needed without affecting the original copy of the player's hand - in particular, using the rank and then suit ordering relation (and possibly the suit and then rank ordering relation) to sort the cards in the local copy may help to simplify the logic for the following evaluations.

    The member function should check for each of the following combinations of cards in the hand, and for each one that is found (with the exception of the ones that don't count if another higher valued version exists) push back the appropriate enumeration value into the vector to which a reference was passed (note that doing them in the following order may make it easier to write the conditional logic for skipping ones that don't count if a higher valued version exists):

  13. In the play member function of your PinochleGame class, after it prints out each player's name and hand, it should also declare an (empty) vector of the PinochleMelds enumerated type and pass a reference to the player's hand and a reference to that vector into the suit_independent_evaluation member function to identify all the melds that are possible with the player's hand. The play member function should then iterate through the vector and (inserting appropriate line breaks when needed) use the shift operator to print out the label and value of each meld that was found. Note that it is essential that melds found in a previous hand should not be carried over into the next one - the vector must be empty before each call to the evaluation function.
  14. Declare an enum class HoldEmHandRank enumerated type in the header file where your HoldEmGame class is declared, with monotonically increasing values for the labels xhigh, pair, twopair, threeofakind, straight, flush, fullhouse, fourofakind, straightflush, and undefined.

    Also declare and define a left shift operator (operator<<) that takes a reference to an ostream and a const reference to a variable of the enumerated type, shifts an appropriate string for the label into the passed ostream and returns a reference to the ostream.

  15. Declare and define a private holdem_hand_evaluation member function in your HoldEmGame class, which takes a const reference to a CardSet parameterized with the HoldEmRank and Suit enumerated types (for that player's hand) and a return value of the HoldEmHandRank enumerated type.

    The member function should again declare a local CardSet variable (though this time parameterized with the HoldEmRank and Suit enumerated types) and should again initialize it with the passed reference to the player's hand to make a deep copy - here too using the rank and then suit ordering relation to sort the cards in the hand may simplify the logic for the following evaluations.

    The member function should then check for each of the following combinations of cards in the hand, and return the highest valued one that it finds (for example, a straight flush is also a straight and is also a flush but its ranking is higher than either of the other two):

  16. Add a nested struct to your HoldEmGame class which has (1) a CardSet that is parameterized with the HoldEmRank and Suit enumerated types, (2) a variable for the player's name, (3) a variable of the HoldEmHandRank enumerated type, and (4) a constructor that takes arguments of those types and uses them to initialize its member variables (in its base/member initialization list).

    The instructions for this nested struct originally had specified that the player name variable in it should be a reference - however, depending on how your code uses those structs it is possible that it may result in one of them being default constructed which will cause a compilation error, in which case it's necessary to declare the variable to be an lvalue instead of an lvalue reference.

    In the source and header files for your HoldEmGame class, declare and define a non-member less than operator (operator<) that takes const references to two objects of the nested struct type and returns true if (and only if) the value of the first object's HoldEmHandRank variable is less than that of the second object's, or any of the following are true:

  17. In the play member function of your HoldEmGame class, after the first three cards are dealt to the common board of cards the players share (the flop) declare a vector of the nested struct type, and push back an instance of that struct with each player's hand and name and the undefined value.

    The play method should then iterate through that vector, and (1) add the three cards from the common board of cards to the two cards (from the player's hand) in each element's CardSet and then (2) assign the element's HoldEmHandRank member variable the result of calling the holdem_hand_evaluation member function on the element's CardSet member variable.

    The play method should then sort the vector and print out (from the highest ranked to the lowest) each player's name, card set (which includes the player's cards and the cards from the common board of cards), and hand rank at that point in the game.

  18. In your lab directory, compile your code using the CMakeLists.txt, and fix any errors or warnings that occur. Please be sure to note the different kinds of errors or warnings you ran into (though you don't have to list every instance of each kind) in your readme file. If you were fortunate enough not to run into any errors or warnings, please note that instead, where you would have listed errors or warnings in your readme file.

  19. Run the executable program repeatedly to test (and confirm) that for any incorrectly formed command line (due to the name of the game or due to the number of players listed following the game's name) the program outputs a helpful error message and returns an appropriate non-zero value to indicate that kind of failure. Then run the executable program repeatedly to confirm that: (1) the appropriate suit independent melds are identified for each hand in the Pinochle game, (2) the correct combination of cards is produced and appropriately ranked for each player in Hold 'Em (for example after the flop all players will share the same 3 cards from the common board in addition to their own two unique cards; and (3) the list of ranked hands (and the players that have them) that is printed after the flop is ordered correctly.
  20. In your readme file, make sure that your names, e-mail addresses, and the lab number (lab 2) are at the top of the file, and that you've documented whether you ran into any errors or warnings while developing your solution (and if so what they were) and what the executable program did for each of the trials you ran. Be sure to save your changes to your readme file and your CMakeLists.txt, as well as those to your source and header files, before uploading your solution.
  21. Upload to the lab 2 assignment page for CSE 4208 in Canvas a single zip file that contains: (1) your readme file (2) the CMakeLists.txt you used to compile your solution; and (3) all your header and source files for your solution (including any files that you may have copied over unmodified from your previous lab assignment).