# Poker game good algorithm for solving ties between two pairs

#### sshou14

##### New Member
This is the Poker problem in one of previous year's final. See question here
I attempted most questions in the problem and have attached my worksheets, and here's my question:
Does anyone have any good idea how to best solve ties between two pairs?
8C 8D 7S 5S 5C
9C 8S 8H 5H 5D

Here's my idea:
step 1: rank the five cards (solved in part 1)
step 2: Since there're two pairs in five cards, there must be a pair in the first three cards, and another pair in the last three cards. We can use the Mode function to find the two pairs
step 3: if both pairs are equal, we can just do a high card rank for all the cards in order to break ties.

What do you think is the best way to do it?

### Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

#### edopts

##### New Member
Hi sshoul14,
Was puzzling over this one for some time. You can't just find the high card if both pairs are equal because the high card could be part of one of the pairs. You have to find the one that isn't a pair or the one that occurs least frequently and rank them. I assume during your sorting process you separated the numbers and suits. For me, this was in row 9. I found this on another site but it worked great to find the single card for player 1.

=INDEX(\$D\$9:\$H\$9, MATCH(MIN(INDEX(COUNTIF(\$D\$9:\$H\$9,\$D\$9:\$H\$9),0)), INDEX(COUNTIF(\$D\$9:\$H\$9,\$D\$9:\$H\$9), 0),0))

Use the same format for player 2 and then rank them. I hate really long formulas so I made a little table for high pair/low pair/single for each player using Mode as you suggested in step 2 and the above for the single.
Once I found that line it was easy.

#### Peter_SSs

##### MrExcel MVP, Moderator
You can't just find the high card if both pairs are equal because the high card could be part of one of the pairs. You have to find the one that isn't a pair or the one that occurs least frequently and rank them.
Welcome to the MrExcel board!

The blue part is right. The red part might work but I don't agree with the underlined part.

I'm guessing to some extent but I don't think sshou14 was suggesting "just find the high card". Considering the particular example shown and given it is already established that there are two equal pairs then I think the underlined part below meant:

Hand1: 8+8+7+5+5=33
Hand2: 9+8+8+5+5=35
Hand2 wins

Does anyone have any good idea how to best solve ties between two pairs?
8C 8D 7S 5S 5C
9C 8S 8H 5H 5D

Here's my idea:
step 1: rank the five cards (solved in part 1)
step 2: Since there're two pairs in five cards, there must be a pair in the first three cards, and another pair in the last three cards. We can use the Mode function to find the two pairs
step 3: if both pairs are equal, we can just do a high card rank for all the cards in order to break ties.

#### edopts

##### New Member
Peter,
Well that is much less complicated and works equally well. Just out of curiosity though, I think my method might be necessary to determine tie breaks for a full house since the total could be vastly different. The problem assigns a value of 14 to an ace. Therefore, a full house of AA222 is beaten by 44333 but the first hand would total 34 while the second hand only totals 17.
I believe I have solved the problem, so I am just interested if you would have a different method of solving that tie break.
Thanks,
Ed

#### Peter_SSs

##### MrExcel MVP, Moderator
I think my method might be necessary to determine tie breaks for a full house ..
As far as my knowledge of poker hands goes it is not possible to get a tie with two full house hands so a tie breaker would be unnecessary.

#### edopts

##### New Member
Ah, true. its part of the steps of the original problem.
First you identify each player's hand. If both players have the same hand, (a full house, straight, 2 pair, etc), then you have to determine which is better. My use of the words "tie breaks" was the verbiage used in the problem and not necessarily in the context of poker.

#### TimHeng

##### New Member
Hi edopts, Peter,

Actually, the blue part highlighted is incorrect, in the context that shhou14 was using it, I think. The high card rule should work its way down both hands, see that both high cards tie, and move to the next highest card. This is required to solve one of the other parts of the problem, to break ties when there are no hand combinations available.

So if we had two hands with the following cards, we should have the following results:

H1 : 8, 8, 7, 5, 5
H2 : 8, 8, 5, 5, 4
Win: T, T, 1, -, -

... thus identifying that Hand 1 is the winner.

sshou14, your approach is basically the way that we solved the 'two pairs' problem in the official Corality Worked Solution, except we took a different approach to determining the value of the two pairs, rather than using the MODE function as you have.

#### toidayma

##### New Member
In the case there are 2 pairs, after solving step 1, the higher pair is the 2nd card and the lower pair is the 4th card.
If both pairs are equal, we can just do a high card rank for all the cards in order to break ties.

For ranking high card, since all the card are ranked then you can find the winner by comparing the two sums: Sum1 of player1 = Card1*10^4 + Card2*10^3 +Card3*10^2 +Card4*10 + Card5. Similar for Player2.
If sum1 = sum2 then it's a tie, if sum1 > sum 2 then player 1 wins.

#### toidayma

##### New Member
I also attempted questions in the problem and have attached here

1,101,754
Messages
5,482,686
Members
407,358
Latest member
Maze123

### This Week's Hot Topics

• Finding issue in If elseif else with For each Loop
Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
• MsgBox Error
Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
• CELL FORMAT - IF CONDITION
My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
• Show numbers nearly the same
Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...