```
import random
from itertools import islice
suitcode={'heart':'H','diamonds':'D','spade':'S','club':'C'}
valuecode={14:'A',13:'K',12:'Q',11:'J'}
for i in range(2,11):
valuecode[i] = str(i)
def dealer(deck):
while deck:
yield deck.pop()
def gethand(dealer):
return sorted(islice(dealer,5))
def isflush(cards):
return all(suit==firstsuit
for value,suit in cards
for _,firstsuit in (cards[0],)
)
def isstraight(cards):
if all(a==b for a,b in zip(range(2,6)+[14],[x for x,_ in cards])):
## print 'Small ace',cards #straight 1..5
return True
else:
return all(nextvalue==thisvalue+1
for (thisvalue,_),(nextvalue,_) in zip(cards[:-1],cards[1:]))
def cardcode(value,suit):
return suitcode[suit]+valuecode[value]
def codedhand(cards):
return ','.join(cardcode(*card) for card in cards)
if __name__== '__main__':
wins = games = 0
handstyle=''
multiples={frozenset([1,3]):'Triple',frozenset([1,2]):'Two pairs', frozenset([1,4]):'Four same',
frozenset([2,3]):'Full house'}
while 'Royal' not in handstyle: # and wins < 1000:
## cards with ace as more high value 14
deck=[(value+2,suit)
for suit in ('heart','diamonds','spade','club')
for value in range(13)]
random.shuffle(deck)
getcards=dealer(deck)
while len(deck)>=10 and 'Royal' not in handstyle:
games+=1
playerhand, dealerhand = gethand(getcards), gethand(getcards)
for cards in (dealerhand,playerhand):
if len(set(x for x,_ in cards))<4: ## 3 or 4 same, two pairs or full house
wins+=1
values=list(x for x,_ in cards)
counts={}
for value in set(values):
counts[value]=values.count(value)
handstyle = multiples[frozenset(counts.values())]
print handstyle,':',codedhand(cards)
if isflush(cards) or isstraight(cards):
wins+=1
if isflush(cards) and isstraight(cards):
handstyle = 'Royal Flush' if cards[-1][0]==14 else 'Straight Flush'
print '***'+handstyle+'***'
else:
handstyle = 'Straight ' if isstraight(cards) else 'Flush'
print handstyle,':',codedhand(cards)
raw_input("\n\n%i wins in %i games" % (wins,games))
```

About the Author

**Are you able to help answer this sponsored question?**

Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.

Recommended Topics