代写CS 20A: C++ Programming Homework 3: Poker Hands代做C/C++程序
- 首页 >> CSCS 20A: C++ Programming
Homework 3: Poker Hands
Due: Friday, 4/30/2024 @ 11:59PM
Goal
In this homework, you will practice writing unit tests against the STD vector. You will then use a vector to generate a random hand of five cards and determine the poker hand.
Setup
• Be sure to follow the setup guide on Blackboard.
• You should read this writeup in its entirety before writing any code. There is more direction in this write up than there will be for later assignments.
• This assignment has an interactive mode and a testing mode. Running main runs in interactive
mode where all interaction happens through COUT and CIN. Running tests.exe uses a text file to take in input and another text file to store some of the output. See the sample output at the end to see which output goes to which stream.
• For this homework, you will be editing Cards.cpp and studentTests.cpp only.
• All functions you’re to complete are currently stubs – they have just enough code for the
program to compile and run – though the results aren’t correct. Several places there are TODO comments – this is where we recommend you fill in your code.
• For this homework, do not modify the function names, return types, or parameters of any of the functions. We will be testing your code using automated testing programs, so changing these
will result in an automatic deduction.
Part 1: Poker hands
• Now that you have a working vector class, let’s use it to draw poker hands!
• If you are unfamiliar on poker hands, consult the Wikipediaarticleon the topic.
• If you take a look at Cards.h/cpp, you will see that some of the code has already been provided to you, including a Suit enum and a Card struct (that has both rank and suit for the card).
• Of the provided implementations, the most important is createDeck. This function will take an empty std::vector of Cards, fill it with a standard 52 card deck, and then shuffle the deck. The code is already in the skeleton, but to shuffle the deck, you’ll need to end the function with the following 2 lines of code …
std::sort(&deck[0], &deck[0] + deck.size());
std::shuffle(&deck[0], &deck[0] + deck.size(), g);
• You will need to implement the following member functions of PokerHand (in Cards.cpp):
1. The PokerHand constructor. This will take in a deck of cards, by reference, draw 5 cards and store them in themHand member variable. To do this, you will use back, push_back, and pop_back. The constructor also needs to sort the hand – so use your sortHand
function (that should do selectionsorton the hand).
2. hasStraight – Returns true if the hand has a straight. Since they are sorted, you can
check simply by making sure that the rank at index 0 is equal to one less than the rank at index 1, the rank at index 1 is one less than the rank at index 2, and soon.
3. hasFlush – Returns true if the hand has a flush. To check if it’sa flush, make sure the suit of every card is identical.
4. hasFourOfAKind – Returns true if the hand has a four of a kind. Since it’s sorted, there are only two possibilities: either the first four cards have the same rank, or the last four cards have the same rank
5. hasFullHouse – Returns true if the hand has a full house. As with four of a kind, there are only two possibilities: xxx yy or xxy yy
6. hasThreeOfAKind – Returns true if the hand has a three of a kind. There are three possibilities: xx xyz or x yy yz or xyz zz
7. hasTwoPairs – Returns true if the hand has two pairs. There are three possibilities: xxy yz or x yy zz or xxy zz
8. hasPair – Returns true if the hand has a pair. There’sa pair if the rank of any card is equal to the rank of its neighbor
9. getBestPokerHand – This returns a string describing which poker hand you have . Start by checking for the best hand (straight flush) using the appropriate member functions and continuedown from there
• Hint: You can check your code for the better hands by creating a “stacked” deck of only the cards you want to draw.
Part 2: Tests
• For this assignment, your tests aren’t graded. For the next project, you’ll be filling in the tests. Based on their thoroughness, these tests will receive a score of 0, 1, or 2 .
• You can practice writing tests now in studentTests for the Card struct, PokerHand class, and smcVector.
• For each of the Card tests, you should test several cards to make sure the struct works correctly. For each PokerHand test, testing at least 1 hand is required.
• For the PokerHand tests, you can try creating a vector with 5 specific cards on the top to pass into the PokerHand constructor to test different hand combinations.
• For the vector tests, try making a smcVector of integers and make sure the vector behaves as expected.
Sample Output
Below is sample output for a full run-through of the interactive version of the program. Your output
should resemble the following (user input is in red) – based on the random number seed, your output may vary in card hands …
You drew: { 4 of Spades, 5 of Diamonds, 6 of Hearts, 8 of Spades, King of Spades } You have a high card
Try again (y/n): y
You drew: { 3 of Clubs, 3 of Hearts, 6 of Diamonds, 6 of Hearts, 9 of Diamonds } You have a two pairs
Try again (y/n): y
You drew: { 6 of Diamonds, 6 of Hearts, Jack of Spades, Ace of Diamonds, Ace of Spades } You have a two pairs
Try again (y/n): y
You drew: { 2 of Clubs, 2 of Spades, 6 of Hearts, 10 of Clubs, Ace of Diamonds } You have a pair
Try again (y/n): y
You drew: { 9 of Clubs, 10 of Hearts, Ace of Clubs, Ace of Diamonds, Ace of Spades } You have a three of a kind
Try again (y/n): y
You drew: { 2 of Hearts, 4 of Diamonds, 7 of Diamonds, Jack of Clubs, Ace of Hearts } You have a high card
Try again (y/n): y
You drew: { 2 of Clubs, 2 of Spades, 8 of Diamonds, 8 of Spades, King of Hearts } You have a two pairs
Try again (y/n): y
You drew: { 5 of Clubs, 6 of Spades, 7 of Diamonds, 7 of Spades, Queen of Diamonds } You have a pair
Try again (y/n):n
Submission
You must push your code to the GitHub assignment to submit your solution.
Grading
We will be using a set of tests only available to GitHub. You do not have access to our testing file. For questions about individual tests, please post on Piazza. The point breakdowns are …
Item Points
Card (based on graded tests) |
12 |
Poker hand (based on graded tests) |
32 |
End-to-end graded tests (based on graded tests) |
6 |
Total 50