I'm attempting to create 5 card stud poker game, I'm almost in the final step of it but I am not understanding how to implement it.
Evaluate each players hand to determine winner(This is where my difficulty reside at the moment)

Sorry if the code is messy and long, still getting used to all this. I would appreciate any input.

This is what i have so far...

import random
deck = [ \
'2c' , '3c' , '4c' , '5c' , '6c' , '7c' , '8c' , '9c' , '10c' , 'Jc' , 'Qc' , 'Kc',  'Ac', \
'2h' , '3h' , '4h' , '5h' , '6h' , '7h' , '8h' , '9h' , '10h' , 'Jh' , 'Qh' , 'Kh' , 'Ah', \
'2s' , '3s' , '4s' , '5s' , '6s' , '7s' , '8s' , '9s' , '10s' , 'Js' , 'Qs' , 'Ks' , 'As', \
'2d' , '3d' , '4d' , '5d' , '6d' , '7d' , '8d' , '9d' , '10d' , 'Jd' , 'Qd' , 'Kd' , 'Ad' ]
def sortRank(hand):
    ranks = list()
    for card in hand:
        if len(card) == 3:
            ranks.append(10)
        elif card[0] == 'J':
            ranks.append(11)
        elif card[0] == 'Q':
            ranks.append(12)
        elif card[0] == 'K':
            ranks.append(13)
        elif card[0] == 'A':
            ranks.append(14)
        else:
            ranks.append(int(card[0]))  
    for i in range(4):
        for j in range(i+1, 5):
            if ranks[j] > ranks:
                temp = hand
                tempR = ranks
                hand = hand[j]
                ranks = ranks[j]
                hand[j] = temp
                ranks[j] = tempR
    return hand

def sortSuit(hand):
    ranks = list()
    for card in hand:   
        if card[1] == 'h' or (card[1] == '0' and card[2] == 'h' ):
            addval = 26
        elif card[1] == 's' or (card[1] == '0' and card[2] == 's' ):
            addval = 13
        elif card[1] == 'd' or (card[1] == '0' and card[2] == 'd' ):
            addval = 0
        else:
            addval = 39
        if len(card) == 3:
            ranks.append(10 + addval)
        elif card[0] == 'J':
            ranks.append(11 + addval)
        elif card[0] == 'Q':
            ranks.append(12 + addval)
        elif card[0] == 'K':
            ranks.append(13 + addval)
        elif card[0] == 'A':
            ranks.append(14 + addval)
        else:
            ranks.append(int(card[0]) + addval)        
    for i in range(4):
        for j in range(i+1, 5):
            if ranks[j] > ranks:
                temp = hand
                tempR = ranks
                hand = hand[j]
                ranks = ranks[j]
                hand[j] = temp
                ranks[j] = tempR
    return hand
def numRank(hand):
    ranks = list()
    for card in hand:
        if len(card) == 3:
            ranks.append(10)
        elif card[0] == 'J':
            ranks.append(11)
        elif card[0] == 'Q':
            ranks.append(12)
        elif card[0] == 'K':
            ranks.append(13)
        elif card[0] == 'A':
            ranks.append(14)
        else:
            ranks.append(int(card[0]))
    return ranks
def numSuit(hand):
    ranks = list()
    for card in hand:
        if card[1] == 'h' or (card[1] == '0' and card[2] == 'h' ):
            addval = 26
        elif card[1] == 's' or (card[1] == '0' and card[2] == 's' ):
            addval = 13
        elif card[1] == 'd' or (card[1] == '0' and card[2] == 'd' ):
            addval = 0
        else:
            addval = 39
        ranks.append(addval)
    return ranks

def setDealt(nbrhands):
    sets=list()
    while len(sets) < nbrhands:
        sets.append(list())
    return sets

def shuffle(nbrhands):
    random.shuffle(deck)
    return deck

def dealHands(nbrhands):
    setsofHands=setDealt(nbrhands)
    shuffledDeck=shuffle(nbrhands)
    for i in range(nbrhands):
        setsofHands.append(shuffledDeck[0])
        shuffledDeck.remove(shuffledDeck[0])
    for i in range(nbrhands):
        setsofHands.append(shuffledDeck[0])
        shuffledDeck.remove(shuffledDeck[0])
    for i in range(nbrhands):
        setsofHands.append(shuffledDeck[0])
        shuffledDeck.remove(shuffledDeck[0])
    for i in range(nbrhands):
        setsofHands.append(shuffledDeck[0])
        shuffledDeck.remove(shuffledDeck[0])
    for i in range(nbrhands):
        setsofHands.append(shuffledDeck[0])
        shuffledDeck.remove(shuffledDeck[0])
    return setsofHands

def royalFlush(hand):
    sorthand=sortRank(hand)
    if sorthand == ['Ad', 'Kd', 'Qd', 'Jd', '10d']: 
        return True
    elif sorthand == ['Ac', 'Kc', 'Qc', 'Jc', '10c']:
        return True
    elif sorthand == ['Ah', 'Kh', 'Qh', 'Jh', '10h']:
        return True
    elif sorthand == ['As', 'Ks', 'Qs', 'Js', '10s']:
        return True
    else:
        return False

def straightFlush(hand):
    numrank=numRank(sortRank(hand))
    numsuit=numSuit(sortSuit(hand))
    if numsuit[0]==numsuit[1]==numsuit[2]==numsuit[3]==numsuit[4]:
        if numrank[0]==numrank[1]+1==numrank[2]+2==numrank[3]+3==numrank[4]+4:
            return True
        else:
            return False
    else:
        return False    

def fourOfAKind(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1]==numrank[2]==numrank[3]:
        return True
    elif numrank[1]==numrank[2]==numrank[3]==numrank[4]:
        return True
    else:
        return False

def fullHouse(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1] and numrank[2]==numrank[3]==numrank[4]:
        return True
    elif numrank[0]==numrank[1]==numrank[2] and numrank[3]==numrank[4]:
        return True 
    else:
        return False 

def flush(hand):
    numsuit=numSuit(sortSuit(hand))
    if numsuit[0]==numsuit[1]==numsuit[2]==numsuit[3]==numsuit[4]:
        return True
    else:
        return False

def straight(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1]+1==numrank[2]+2==numrank[3]+3==numrank[4]+4:
        return True
    else:
        return False

def threeOfAKind(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1]==numrank[2]:
        return True 
    elif numrank[1]==numrank[2]==numrank[3]:
        return True
    elif numrank[2]==numrank[3]==numrank[4]:
        return True
    else:
        return False

def twoPair(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1] and numrank[2]==numrank[3]:
        return True
    elif numrank[1]==numrank[2] and numrank[3]==numrank[4]:
        return True
    elif numrank[0]==numrank[1] and numrank[3]==numrank[4]:
        return True
    else:
        return False

def pair(hand):
    numrank=numRank(sortRank(hand))
    if numrank[0]==numrank[1]:
        return True
    elif numrank[1]==numrank[2]:
        return True
    elif numrank[2]==numrank[3]:
        return True
    elif numrank[3]==numrank[4]:
        return True
    else:
        return False

def winner(allHands):
    checkhand=list()
    winnerhand=list()
    for i in allHands:
        if royalFlush(i)==True:
            checkhand.append(i)
    if len(checkhand)==0:
        for i in allHands:
            if straightFlush(i)==True:
                checkhand.append(i)
    if len(checkhand)==0:
        for i in allHands:
            if fourOfAKind(i)==True:
                checkhand.append(i)
    if len(checkhand)==0:
        for i in allHands:
            if fullHouse(i)==True:
                checkhand.append(i)
    if len(checkhand)==0:    
        for i in allHands:
            if flush(i)==True:
                checkhand.append(i)            
    if len(checkhand)==0:
        for i in allHands:
            if straight(i)==True:
                checkhand.append(i)
    if len(checkhand)==0:        
        for i in allHands:
            if threeOfAKind(i)==True:
                checkhand.append(i)           
    if len(checkhand)==0:
        for i in allHands:
            if twoPair(i)==True:
                checkhand.append(i)            
    if len(checkhand)==0:    
        for i in allHands:
            if pair(i)==True:
                checkhand.append(i)
    if len(checkhand)==0:
        for i in allHands:
            checkhand.append(i)

I've done this several times before, but not in Python. A simple approach is to give each hand a numerical ranking, and there are a couple of ways to do that.

One way is to assign each hand a number, specifically the number of hands it beats. Thus 7-5-4-3-2 offsuit gets rank 0 and a royal flush gets rank 2598956 since there are 2598960 possible 5-card hands and 4 possible royals. I have a C program that actually generates every possible hand and does this.

Another, somewhat simpler way is to assign each hand a floating point value derived from the major type (bust=0, pair=1, flush=5) etc. and the rank of the cards kind of as you would use yourself.
Here are some examples, where I'm assigning "values" of deuce=0, trey=1 .. king=11, ace=12:

1 pair: base value 1.0 to which you add:

  • X/13.0 where X is the "value"what you have a pair of
  • K1/(13.0*13.0) where K1 is the "value" of the highest kicker
  • K2/(13.0*13.0*13.0) where K2 is the "value" of the second highest kicker
  • K3/(13.0*13.0*13.0*13.0) where K3 is the "value" of the bottom kicker

So JJK53 becomes 1.758796960890725, if my math is correct. (1.0 + 9/13.0 + 11/(13.0*13.0) + 3/(13.0*13.0*13.0) + 1/(13.0*13.0*13.0*13.0))

2 pair, QQ55J, becomes

  • 2.0 for 2 pair base value PLUS
  • 10/13. because Queen is worth 10 PLUS
  • 3/(13.0*13.0) because a Five is worth 3, PLUS
  • 9/(13.*13.*13.) because a Jack is worth 9

... for a total value of 2.7910787437414655, again if my math is correct.

As you can see, you need separate pieces of code to evaluate separate hand types. Straights are pretty easy to evaluate, just look at the top card (but remember A-2-3-4-5 has top card 5, not A, so is worth base 4.0+3/13.0 since a Five is "worth" 3.)

While a bit tedious, something like the above returns you a number corresponding to a hand such that the better hand always has the higher number. In Python, I would not calculate these values directly but instead first calculate a sequence for each hand type so JJK53 above would turn into (1,9,11,3,1) and QQ55J would turn into (2,10,3,9) and a wheel would be (4,3). Then have a general routine to do all the arithmetic on the sequence, just to separate the arithmetic from the hand-specific problem of ranking each type of hand.

This article has been dead for over six months. Start a new discussion instead.