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.
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.
readme.txt), in which you will record your observations, design decisions,
and other information pertaining to the lab assignment as you develop your solution.
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).
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.
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
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;...
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.
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.
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.
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.
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.
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:
red, one blue, one green, and one yellow card of rank zero;
red, two blue, two green, and two yellow cards of each rank one through drawtwo; and
black cards of each rank drawfour through blank.
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.
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>)
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.
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.
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.
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.
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.
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.
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.
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.
CMakeLists.txt,
as well as those to your source and header files, before uploading your solution.
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).