The total number of poker hands (i.e. five cards for each hand) from a 52 deck is computed by using the choose method nPk (i.e. nPk = n!/(n-k)!*k!) which is 2,598,960 possible hands. What I would like to do is to generate all possible cases(i.e. 2,598,960 hands). The only way I think of is to randomly generate a hand and add it to a vector. Every time I add a hand, I check it if it already exists if so, keep generate random hand. This will of course work but I'm looking for faster and elegant approach for this. The order of cards in a hand is not important. This is a minimal working example to save your time.
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
const int SUIT_MAX(4);
const int RANK_MAX(13);
const int HAND_SZ(5);
const std::string SUIT[SUIT_MAX] = {"S", "H", "D", "C"};
const std::string RANK[RANK_MAX] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
class Card
{
public:
Card(const string &suit, const string &rank) { m_card = rank + suit; }
std::string print() const { return m_card; }
private:
string m_card;
};
class Hand
{
public:
Hand(const vector<Card>& cards)
{
m_hand.push_back(cards);
}
void printHand()
{
for (int i(0); i < m_hand.size(); ++i)
for (int j(0); j < m_hand[i].size(); ++j)
cout << m_hand[i][j].print() << " ";
cout << endl;
}
private:
vector<vector<Card>> m_hand;
};
int main()
{
srand((int) time(0));
Card c1(SUIT[0], RANK[3]);
vector<Card> handVec;
for(int i(0); i < HAND_SZ; ++i){
int suit = (rand() % (SUIT_MAX));
int rank = (rand() % (RANK_MAX));
Card c(SUIT[suit], RANK[rank]);
handVec.push_back(c);
}
Hand h1(handVec);
h1.printHand();
return 0;
}