""" simple_cards.py A simplified version of Card, Deck, Hand, using only values 1 to 10 for each card, a 10 card deck, and a 3 card hand. Jim M | Nov 2012 """ import random class Card(object): """ A playing card with value 1 to 10, no suit """ def __init__(self, value): self.value = value def __str__(self): return "Card({})".format(self.value) def __cmp__(self, other): return cmp(self.value, other.value) class Deck(object): """ A collection of Card objects """ def __init__(self): self.cards = [] for i in range(1, 11): self.cards.append(Card(i)) self.shuffle() def __str__(self): return "A deck of {} cards".format(len(self.cards)) def shuffle(self): random.shuffle(self.cards) def deal_one_card(self): which_card = random.randrange(len(self.cards)) return self.cards.pop(which_card) def deal_cards(self, n): result = [] for i in range(n): result.append(self.deal_one_card()) return result class Hand(object): """ Here a hand is 3 cards, just to keep it simple """ def __init__(self, deck): self.cards = deck.deal_cards(3) self.cards.sort() # put them in order def __str__(self): result = "Cards:" for c in self.cards: result += str(c.value) + " " return result def is_straight(self): for i in range(1, 3): # look at cards i=1,2 if self.cards[i].value != self.cards[i-1].value+1: return False return True