0
\$\begingroup\$

The examples below come from the answer by Steven Stadnicki in Determining Poker Hands

Suppose a player get a hand like A9772. I would like to store the hand as either 77A92 or, better yet, 7A927. A hand of two pair would be stored with the higher of the two pairs first, then the lower, then the 'kicker' (so, e.g., A9977 would store as 97A97). Three of a kind would be stored with one card of the three first, then the kickers, then the other cards (e.g., A7772 would be 7A277); a full house would be stored with one of its three and then one of its two (e.g., 99777 would be stored as 79779);

How can I do that? Suppose I have a vector of cards :

std::vector<Card> m_cards;

Or simpler a vector of int

std::vector<int> v = {3, 9, 7, 7, 2};

The goal is to sort the above vector as follows: 77932.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You need to determine the class of your hand first. I.e. if the hand is "two pair", "full house" etc.

The answer you linked is only meant to compare hands with identical class. It does this by comparing their lexicographical orders (using the poker "alphabet") after the hands are sorted according to their class.

That means you should already know what class and which cards form part of that class. For example, given the hand

7, A, 9, 7, 9

You need to know that it's a two pair, of 9 and 7, already. So based on that, you sort based on the cards that form the two pair (7, 7, 9, 9), using the higher pair first:

9, 9, 7, 7, A

The "even better" part of the answer is not strictly necessary. It is a questionable micro-optimisation.

The idea is you can easily compare the hand against another two pair hand, like

9, 9, 8, 8, 10

By comparing the two, you will notice that the 9's are equal but the second hand's 8 is higher, so the second hand wins.

\$\endgroup\$
5
  • \$\begingroup\$ Thank you for your reply but that was not exactly my question. It seems that you just repeated what they said in the example I gave above. I already determined the class of my hands. What I want to do is an algorithm to rank in the way I mentioned the cards for a specific hand: I a hand is 39772, how to sort it such as I got : 77932? \$\endgroup\$
    – RangerBob
    Commented Mar 19, 2018 at 8:50
  • \$\begingroup\$ @RangerBob you first decide that it is a single pair so that pair should be at the start, then the rest of the cards are sorted high to low. When comparing hands of the different classes you first compare the class and then you compare the hands themselves, that way a 4-of-a-kind with 2s beats a Ace high \$\endgroup\$ Commented Mar 19, 2018 at 10:45
  • \$\begingroup\$ @RangerBob some classes need to be sorted differently. Most of the time you can sort them as-is, but for hands like the full house, you need to make sure the 3-of-a-kind is ahead of the pair. Finally sort the rest of the cards normally. You don't need to worry about the kicker, it's just a fancy way of saying "the highest of the leftover cards". \$\endgroup\$ Commented Mar 19, 2018 at 21:28
  • \$\begingroup\$ I understand but my question is how can I do that? \$\endgroup\$
    – RangerBob
    Commented Mar 20, 2018 at 9:33
  • \$\begingroup\$ @RangerBob Basically, make a class that has the cards, and a enum in it that contains the type of hand, and make a custom sort based on the state of that enum. \$\endgroup\$
    – ChrisPy
    Commented Apr 18, 2018 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .