Your class could have the following member functions and member variables. However, it's up to you to design the class however you like. The following is a suggestion, you can add more member variables and functions or remove any as you like, except shuffle() and printDeck(): (Note: operators must be implemented)
bool empty(); //returns true if deck has no cards
int cardIndex; //marks the index of the next card in the deck
Card deck[52];// this is your final deck of cards
const int DECK_SIZE = 52;// this is the size of your deck
void shuffle(); // puts cards in random order
friend ostream& operator<<(ostream& out, const Deck& deck); // prints all the cards to the console
Card dealCard(); // returns the next card in the deck
Card getCardAt(int index);// gets the Card at the given index
Deck(); // this is the constructor
Think about which member functions and member variables should be public and private.
Notice your Deck class has an array of size 52. This is because a classic that has 4 suits each containing 13 rankings.
Card Class
Notice in the Deck class, your deck array is of data type Card. You must define this class as well. A card consists of two entities, a suit and a value. There are four suits, (hearts, diamonds, clubs, and spades), and 13 values (numbers 2-10, A for ace, K for king, Q for queen and J for jack. Your Card class should look something like below, remember this is just a suggestion and you may add or omit to the list of member variables and function as you see suitable (Hint, you may want two string arrays to represent the suits and the values). Again, I will leave it up to you to determine the access for each member variable and member function. (Note: operators must be implemented)
string suit;
string value;
string getSuit();
string getValue();
void setSuit(string suit);
void setValue(string value);
friend ostream& operator<<(ostream& out, const Card& card);
Card(string suit, string value);
Card(int suit, int string);
Card();
Testing your Deck and Card Classes
To test your Deck and Card classes, write a program that will print out cards in random order).
Your program output should look like the following:
1: Jack of Diamonds
2: Ace of Spades
3: Six of Spades
4: Nine of Diamonds
5: Ten of Spades
6: Four of Spades
7: Four of Clubs
8: Jack of Clubs
9: Three of Spades
10: Seven of Clubs
11: Queen of Diamonds
12: Eight of Clubs
13: Ace of Clubs
14: Eight of Hearts
15: Seven of Hearts
16: Queen of Hearts
17: Three of Clubs
18: Eight of Spades
19: Four of Hearts
20: Eight of Diamonds
21: Queen of Spades
22: Five of Clubs
23: Ten of Clubs
24: Three of Diamonds
25: Queen of Clubs
26: Five of Hearts
27: Jack of Spades
28: Six of Diamonds
29: Five of Diamonds
30: Jack of Hearts
31: Two of Spades
32: Six of Hearts
33: Four of Diamonds
34: Ace of Diamonds
35: Ace of Hearts
36: King of Spades
37: King of Clubs
38: Two of Diamonds
39: Nine of Hearts
40: Seven of Spades
41: Nine of Clubs
42: Five of Spades
43: Two of Clubs
44: Ten of Hearts
45: Three of Hearts
46: Ten of Diamonds
47: Seven of Diamonds
48: Two of Hearts
49: King of Hearts
50: Nine of Spades
51: Six of Clubs
52: King of Diamonds
CardHand Class
Next, create a class called CardHand. Your class should have the following:
a vector to store all the cards in a player’s hand.
Remember in a card game, a player is usually dealt one card at a time, so make sure your CardHand class has the ability to take in one card at a time.
Accessors and Mutators where needed
Just like in the other classes, I will leave it up to you to determine access for member variable and member function.
CardHandScorer Class
Next, create another class, CardHandScorer that is used to analyze a players hand and determine a score for that hand. Because we are going to create a Poker game we will focus on making a scorer for Poker only, though in the future, we will add function for other card games such as Blackjack. The CardHandScorer class should have the following:
a static function that takes a CardHand object as an argument. The purpose of this static function is to return the score for the Poker hand. This score will be stored in a PokerScore object that is mention in the next section.
The function should analyze the CardHand and add all possible scores to the PokerScore object in which it will then return this object.
PokerScore Class
Next, create a PokerScore class. In the game of Poker, a player’s hand can have multiple scores. For example, if a player has four-of-a-kind, they also have, three of a kind, two pairs, one pair, or a high card . See the list of the different poker hand scores below. Your PokerScore class should be designed to hold all the possible scores a particular CardHand has. To do this, create an enumerator of Scores that holds all the possible poker scores. Then, create a vector that can hold the data type of your enumerator, Scores. Your CardHandScorer should have the ability to add scores to a PokerScore object. Here is the declaration of the PokerScore class: (Note: operators must be implemented)
enum Scores{
ROYAL_FLUSH, //A, K, Q, J, 10, all the same suit.
STRAIGHT_FLUSH, //five cards of the same suit and consecutive ranking
FOUR_OF_A_KIND, //four cards of the same ranking
FULL_HOUSE, //three cards of the same rank along with two cards of the same rank
FLUSH, //five cards of the same suit
STRAIGHT, //five cards in consecutive ranking
THREE_OK_A_KIND, //three cards of the same rank
TWO_PAIR, //two cards of the same rank along with another two cards of the same rank
ONE_PAIR, //two cards of the same rank
HIGH_CARD //highest card in the player’s hand
};
vector<Scores> scores;
void operator+=(const Scores& score);
friend bool operator==(const PokerScore& p, Scores score);
int size();
Score& operator[](unsigned int index);
PokerScore();
Here’s an example of usage of the Deck, Card, CardHand, CardHandScorer, and PokerScore classes:
int main() {
Deck deck;
CardHand ch;
//deal five cards to the card hand
for (int i = 0; i < 5; i++)
ch.addCard(deck.dealCard());
//analyze card hand and get poker scores
PokerScore pokerScore = CardHandScorer::scorePokerHand(ch);
//add a score to pokerScore
pokerScore += PokerScore::FULL_HOUSE;
}
Analyzing the Poker Hands
After you have written and thoroughly tested your class, write a program to analyze the probability of each Poker score. Write a function that does the following:
Deal five cards to five different card hands.
Score one of card hands.
Return the scores for that card hand.
After running this function 1000 time, your program should output how many times each poker score was met. Here’s an example of your output:
ROYAL_FLUSH: 0
STRAIGHT_FLUSH: 10
FOUR_OF_A_KIND: 30
FULL_HOUSE: 47
FLUSH; 105
STRAIGHT; 146
THREE_OK_A_KIND; 234
TWO_PAIR: 759
ONE_PAIR:823
HIGH_CARD: 1000
Sample Solution