Poker Hand Combinations!

Started by
5 comments, last by RegularKid 16 years, 6 months ago
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!
Advertisement
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.
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).
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".
Yes, you're right Simian Man. Order doesn't matter in my situation since:

( A 2 3 4 5 ) is the same as ( 4 3 A 5 2 ) in poker of course.
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 );            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]
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;<br>						combos[ numCombos ][ <span class="cpp-number">1</span> ] = hand[ j ];<br>						combos[ numCombos ][ <span class="cpp-number">2</span> ] = hand[ k ];<br>						combos[ numCombos ][ <span class="cpp-number">3</span> ] = hand[ m ];<br>						combos[ numCombos ][ <span class="cpp-number">4</span> ] = hand[ p ];<br>						numCombos++;<br>					}<br>				}<br>			}<br>		}<br>	}<br><br>	<span class="cpp-comment">// Print combos</span><br>	<span class="cpp-keyword">for</span>( <span class="cpp-keyword">int</span> i = <span class="cpp-number">0</span>; i &lt; numCombos; i++ )<br>	{<br>		printf( <span class="cpp-literal">"( %c, %c, %c, %c, %c )\n"</span>, combos[ <span class="cpp-number">0</span> ], combos[ <span class="cpp-number">1</span> ], combos[ <span class="cpp-number">2</span> ], combos[ <span class="cpp-number">3</span> ], combos[ <span class="cpp-number">4</span> ] );<br>	}<br><br>	<span class="cpp-comment">// Wait for input before closing</span><br>	getchar();<br><br>	<span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br>[\source]<br><br></pre></div><!–ENDSCRIPT–><br><br>Thanks for the responses! :)

This topic is closed to new replies.

Advertisement