Hey everyone, I currently am creating a Card shuffling program in C++, Ive taken care of the shuffling of the deck. Now I just need to distribute a hand of 5 cards, and determine hands. Can anyone help? Please!

cardheader.h ( includes both Class Card & Deck )

``````#ifndef CARDHEADER_H

#include <vector>

enum Face{ Ace , Two , Three , Four , Five , Six , Seven , Eight , Nine, Ten , Jack , Queen , King };
enum Suit{ Diamonds , Hearts , Clubs , Spades };

class Card{

public:
Card( Face, Suit );
void get_cardvalues( Face & , Suit & );
private:
Face myFace;
Suit mySuit;

};

class Deck{

public:
Deck( void );
~Deck();
void shuffle_cards( bool swap = false );
void print_tostring();
private:
std::vector<Card> deck;

};

#endif``````

cardndeck.cc ( includes card & deck implementations )

``````#include <algorithm>
#include <iostream>
#include <ctime>

using namespace std;

Card::Card( Face face, Suit suit ){

myFace = face;
mySuit = suit;

}

void Card::get_cardvalues( Face &face, Suit &suit ){

face = myFace;
suit = mySuit;

}

Deck::Deck(){

int counter = 0;
int i;
int j;

for( j = int( Ace ); j <= int( King ); j++ ){
for( i = int( Diamonds ); i <= int( Spades ); i++ ){
deck.push_back( Card( Face( i ) , Suit( j ) ) );
counter++;
}
}
}

Deck::~Deck(){

deck.clear();

}

void Deck::print_tostring(){

int i;

static const char *faces[] = { "Diamonds" , "Hearts" , "Clubs" , "Spades" };
static const char *suits[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

for( i = 0; i < ( int )deck.size(); i++ ){
Face f_temp;
Suit s_temp;
deck[ i ].get_cardvalues( f_temp , s_temp );
cout << "[" << suits[ s_temp ] << " of " << faces[ f_temp ] << "]";
cout << endl;
}
cout << endl;

}

void Deck::shuffle_cards( bool swap ){

static bool swapper;

if( !swapper && swap ){
srand( unsigned( time( NULL )));
swapper = true;
}

random_shuffle( deck.begin() , deck.end() );
}``````

## All 2 Replies

My suggestions:

You stated that you need to distribute hands to each player. I would recommend removing cards from deck() and pushing them into arrays assigned to each player; each array containing 5 Card objects.

In order to determine what hand someone has, I would recommend making a function for every possible winning hand available in poker. Each function could be prototyped to return a boolean value; true if their hand qualified as a winner for that particular winning scenario and false if not. It is possible that a hand could qualifiy for several winning scenarios, i.e. a full house will also return true for 2 of a kind, and 3 of a kind. To account for this, I would progress through the functions in descending order (royal flush to 1pair).

Special case scenrios: if all players return false for all winning functions, then you will have to obtain a 'high card' from each hand in order to determine a winner. Furthermore, you may have use a 'high card' in order to break a tie between two winning hands (i.e. flush vs. flush). Lastly, you will have to handle the rare case of a tie, in which case the pot is split.

``````int winner(Card player_hand[5])
{
//Exctract and save 'high card'
//you can handle this here, or in another dedicated funtion for obtaining high card
::high_card = get_high_card(player_hand[]);

//Make winning hand determinations:
if(royal_flush(player_hand))
{
return 9;
}
else if(striaght_flush(player_hand))
{
return 8;
}
else if(4_kind(player_hand))
{
return 7;
}
else if(full_house(player_hand))
{
return 6;
}

...
....
......
etc. etc. etc.``````

Special cases aside, the player with the highest return value from winner() wins.

Again, this is all just a suggestion... do what you wanna do. If somebody knows a better way let me know.

Thanks for the post! that's a good start for the hand determinations! Much appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.