SUIT_DICT = { 1:"H", 2:"D", 3:"C", 4:"S" } SUIT_REVERSE = { "H":1, "D":2, "C":3, "S":4 } FACE_DICT = { 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9",10:"T", 11:"J", 12:"Q", 13:"K", 14:"A" } FACE_REVERSE = { "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "T":10, "J":11, "Q":12, "K":13, "A":14 } def makecard(strname): """uses string value and string suit to construct a Card""" assert len(strname) == 2, "makecard(strname) takes two-character canonicals only" value = strname[0] suit = strname[1] value = value.upper() suit = suit.upper() return Card(FACE_REVERSE[value], SUIT_REVERSE[suit]) def makehand(strhand): """turns a comma-delineated string into a hand list""" handlist = strhand.split(",") handlist = [x.strip() for x in handlist] newlist = [makecard(f) for f in handlist] return newlist class Card: """ basic card class """ def __init__(self, face, suit): """ constructor to create a card face -- face of the card (2-14) suit -- suit of the card (1-4) """ assert int(face), "Face must be an integer in range (2,14)" assert int(suit), "Suit must be an integer in range (1,4)" self._face = face self._suit = suit def face(self): return FACE_DICT[self._face] def suit(self): return SUIT_DICT[self._suit] def value(self): return self._face def same_card(self, other): """returns True if other has same suit and face as self, False otherwise""" return other.suit() == self.suit() and other.face() == self.face() def __cmp__(self, cmpto): """returns the greater of two cards, None if equal""" return (self._face > cmpto.value()) or -(self._face < cmpto.value()) def __repr__(self): return str(self.face() + self.suit()) class Deck: """ basic deck class""" def __init__(self, count=1): """ constructor to create a deck count -- number of 52-card decks in this deck """ self._deck = [] self._burned = [] for x in range(1, count+1): for s in range(1,5): for f in range(2,15): self._deck.append(Card(f,s)) def __len__(self): return len(self._deck) def __repr__(self): return str([x for x in self._deck]) def __getitem__(self, i): return self._deck[i] def shuffle(self): import random shufnum = random.choice(range(2,7)) for l in range(1,shufnum): # shuffle multiple times to lower chances of seed-detection random.shuffle(self._deck) def peek(self): """ returns the top card without removing it """ return self._deck[-1] def top(self): """ returns the top card off the top of the deck and burns it""" t_card = self._deck[-1] self._burned.append(t_card) del self._deck[-1] return t_card def burns(self): """ returns the burned card list """ return self._burned def reset(self): """ resets the deck (combines remaining and burned) """ self._deck.extend(self._burned) self._burned = [] self.shuffle() def random_deal(self, size): """deals a random hand of size cards""" import random dealt = [] for t in range(0, size): dealt.append(random.choice(self._deck)) return dealt if __name__ == "__main__": import random, handlib f = Card(random.choice(range(2, 15)), random.choice(range(1,5))) print f