For the PokerHand class, there are some complications. First, it is much easier
ID: 3676157 • Letter: F
Question
For the PokerHand class, there are some complications. First, it is much easier to determine the rank of a poker hand if the cards are sorted by points. For example, if you want to check if a hand is a Two Pair, there are only 3 possible patterns when the cards are sorted. A Two Pair hand must have a 2-2-1, 2-1-2 or 1-2-2 pattern. The 2-2-1 pattern means the first two cards are a pair with the same point value. The next two cards are a pair with a different (higher) point value. Finally, the last card is by itself and has a point value higher than the second pair. Because the cards are sorted, there are no other patterns possible to make a Two Pair. Since sorting is so useful, it should be done by the constructors. That way, all member functions can assume that the hand is already sorted.
Note that poker ranks are mutually exclusive. A Full House is not a Two Pair, even though there are two pairs in a Full House. Similarly, a Straight Flush is not a Straight and a Royal Flush is not a Straight Flush. Thus, if two poker hands have different ranks, we can quickly determine which hand is better. Your implementation should store the rank of the hand in the data member m_rank after it has been computed for the first time. This way you can avoid having to recompute the rank when the client calls getRank(). Before the rank has been computed, m_rank should hold the value NoRank. That is how you can tell if the rank has been computed previously. The constructors must make sure that m_rank is initialized to NoRank.
If two PokerHand objects have the same rank, your cmp() function should still determine which hand is better according to the rules of poker. A tie is possible, but this is not determined just by the rank. For example, if two hands are both Two Pair, the hand whose higher pair has higher point value wins. If that's a tie, then the hand whose lower pair has higher point value wins. If that's a tie, then the fifth card, the one that is not in any pair, is used to break the tie. If the fifth cards are once again tied,then the two hands are tied. Thus, to determine which of the Two Pair hands is better, we need three values: the point value of the higher pair, the point value of the lower pair and the point value of the last card. These three values can be determined easily when you first discovered that the rank of the hand is two pair. For example, when you check if the hand is a Two Pair with the 2-2-1 pattern, you can do the following:
// Check 2-2-1 if ( m_cards[0].points() == m_cards[1].points() && m_cards[1].points() != m_cards[2].points() && m_cards[2].points() == m_cards[3].points() && m_cards[3].points() != m_cards[4].points() ) { m_firstPairPoints = m_cards[3].points() ; m_secondPairPoints = m_cards[1].points() ; m_lastCardPoints = m_cards[4].points() ; m_rank = TwoPair ; return true ; }
The code below is what ive written so far to implement the pokerhand class for a game for 5 and 7 card poker. Im having trouble coding the member functions for pokerhand.cpp. The class functions for card.h and pokerhand.h are correct and do not require any additional editing. Im only having trouble with the member functions in pokerhand.cpp. Any help would be appreciated!
Pokerhand.h
#ifndef _POKERHAND_H_
#define _POKERHAND_H_
#include "card.h"
class PokerHand {
public:
// Avoiding magic numbers.
// Note: must use PokerHand::FIVE outside member functions.
//
static const int FIVE = 5 ;
// All possible ranks in poker in an enumeration type.
//
// A rank of NoRank indicates that the hand is not valid
// or that the hand has not been evaluated.
//
// Note: outside member functions must use refer to this type
// PokerHand::pRank and use constants PokerHand::RoyalFlush ...
//
typedef enum { NoRank, HighCard, OnePair, TwoPair, ThreeOfAKind,
Straight, Flush, FullHouse, FourOfAKind, StraightFlush,
RoyalFlush } pRank ;
// Default constructor.
//
// We allow a blank PokerHand to be created.
// However, the default constructor should make sure
// that m_valid is false and m_rank is NoRank.
//
PokerHand() ;
// Alternate constructor.
//
// The alternate constructor should store the FIVE cards
// given as parameters in the m_cards[] array.
// It should also immediately evaluate the hand, determine
// its rank and compute the additonal information (see below).
//
// Note: this constructor does not check for validity between
// cards. For example, the hand created might have two Queen
// of Hearts.
//
PokerHand(Card c0, Card c1, Card c2, Card c3, Card c4) ;
// Compare the host hand versus otherHand according
// to the rules of poker.
//
// Returns a negative number (< 0)
// if host hand loses to // the otherHand.
//
// Returns a positive number (> 0)
// if the host hand beats the otherHand.
//
// Returns zero if it's a tie.
//
// An invalid hand loses to any hand. If both hands
// are invalid, the result is undefined (could be
// negative, zero or positive).
//
// Note: the cmp() should not assume that the two hands
// come from the same deck of cards. E.g., both hands
// may have 4 Aces, the fifth card should be used to
// determine which hand wins.
//
int cmp(PokerHand &otherHand) ;
// Prints out the rank to standard output.
//
void printRank() ;
// Prints out the FIVE cards to standard output
// one card per line.
//
void printCards() ;
// Return the rank of this hand.
// If hand is not valid, should return NoRank.
// Should use rank stored in m_rank, if available.
//
pRank getRank() ;
// The following "isRank()" functions should all:
// 1. Assume that the hand is already sorted.
// 2. If true, store the rank in m_rank.
// 3. If true, store the "additional information"
// for that rank,
// 4. Use the rank if m_rank, if available.
// Returns true if the hand is a Royal Flush.
//
bool isRoyalFlush() ;
// Returns true if the hand is a Straight Flush.
//
bool isStraightFlush() ;
// Returns true if the hand is Four of a Kind.
// Sets m_lastCardPoints.
//
bool isFourOfAKind() ;
// Returns true if the hand is a Full House.
// Sets m_tripletPoints and m_firstPairPoints.
//
bool isFullHouse() ;
// Returns true if the hand is a Flush.
//
bool isFlush() ;
// Returns true if the hand is a Straight.
//
bool isStraight() ;
// Returns true if the hand is Three of a Kind.
// Sets m_tripletPonts.
//
bool isThreeOfAKind() ;
// Returns true if the hand is a Two Pair.
// Sets m_firstPairPoints, m_secondPairPoints and
// m_lastCardPoints.
//
bool isTwoPair() ;
// Returns true if the hand is a One Pair.
// Sets m_firstPairPoints.
//
bool isOnePair() ;
// Returns true if the hand is High Card.
// (This means it isn't anything else.)
//
bool isHighCard() ;
private:
Card m_cards[FIVE] ; // holds the FIVE cards in this poker hand
bool m_valid ; // false if no cards assigned
pRank m_rank ; // stored rank
// The data members below hold some additional information
// for this hand that must be computed by evaluate().
// This information makes it possible to compare two
// hands when the ranks are equal. For example, if both
// hands are Two Pair, use m_firstPairPoints to see which
// hand has higher points for the top pair. If that's a tie,
// check m_secondPairPoints. If that is also a tie, then
// use m_lastCardPoints
// For Two Pair and Four of a Kind, the point value of the
// last card
//
Card::cPoints m_lastCardPoints ;
// For One Pair, the point value of the pair.
// For Two Pair, the point value of the higher pair.
// For Full House, the point value of the pair.
//
Card::cPoints m_firstPairPoints ;
// For Two Pair, the point value of the lower pair.
//
Card::cPoints m_secondPairPoints ;
// For Full House and Three of a Kind, the point value
// of the triple.
//
Card::cPoints m_tripletPoints ;
// For Four of a Kind, the point value of the four.
//
Card::cPoints m_quadrupletPoints ;
// Sort the hand by point value.
// The cards'suits are not considered in the sort.
//
void sort() ;
// Some helper functions
// These helper functions assume that the
// hand is already sorted.
// Returns true if the cards in the hand
// have the same suit. Note that this is
// not the same as isFlush(). This function
// returns true if the hand is a Flush or
// a Straight Flush or a Royal Flush.
// OTOH, isFlush() returns false if the
// hand is a Straight Flush.
//
bool isAllOneSuit() ;
// Returns true if the hand is five cards
// in an sequence (e.g., 7 8 9 10 Jack).
// Ace is always 14 and never 1.
// Note: this is not the same as isStraight()
// since isStraight() returns false when
// the hand is a Straight Flush.
//
bool isSequence() ;
// End of helper functions
// -----------------------------------------
// IF YOU WANT TO ADD MORE HELPER FUNCTIONS,
// PUT THEIR DECLARATIONS AFTER THIS LINE.
// -----------------------------------------
} ;
card.h
#ifndef _CARD_H_
#define _CARD_H_
class Card {
public:
// Type definition for the suits of a playing card.
// Invalid means the host Card has not been initialized.
//
// Note: outside Card member functions, use Card::cSuit,
// Card::Spades, Card::Hearts, ...
//
typedef enum { Invalid, Clubs, Diamonds, Hearts, Spades} cSuit ;
// Type definition for the point value of a playing card.
// A point value of 0 means the host Card has not been initialized.
// Jack = 11, Queen = 12, King = 13, Ace = 14.
// A point value of 1 is not used.
//
// Note: outside Card member functions, use Card::cPoints
//
typedef unsigned int cPoints ;
// Default constructor.
//
// We allow uninitialized Cards to be created.
// This allows arrays of Cards.
// Uninitialized cards should have Invalid for its suit
// and 0 for its points.
//
Card() ;
// Alternate constructor.
//
// Create a Card with specified suit and points.
// The alternate constructor should make sure that
// the values of the arguments are valid.
// If invalid arguments are given, a Card should
// created with Invalid for its suit and 0 for its points.
//
Card(cSuit s, cPoints p) ;
// Returns suit of the host Card.
//
cSuit suit() ;
// Returns point value of the host Card.
//
cPoints points() ;
// Prints a description of the host Card to standard output.
// The output should look like:
// 8 of Diamonds
// Queen of Hearts
// ...
//
void print() ;
private:
cSuit m_suit ; // suit of host Card
cPoints m_points ; // point value of host Card
} ;
#endif
Pokerhand.cpp
#include <iostream>
using namespace std ;
#include "pokerhand.h"
PokerHand::PokerHand () {
m_valid = false ;
m_rank = NoRank ;
m_lastCardPoints = 0 ;
m_firstPairPoints = 0 ;
m_secondPairPoints = 0 ;
m_tripletPoints = 0 ;
m_quadrupletPoints = 0 ;
}
PokerHand::PokerHand (Card c0, Card c1, Card c2, Card c3, Card c4) {
// Store cards
m_cards[0] = c0 ;
m_cards[1] = c1 ;
m_cards[2] = c2 ;
m_cards[3] = c3 ;
m_cards[4] = c4 ;
// We have cards.
// Note: this assumes c0, ..., c4 are valid
//
m_valid = true ;
m_rank = NoRank ;
// Default values for "additional information"
//
m_lastCardPoints = 0 ;
m_firstPairPoints = 0 ;
m_secondPairPoints = 0 ;
m_tripletPoints = 0 ;
m_quadrupletPoints = 0 ;
// Don't forget to sort
//
sort() ;
// IF YOU WANT THE ALTERNATE CONSTRUCTOR
// TO DO MORE, PLACE THE CODE AFTER THIS
// LINE:
// --------------------------------------
}
Explanation / Answer
Answer:
I recommend you see the following code and have an idea to improve yours, usually I do not like modify codes that are not mine because much time is wasted. I hope you serve in your project.
Simple and Correct.