Source code for serge.blocks.cards

"""Simple card system"""

import random


# Error classes
[docs]class NoCard(Exception): """The collection was empty"""
[docs]class CardNotFound(Exception): """The card was not found"""
[docs]class Card(object): """A card for use in a card game""" def __init__(self, name): """Initialise the card""" self.name = name
[docs]class CardCollection(object): """A collection of cards""" def __init__(self): """Initialise the collection""" self.cards = []
[docs] def numberOfCards(self): """Return the number of cards in the deck""" return len(self.cards)
[docs] def addCardToTop(self, card): """Add a card to the top of the deck""" self.cards.append(card)
[docs] def addCardsToTop(self, cards): """Add multiple cards to the top of the deck""" for card in cards: self.addCardToTop(card)
[docs] def containsCard(self, card): """Return True if the collection contains the card""" return card in self.cards
[docs] def clearCards(self): """Clear all the cards""" self.cards[:] = []
[docs] def getCards(self): """Return all the cards""" # # Return a copy of the list to avoid accidentally changing the deck return self.cards[:]
[docs] def shuffleCards(self): """Shuffle the cards""" random.shuffle(self.cards)
[docs] def drawTopCard(self): """Draw the top card""" try: return self.cards.pop(-1) except IndexError: raise NoCard('There are no cards in the collection')
[docs] def drawTopCards(self, number): """Return a number of cards from the top of the deck""" return [self.drawTopCard() for _ in range(number)]
[docs] def drawSpecificCard(self, card): """Withdraw a specific card from the collection""" try: self.cards.remove(card) except ValueError: raise CardNotFound('Card %s was not in the collection' % card) else: return card
[docs]class Deck(CardCollection): """A deck of cards"""
[docs]class StandardCard(Card): """A standard card""" # Suits HEART = 'Heart' DIAMOND = 'Diamond' CLUB = 'Club' SPADE = 'Spade' SUITS = [HEART, DIAMOND, CLUB, SPADE] def __init__(self, suit, card): """Initialise the card""" super(StandardCard, self).__init__('%s-%s' % (card, suit[0])) self.suit = suit self.card = card
[docs]class StandardDeck(Deck): """A deck of standard cards""" def __init__(self): """Initialise the deck""" super(StandardDeck, self).__init__() # # Add the cards for suit in StandardCard.SUITS: for card in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']: self.addCardToTop(StandardCard(suit, card))