Hi, I've been trying to make a poker game, i got the basic things running like giving the cards to the player and dealer, but i am having trouble coming up with a way to compare the 2 hands... I'm new to python, i tried different ways and didn't really work out.. Right now i have the cards put in order. Can someone please tell me a good way to compare the hands between the player and dealer >< that will be great. Thanks

4
Contributors
13
Replies
15
Views
7 Years
Discussion Span
Last Post by yari
Featured Replies
• 1

...And here's my definition of class Card, along with a very minor exercise[CODE]class Card: """ A standard poker card with suit and value Value is intended to be an integer for easy sorting and comparison Suit can be anything. Test code uses a single capital letter """ # I like …

You need a system that creates an ordinal value for each hand (the rank of the hand) and you just compare the ranks. The trick, then, is to write that function. I'm not a poker expert, but I'd do something like:
Recognize all the possible ways the hand can be scored (A-C, A-D, A-H, 9-H, 9-D: Ace high, 2 of a kind (once for aces, once for 9s) two pair (aces high) three of a kind (aces), full (house-aces) high.

``````for "each of the possible options":
"Evaluate the score based on 'kind'" # N of a kind, flush, etc
"Adjust the evaluated score to account for the high card when it matters"
"Adjust the evaluated score to account for the suit when it matters"
"Compare against the prior best score retaining the best and its explanation"``````

The score can be quite large if you wish, so maybe each better 'kind' is 1000 more than the prior, then it is easy to have room for the adjustments. Or it can be any arbitrary size and you can use floating point to allow for adjustments.

How you recognize each kind is also interesting. Maybe you can have a collection (list, dict) of functions each returning a pair: (score, explanation) (score is 0 if there is no match). Some would look at the value, some at the suit

This URL seems to have a broad outline of the way to score: http://www.compendia.co.uk/poker_scores.htm

I really dont get this.. I'm sorry.. theres scores in poker o.o? I thought i just need to compare the cards and win or lose...

I'm still trying to see how to get the hand to check for royal flush T_T~ can u maybe give me an example of doing that or something please. Thanks.

I think he meant scores as in rank
Like a flush is worth more then a pair.

ohh i see~! thanks

but my problem right now is i'm stuck at the part where i have to check what score the 5 cards are @_@ I have no idea how to do that, I tried "for" and "if" and "while" loops but i always can't get it right for some reasons >< help please... right now i got this code for checking royal flush... but it doesn't have the outcome i want ~_~

``````def straightFlush(list):
number = int(list[0][0]) # 4
suit = list[0][1]
count = number
same = True

print "number is", number
print count
print same

while same == True:

for i in range len(list):
if count == number:
count = count + 1
number = number + 1
same = True

else:
same = False
return -1

return 1000``````

Don't use 'list' as a variable. It is a built-in function.
I rewrote your function to make it more readable. However, without the complete code, I don't know exactly how I can help

``````def straightFlush(list1):
number = int(list1[0][0])
suit = list1[0][1]
count = number

print "number is", number
print count

while True:

for i in list1:
number = i[0]
if count == number:
count = count + 1
else:
return -1
return 1000``````

Edited by redyugi: misspelled words

i tried that with the code, but it turns out both -1
but the outcome should be 1000 and -1

``````# check flush---------------------------

def straightFlush(list1):
number = int(list1[0][0])
suit = list1[0][1]
count = number

while True:

for i in list1:
number = i[0]
if count == number:
count = count + 1
else:
return -1
return 1000

#start program---------------------------------

playerHand = ['2H', '3H', '4H', '5H', '6H']
dealerHand = ['3H', '7H', 'KH', 'QH', 'QH']

print playerHand
print dealerHand

print straightFlush(playerHand)
print straightFlush(dealerHand)``````

You look at the 5 cards in the hand (assuming we aren't going to play some kind of silly 7 card game or whatever). In the code below, I'm making some assumptions about the kind of thing a "card" is. You will need to fix your code to make those assumptions true... or fix my assumptions to match your data structure. I'm also assuming that what I'm doing is clear enough to be helpful. Please take the time to actually think your way through the code, seeing what it does.

Here are three functions that determine if a hand is a straight, a flush, or both:

``````def straighFlush(listOfCards):
return flush(listOfCards) and straight(listOfCards)

def flush(listOfCards):
suit = listOfCards[0].suit
for card in listOfCards[1:]:
if card.suit != suit:
return False
return True

def straight(listOfCards): # simplistic about Aces high or/and low
# next line: http://docs.python.org/library/functions.html?highlight=sorted#sorted
cards = sorted(listOfCards, key=lambda x: x.value) # sort on value, not suit
for i in range(len(cards)):
if not s[i] = s[0]+i:
return False
return True``````

Edited by griswolf: n/a

...And here's my definition of class Card, along with a very minor exercise

``````class Card:
"""
A standard poker card with suit and value
Value is intended to be an integer for easy sorting and comparison
Suit can be anything. Test code uses a single capital letter
"""
# I like letters for the face cards and the Ace, so here's a way
numToName = {1:'A',11:'J',12:'Q',13:'K'} # ace is low
for i in range(2,11):
numToName[i] = str(i)

def __init__(self, suit, value):
self.suit = suit
self.value = value

def __str__(self):
return "%s:%s"%(self.suit,Card.numToName[self.value])

deck = []
for v in [1,2,3,4,5,6,7,8,9,10,11,12,13]:
for s in ['C','D','H','S']:
deck.append(Card(s,v))
# for c in deck:
#   print(c)

import random
random.shuffle(deck)
for c in deck:
print(c)``````

Thanks a lot for the replies!!

this made it more clear for me to do this. however i got one little question.

I got my class Card but it gives me the T(10), J Q K and A

I was trying to change that outside the class card, like making a function to change it.

I tried to make a function to change that, but somehow it doesn't change the list when i return the list. However, when i put prints in the def, it says its changed.. How come?

I am still using the same playerHand and DealerHand but it doesn't change... Sorry i'm really bad with this. Thanks a lot for the help!!

``````def checkCards(list):

for cards in list:
a = cards[0]
if a in "JQKA":
print a
if a== "J":
a = "11"
elif a == "Q":
a = "12"
print a
cards = a+cards[1]
print cards
elif a == "K":
a = "13"
cards = a+cards[1]
elif a == "A":
a = "1"

return list``````

You really need to change your variable named list to have some other name: list is a key word in Python. (I suppose Python is 'doing the right thing' by hiding that keyword in contexts where your variable is in scope, but it is still very confusing)

I also just realized that it wouldn't work for comparing hands if some cards are 3 length and some are 2...sigh...

dealerHand =

Two Queen of Hearts for dealer? I wonder what hand that makes ;)

And, Yes, please do not use list for variable name as function list from python becomes unusable (hidden) after that.

Edited by pyTony: n/a

Lol yah i changed it after a while LOL XD and i changed all the list as variable to something else ^_^ thanks a lot for the tips!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.