#54 - Poker hands

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of the same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following 5 hands dealt to two players:

HandPlayer 1Player 2Winner
15H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
25D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
32D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
44D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
52H 2D 4C 4D 4S
Full House
with Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1’s cards and the last five are Player 2’s cards, You can assume that all hands are valid (no invalid characters or repeated cards), each player’s hands are in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?


We simply need to convert the rules of poker into code. The hand order we check in is important, as we need to check the most restrictive down to the most relaxed hand. The code mostly consists of parsing the file, and then sorting each player’s hand based on card value. Then we grab the counts of each card, which allows to check the hands for four of a kind, full house, etc.

We also need to keep track of the highest card in each ranking for each player, in case the hand ties in rank. If these cards are still tied, then we need to keep removing the highest ranked card until one player wins.

# file: "problem054.py"
# Returns the ranking of the hand and the
# highest card in that ranking
def pokerRanking(hand):
    order = '23456789TJQKA'
    # First split the hand into a paired list of sorts
    #hand = list(zip(*[tuple(card) for card in hand]))
    numbers = [card[0] for card in hand]
    suits = [card[1] for card in hand]
    # Go down the list
    # Royal Flush - T, J, Q, K, A all same suit
    if all(card in numbers for card in 'TJQKA') and all(suits[0] == s for s in suits):
        return 'A', 'RF'
    # Straight Flush - consecutive values
    sortedNums = sorted(numbers, key=lambda x: order.index(x))
    if (all(suits[0] == s for s in suits) and
            all(order.index(sortedNums[i+1]) - order.index(sortedNums[i]) == 1 for i in range(4))):
        return sortedNums[-1], 'SF'
    # Four of a kind - only need to look at values
    unique, counts = np.unique(numbers, return_counts=True)
    # Sort unique
    unique = unique[np.argsort(counts)[::-1]]
    counts = np.sort(counts)[::-1]
    if counts[0] == 4:
        return unique[0], 'FK'
    # Full House
    if counts[0] == 3 and counts[1] == 2:
        return unique[0], 'FH'
    # Flush - all same suit
    if all(suits[0] == s for s in suits):
        return max(numbers, key=lambda x: order.index(x)), 'F'
    # Straight
    if all(order.index(sortedNums[i+1]) - order.index(sortedNums[i]) == 1 for i in range(4)):
        return sortedNums[-1], 'S'
    # Three of a kind
    if counts[0] == 3:
        return unique[0], 'TK'
    # Two pairs
    if counts[0] == 2 and counts[1] == 2:
        return max(unique, key=lambda x: order.index(x)), 'TP'
    # One pair
    if counts[0] == 2:
        return unique[0], 'OP'
    # Highest card
    return max(numbers, key=lambda x: order.index(x)), 'H'


with open('p054_poker.txt') as f:
    pokerHands = np.array([[hands[:14].split(' '), hands[-14:].split(' ')] for hands in f.read().splitlines()])
# Codes for each card ranking.
rankings = ['H', 'OP', 'TP', 'TK', 'S', 'F', 'FH', 'FK', 'SF', 'RF']
order = '23456789TJQKA'
p1Wins = 0
for hand1, hand2 in pokerHands:
    hand1Res = pokerRanking(hand1)
    hand2Res = pokerRanking(hand2)
    if rankings.index(hand1Res[1]) > rankings.index(hand2Res[1]):
        p1Wins += 1
    if rankings.index(hand1Res[1]) == rankings.index(hand2Res[1]):
        if order.index(hand1Res[0]) > order.index(hand2Res[0]):
            p1Wins += 1
        elif order.index(hand1Res[0]) == order.index(hand2Res[0]):
            # Keep removing the highest card from both sides until
            # there's a winner
            numbers1 = [card[0] for card in hand1]
            numbers2 = [card[0] for card in hand2]
            while max(numbers1, key=lambda x: order.index(x)) == \
                    max(numbers2, key=lambda x: order.index(x)):
                toRem = max(numbers1, key=lambda x: order.index(x))
                numbers1.remove(toRem)
                numbers2.remove(toRem)
            if (max(numbers1, key=lambda x: order.index(x)) >
                max(numbers2, key=lambda x: order.index(x))):
                p1Wins += 1

print(p1Wins)

Running this large code block results in,

376
0.15291450749056495 seconds.

Therefore, Player 1 wins 376 of the hands listed.