• 12
• 12
• 9
• 10
• 13

do I need to sort a poker hand to eval it?

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

Recommended Posts

I have working on a text based poker game(trying to keep it to a single .cpp file for now) and have hit a snag. To effectively evaluate the hands(for flushed and straights) with the method I'm using, should I write a function (C++) to sort the hand, before evaluating higher ranking hands? code structure -only using arrays & pointers (to brush up on these topics)


// Would using this structure for the cards
// help or hurt my decision to sort?

char *face[13] = {"Ace","2","3","4","5","6","7","8",
"9","10","Jack","Queen","King"};

// this array is seeded with # 1-52 then "randomized"
//Yea I know, it's straight out of Deitel&Dietel 2nd ED

int deck[4][13] = {0};		//Holds 52 cards in a deck

char *player1[2][5] = {""};	//Holds a 5 card hand, [0][] for suit,
//[1][] for face values.

//further into the code(parts omitted)

//Looks for a pair(finds 1, 2, & 3 pair)
//3 of a kind shows as 3 sets of pairs

void test1Pair(char *wHand[][PLAYER_HAND])
{
int pairCount = 0;
char *pairFace[4] = {"blank"};

cout << endl << "Looking for a pair..." << endl;
for(int card1 = 0; card1 < PLAYER_HAND; card1++)
{
for(int card2 = (PLAYER_HAND - 1); card2 > card1; card2--)
{
if(wHand[DECK_FACE][card1] ==  wHand[DECK_FACE][card2])
{
pairCount += 1;
pairFace[pairCount-1] = wHand[DECK_FACE][card1];
}
}
}
if(pairCount > 0)
{
for (int i = pairCount; i > 0;i--)
{
if((pairFace[i-1] != NULL)   && pairFace[i-1] != pairFace[i-2])
cout << " Pair of " << pairFace[i-1] << "s! found." << endl;
}
}
else
{
cout << "\nThis hand does not contain a pair." << endl;
}
}

//this is the problem right now I'm not sure I've got a good handle on
// setting up the pointers!!!

void bubble(char *wHand[][PLAYER_HAND], const int SIZE)
{
int limit = SIZE;
for(int pass = 1; pass < limit; pass++)
{
for(int count = 0; count < (limit -1); count++)
{
if(wHand[count] > wHand[count+1])
{
char *temp[2] = {""};
temp[] = wHand[count];
wHand[count] = wHand[count+1];
wHand[count+1] = temp[];
}
}
}
}

What's annoying is that the sorting using the array player1[2][5] is slowing me down because I'm having trouble referencing the data. Any suggestions?

Share on other sites
I'd imagine sorting it would be the best way to evaluate hands. That way, you eliminate the possibilities and combinations as you evaluate each of the 5 cards.

Let's say you've got 5 non-sequential, non-suited cards.

Without sorting, if the cards are 9,6,8,7,4, you need to evaluate each card in that order to discover that it isn't a straight, because you have to account for the last number maybe being a 5. However, if you sort the cards in sequence: 4,6,7,8,9, it only takes two evaluations to discover that it is not a straight because you know it must go in order, and if it's ordered, building a loop to check for straights is much easier.

Plus, if it's ordered and you DO have a straight : 5,6,7,8,9, then it's much easier to find out the details of the hand, such as the high-card. If it's unordered: 9,6,5,7,8, you have to eventually reorder it to find out the same things, so why not have it ordered in the first place?

Share on other sites
CyJackX,

Thanks,as I'm trying to write the sort for the char arrays, I'm wondering if setting up the array as int arrays would be less of a headache in the long run.

P.s.(This is not intended at anyone specifically - unless I catch them laughing!)

For anyone who thinks arrays are easy, please don't laugh at my attempt at a sorting function (grin) ...I'm tryin! :)

-Any suggestions would be greatly appreciated.

Share on other sites
Quote:
 Original post by savaliaThanks,as I'm trying to write the sort for the char arrays, I'm wondering if setting up the array as int arrays would be less of a headache in the long run.

IMO, yes.

The card values are directly related to one specific number, except for the Ace, which can be high or low. It's much easier to sort as ints.

Have you considered enum types?

Share on other sites
[/quote]

IMO, yes.

The card values are directly related to one specific number, except for the Ace, which can be high or low. It's much easier to sort as ints.

Have you considered enum types?[/quote]

I'm reading the Dev Studio help on it right now, that would let me set the value of the Ace=1 & Ace=14 for evaluation in high and low situations.
(duh! the example is of a deck of playing cards :) )

Ex: A 2 3 4 5 - low straight
pts: 1 2 3 4 5

and

Ex: 10 J Q K A - high straight
pts: 10 11 12 13 14

right?

(good thing I didn't fire up my copy of Turtle Basic (Apple - circa early 80s)
to start kicking out the graphics.)