Sign in to follow this  

Poker Hand Combinations!

This topic is 3727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys! I'm trying to figure out an algorithm to give me all unique combinations of 5 card hands given a set of 13 cards. I've already got a function to determine whether or not a hand is a valid poker hand, so what i'm trying to do is write a function that will give me just the set of unique 5 card combinations within my 13 card hand. Once that's done I'll simply use my function to decide which of those hands are valid poker hands or not. I did a quick search on google for "unique combinations algorithm", but what I found seemed a little complex ( or maybe I was just slightly confused by them ) for the simple algorithm I'm trying to create. I was hoping to find something that was pretty straight forward and didn't use recursion. I'm not really going for speed here :) Any ideas? Thanks!

Share this post


Link to post
Share on other sites
The proper term which describes the problem you need is "permutation".

But since this is a very common interview and assignment question, I'm not really sure if I'd like to suggest anything else.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
The proper term which describes the problem you need is "permutation".


Actually, unless I'm mistaken, a permutation is a specific ordering of a subset. He does not want to take order into account because the order of cards in your hand doesn't matter.

He's going for a combination (as he mentions in his post title).

Share this post


Link to post
Share on other sites
Ok, fair enough. It's for a card game I'm making. I'm out of school and already have a job .... so I'm not looking to cheat on an assignment or job interview :) But thank you for the correction in terms. Maybe I'll have a better time searching around on google using "permutations" rather than "combinations".

Share this post


Link to post
Share on other sites
The mini C++ program below is based on the C code from here.

It doesn't do exactly what you want (and does use recursion :)), but finds all possible combinations. Once you've got all of these you could sort and cheque for uniqueness, and then check for valid poker hands I guess.


#include <cstdlib>
#include <fstream>
#include <vector>

// The number of cards to go through to find hands.
unsigned const int INPUT_SIZE = 13;
// The size of our hand.
unsigned const int HAND_SIZE = 5;

// Our input cards.
std::vector<int> input;
// A vector to contain all the possible hands.
std::vector<std::vector<int> > output;

// The recursive function that finds the combinations.
void enumerate(std::vector<int> temp, int n_i) {
if (temp.size() == HAND_SIZE) {
output.push_back(temp);
} else {
for (unsigned int i = n_i; i < INPUT_SIZE; i++) {
temp.push_back( input[i] );
enumerate(temp, i+1);
temp.erase( --temp.end() );
}
}
}

int main(int argc, char *argv[]) {
std::ofstream outstream("output.txt");

// Add 13 random integers between 1 and 13 to the input vector.
// (and print them to our output file).
outstream << "Input:\n";
for (unsigned int i = 0; i != INPUT_SIZE; ++i) {
input.push_back( 1 + rand()%13);
outstream << input.back() << std::endl;
}
outstream << std::endl;

// Create a temp vector just to pass in.
std::vector<int> temp;
enumerate(temp, 0);

// Print the output.
outstream << "Outputting " << output.size() << " combinations" << ":" << std::endl;
for (std::vector<std::vector<int> >::iterator i = output.begin(); i != output.end(); ++i) {
outstream << 1 + i - output.begin() << ":";
for (std::vector<int>::iterator j = i->begin(); j != i->end(); ++j) {
outstream << "\t" << (*j);
}
outstream << std::endl;
}
outstream << "Done!" << std::endl;

return EXIT_SUCCESS;
}






[Edited by - sprite_hound on October 1, 2007 4:10:30 PM]

Share this post


Link to post
Share on other sites
Thanks for the response. I was able to figure out a brute force way that seems pretty straight forward to me :)


#include <stdio.h>

#define NUM_HAND_CARDS ( 13 )
#define NUM_COMBO_CARDS ( 5 )
#define MAX_COMBOS ( 10000 )

int main()
{
char hand[ NUM_HAND_CARDS ] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };
char combos[ MAX_COMBOS ][ NUM_COMBO_CARDS ];
int numCombos = 0;

// Find combos
for( int i = 0; i < NUM_HAND_CARDS; i++ )
{
for( int j = i + 1; j < NUM_HAND_CARDS; j++ )
{
for( int k = j + 1; k < NUM_HAND_CARDS; k++ )
{
for( int m = k + 1; m < NUM_HAND_CARDS; m++ )
{
for( int p = m + 1; p < NUM_HAND_CARDS; p++ )
{
// Check if each card is active first
combos[ numCombos ][ 0 ] = hand[ i ];
combos[ numCombos ][ 1 ] = hand[ j ];
combos[ numCombos ][ 2 ] = hand[ k ];
combos[ numCombos ][ 3 ] = hand[ m ];
combos[ numCombos ][ 4 ] = hand[ p ];
numCombos++;
}
}
}
}
}

// Print combos
for( int i = 0; i < numCombos; i++ )
{
printf( "( %c, %c, %c, %c, %c )\n", combos[ i ][ 0 ], combos[ i ][ 1 ], combos[ i ][ 2 ], combos[ i ][ 3 ], combos[ i ][ 4 ] );
}

// Wait for input before closing
getchar();

return 0;
}
[\source]



Thanks for the responses! :)

Share this post


Link to post
Share on other sites

This topic is 3727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this