Welcome, guest | Sign In | My Account | Store | Cart
#!/usr/bin/env python3
import collections
import itertools
import random
import sys

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 hand():
    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)
        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."
        return "{return_string} ({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):
        return hand([self.get_card() for x in range(number_of_cards)])

for i in range(100000):
    print(deck().get_hand())

Diff to Previous Revision

--- revision 1 2013-07-06 21:56:23
+++ revision 2 2013-07-07 03:57:26
@@ -3,8 +3,10 @@
 import itertools
 import random
 import sys
+
 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
@@ -12,6 +14,54 @@
         self.card = self.numeral, self.suit
     def __repr__(self):
         return self.numeral + "-" + self.suit
+
+class hand():
+    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)
+        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."
+        return "{return_string} ({short_desc})".format(**locals())
+
 class deck(set):
     def __init__(self):
         for numeral, suit in itertools.product(numeral_list, suit_list):
@@ -21,41 +71,7 @@
         self.remove(a_card)
         return a_card
     def get_hand(self, number_of_cards=5):
-        return_list = list()
-        for i in range(number_of_cards):
-            return_list.append(self.get_card())
-        return return_list
+        return hand([self.get_card() for x in range(number_of_cards)])
 
-for i in range(10000):
-    print()
-    my_deck = deck()
-    my_hand = my_deck.get_hand()
-    print(my_hand)
-    numeral_dict = collections.defaultdict(int)
-    suit_dict = collections.defaultdict(int)
-    for my_card in my_hand:
-        numeral_dict[my_card.numeral] += 1
-        suit_dict[my_card.suit] += 1
-    if len(numeral_dict) == 4:
-        print("One pair.")
-    elif len(numeral_dict) == 3:
-        if 3 in numeral_dict.values():
-            print("Three-of-a-kind.")
-        else:
-            print("Two pair.")
-    elif len(numeral_dict) == 2:
-        if 2 in numeral_dict.values():
-            print("Full house.")
-        else:
-            print("Four-of-a-kind.")
-    elif len(suit_dict) == 1:
-        print("Flush.")
-    else:
-        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:
-            print("Straight.")
-        # Ace can be low
-        low_straight = set(("Ace", "2", "3", "4", "5"))
-        if not set(numeral_dict.keys()).difference(low_straight):
-            print("Straight.")
+for i in range(100000):
+    print(deck().get_hand())

History