#!/usr/bin/env python3
import collections
import itertools
import random
suit_list = ("Hearts", "Spades", "Diamonds", "Clubs")
numeral_list = ("2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace")
class card:
def __init__(self, numeral, suit):
self.numeral = numeral
self.suit = suit
self.card = self.numeral, self.suit
def __repr__(self):
return self.numeral + "-" + self.suit
class poker_hand():
def __init__(self, card_list):
self.card_list = card_list
def __repr__(self):
short_desc = "Nothing."
numeral_dict = collections.defaultdict(int)
suit_dict = collections.defaultdict(int)
for my_card in self.card_list:
numeral_dict[my_card.numeral] += 1
suit_dict[my_card.suit] += 1
# Pair
if len(numeral_dict) == 4:
short_desc = "One pair."
# Two pair or 3-of-a-kind
elif len(numeral_dict) == 3:
if 3 in numeral_dict.values():
short_desc ="Three-of-a-kind."
else:
short_desc ="Two pair."
# Full house or 4-of-a-kind
elif len(numeral_dict) == 2:
if 2 in numeral_dict.values():
short_desc ="Full house."
else:
short_desc ="Four-of-a-kind."
else:
# Flushes and straights
straight, flush = False, False
if len(suit_dict) == 1:
flush = True
min_numeral = min([numeral_list.index(x) for x in numeral_dict.keys()])
max_numeral = max([numeral_list.index(x) for x in numeral_dict.keys()])
if int(max_numeral) - int(min_numeral) == 4:
straight = True
# Ace can be low
low_straight = set(("Ace", "2", "3", "4", "5"))
if not set(numeral_dict.keys()).difference(low_straight):
straight = True
if straight and not flush:
short_desc ="Straight."
elif flush and not straight:
short_desc ="Flush."
elif flush and straight:
short_desc ="Straight flush."
enumeration = "/".join([str(x) for x in self.card_list])
return "{enumeration} ({short_desc})".format(**locals())
class deck(set):
def __init__(self):
for numeral, suit in itertools.product(numeral_list, suit_list):
self.add(card(numeral, suit))
def get_card(self):
a_card = random.sample(self, 1)[0]
self.remove(a_card)
return a_card
def get_hand(self, number_of_cards=5):
if number_of_cards == 5:
return poker_hand([self.get_card() for x in range(number_of_cards)])
else:
raise NotImplementedError
for i in range(100000):
print(deck().get_hand())
Diff to Previous Revision
--- revision 3 2013-07-07 04:05:28
+++ revision 4 2013-07-07 04:08:23
@@ -18,7 +18,6 @@
def __init__(self, card_list):
self.card_list = card_list
def __repr__(self):
- return_string = "/".join([str(x) for x in self.card_list])
short_desc = "Nothing."
numeral_dict = collections.defaultdict(int)
suit_dict = collections.defaultdict(int)
@@ -59,7 +58,8 @@
short_desc ="Flush."
elif flush and straight:
short_desc ="Straight flush."
- return "{return_string} ({short_desc})".format(**locals())
+ enumeration = "/".join([str(x) for x in self.card_list])
+ return "{enumeration} ({short_desc})".format(**locals())
class deck(set):
def __init__(self):