CSE 4208 Lab 3: A New Game

Due by Sunday December 7, 2025, at 11:59 pm
Final grade percentage: 20 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 (1) implement the deck used for Uno, and (2) create a new game of Go Fish that can use either that Uno deck or the HoldEm deck or the Pinochle deck from the previous lab assignment. To do this, you will add new features to your CardSet class template to iterate through and manipulate the cards without directly exposing the underlying container. You will use this when moving cards between different player's hands during your implementation of the game Go Fish.

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 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.3.0) 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 lab2.cpp becomes lab3.cpp as a starting point for this assignment).
  7. In your CardSet declaration and definition, replace the accessor that returns a pointer to the class template's protected vector data member with two different non-static accessor methods, each returning an iterator positioned at the vector’s begin() or end() respectively.

    These iterators can be used to view the contents of the vector, and can be passed to sorting and searching functions that may use them, e.g., to identify hand types in different games; you also may need to update various places in your code to use those iterators instead of the pointer to class template's protected vector member.

  8. In your CardSet implementation add a method called sort() if you didn’t do so already in the previous lab, or revise it as needed if you did, so that it calls the std::sort function from the C++ standard library.

    Refactor play() in both PinochleGame and HoldEmGame to use the iterators (per the previous step) and the sort wrapper method. Do the same for holdem_hand_evaluation and suit_independent_evaluation
  9. At this point, you should build and run your code and verify it produces the same results as in lab2.

  10. Add type specifiers for the card type, suit type, rank type, and iterator type in the public portion of your declaration of the CardSet class template, e.g.:

    typedef Card<S, R> card_type;
    typedef typename vector<Card<S, R>>::iterator iter;
    ...
  11. Move the collect method from the Deck class template up into the CardSet class template, and rewrite it to use the std::move function from the STL algorithms library. You should consider using a std::back_inserter, and keep in mind that the container you are moving from will be non-empty after the move function and thus eventually will need to be cleared manually.
  12. Add a new method to the CardSet class template, called collect_if that takes a CardSet called deck and a predicate of type std::function<bool(Card<S, R>&)>. This will only collect cards that meet a certain criteria, and will be used later to move 4-of-a-kinds in the Go Fish game.

    First, use the std::copy_if function to copy any cards that match the predicate from the passed CardSet to the one on which collect_if was called.

    Then use the std::remove_if function to remove those cards (which we just copied) from CardSet on which collect_if was called. Note that remove_if doesn’t actually shrink the vector, it only shifts unremoved values over, and leaves undefined regions.

    eg [0 0 3 4 5 7 7 7 7 8 9] -> (remove 7s) -> [0 0 3 4 5 8 9 ? ? ? ?]

    Note that remove_if returns an iterator to the start of this undefined range. Pass that iterator and the vector's end() iterator into a call to the vector's erase method, to remove the undefined entries and redefine the end of the vector.
  13. Add a public method called request to the CardSet class template, which takes a reference to another CardSet and a rank and returns a boolean value.

    The method should use the std::find_if function to find a card in the provided CardSet matching the provided rank.

    If the card couldn’t be found, the method should simply return false. Otherwise, the method should move the card from the CardSet that was passed in, into the CardSet on which the method was called, and then should return true.

    At this point, you should again build and run your code and verify it produces the same results as in lab2.

  14. Create a new C++ header and source file in which you will declare and define types related to an Uno deck.

    Uno doesn’t have suits, but it does have colors. In the header file, but outside of the class template declaration, declare a Color enumeration (which we will use in place of suits) with the labels red, blue, green, yellow, black, and undefined.

  15. In the same header file, declare a UnoRank enumeration with the labels zero, one, two, three, four, five, six, seven, eight, nine, skip, reverse, drawtwo, drawfour, wild, blank, and undefined.

    In the header and source file, declare and define an operator<< for UnoRank that prints the rank to a provided ostream, an operator++ that increments the rank from one to the next, and declare and define similar operators for Color.
  16. In that header and source file, declare and define a class called UnoDeck, that is derived from Deck parameterized by Color and UnoRank.

    The constructor should take no arguments and should populate the inherited vector of cards as follows, based on the official rules for the standard contents of an Uno deck, for a total of 112 cards in the deck:
  17. Create a new C++ source and header file and in them declare and define a class template called GoFishGame, with 3 type parameters: the suit type, the rank type, and a deck type. These type parameters should default to Suit, HoldEmRank, and HoldEmDeck (Go Fish can be played with many types of decks).

    Public members should include a constructor, a virtual destructor, and a virtual implementation of play(). The constructor should take 2 arguments: the number of players, and an array of C-style strings listing their names.

    Protected members should include the deck of cards (with a type matching the one provided by the class template's deck type parameter), a vector of CardSet for the players' hands, a vector of CardSet for the books each of the players has made, and a virtual deal() method.

    Go Fish works with any deck that has at least 4 of each type of card. Before we use an arbitrary deck, we must check that it is valid. The body of the constructor should check that there are at least 4 cards of each rank in the deck, and if not it should throw an exception.

    Consider using the std::count_if function to check which ranks have at least 4 cards.

    You may need to use template specialization to provide appropriate definitions of the constructor for the different possible suit type, rank type, and deck type combinations.
  18. In the GoFishGame class template header, add a static_assert to check whether the Deck parameter makes use of the provided suit and rank types. Using static_assert and std::is_base_of, check that Deck is derived from a CardSet parametrized by the provided rank and suit (eg CardSet<S, R>)
  19. Add a collect_books method to the GoFishGame class template that takes a player number, checks if there is a 4-of-a-kind in that player's hand, and returns a boolean value to indicate whether or not 4 cards of the same rank were found in it.

    The collect_books method should iterate over the CardSet that represents the player's hand to see if there is a rank for which there are 4 cards. If there is none, do nothing and return false.

    If there is a 4-of-a-kind, the collect_books method should call the collect_if function you defined in the CardSet class template, to remove all cards of the appropriate rank from the player's hand and put them into the CardSet for the books that player has made.

    This function requires a predicate. Create a function object of type <bool(Card<S, R>&)> called predicate and initialize it to a lambda function that compares the rank of the passed card to the rank we’re trying to remove. Pass this predicate into your collect_if call, along with the current hand.

    After moving the 4-of-a-kind from the player's hand into their collection of books, return true.
  20. In the GoFishGame class template, declare and define a protected turn method that implements how each player will take their turn. It should take a single argument for the player number, and should return a boolean value to indicate whether it still remains that player's turn.

    The method should use std::cout and std::cin to print out the players hands and books and then prompt the user to indicate what card rank to ask for and what player to ask for it from, doing so repeatedly until the user provides a valid card rank (which must be the rank of a card in the player's hand) and player number (which must be the number of a player in the game).

    Please make sure that the screen output showing the players' hands and books is nicely compact, so that it is easy to see all players holdings at once within the terminal window.

    The turn method should then use the request method to try to obtain a card (or cards) of that rank from the targeted player, and if successful (at least one card obtained) should (1) repeatedly call the collect_books method for the player whose turn it is until the collect_books returns false; and then (2) the turn method should return true.

    If the method is unsuccessful, it should "go fish" by drawing a card from the deck into the hand of the player whose turn it is. If the deck is empty, the player whose turn it is should have all their cards moved into the deck and should be removed from the game (a message to that effect should be printed to std::cout and some indication should be made in the Go Fish game class template so the player takes no more turns) and the turn method should then return false;

    otherwise (if the player was able to draw a card from the deck) the turn method should return true if the card that was drawn is of the rank that the player had asked the targeted player for, or false if it was not.
  21. Implement the deal function of the GoFishGame. Shuffle the deck and deal each player 1 card at a time until each player has received the proper number of cards. If there are only two players in the game, each player should receive seven cards, or if there are more than two players in the game, each player should receive five cards.
  22. Implement the play function in GoFishGame. Start by calling deal and then for each player number repeatedly call the collect_books method with that player number until it returns false. Then, repeatedly iterate over all players that remain in the game, in multiple rounds until the game ends. In each round, for each player in succession, repeatedly call the turn method for the (same) current player until it returns false and then advance to the next player.

    At the end of each round, print out how many books each player has made, the round number, and check for termination conditions. The game is over when all cards have moved from the deck and the players' hands into the players' collections of books, or when there is one or zero players left in the game. Once the game has finished, it should say which player (or players) has made the most books.
  23. In the source file for your main function, modify the create function so that if "GoFish" is followed by the appropriate type of deck to use (which must be either "HoldEm" or "Pinochle" or "Uno") which is followed by between two and five player names, it uses make_shared parameterized with GoFishGame (which is itself parameterized with the appropriate suit type, rank type, and deck type according to the appropriate combination of those based on the command line argument that indicates the deck type) to initialize the shared pointer variable that the create function will return. Because the Go Fish game constructor may throw an exception your create function should surround that logic with a try catch block so that any exceptions are handled within the create function itself.
  24. Modify the progam's main function so that it accepts "GoFish" followed by the type of deck to use (which must be either "HoldEm" or "Pinochle" or "Uno") followed by between two and five player names as a well formed command line, and if so calls the create function accordingly, and if the shared pointer that was returned points to anything returns the result of using it to call the Game object's play member function.
  25. In your lab directory, run the cmake -B build && cmake --build build command to 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.

  26. 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 (in addition to the Hold 'Em and Pinochle games working correctly per the lab 2 specifications), the Go Fish game works correctly for each of the three different deck types: (1) the correct number of cards is dealt to the correct number of players, (2) game play works correctly in terms of turns and alternation among players, (3) scoring and keeping track of books works correctly, and (4) when players are out of the game and when the game itself ends is also happening correctly. Please make sure to document which trials you ran and what happened in them, in your readme file.
  27. In your readme file, make sure that your names, e-mail addresses, and the lab number (lab 3) 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.
  28. Upload to the lab 3 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).