Атанас обнови решението на 26.03.2014 15:56 (преди над 10 години)
+from collections import OrderedDict
+
+
+SUITS = OrderedDict()
+RANKS = OrderedDict()
+
+
+class Rank:
+ symbol = ''
+
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return str(self.symbol)
+
+
+class Suit:
+ color = ''
+
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return str(self.color)
+
+
+class Card():
+ rank = None
+ suit = None
+
+ def __init__(self, rank, suit):
+
+ self.rank = rank()
+ self.suit = suit()
+
+ def __setattr__(self, attribute, value):
+ if self.rank is not None and self.suit is not None:
+ raise AttributeError("can't set attribute")
+ return object.__setattr__(self, attribute, value)
+ #raise AttributeError ("This class is immutable")
+
+ def __eq__(self, other):
+ return (self.rank.symbol == other.rank.symbol and
+ self.suit.color == other.suit.color)
+
+ def __str__(self):
+ card_string = '{} of {}'.format(self.rank.card_property,
+ self.suit.card_property)
+ return str(card_string)
+
+ def __repr__(self):
+ card_string = '<Card {} of {}>'.format(self.rank.card_property,
+ self.suit.card_property)
+ return card_string
+
+
+class CardCollection(list):
+ collection = None
+
+ def add(self, card):
+ self.append(card)
+
+ def draw(self, index):
+ card = self[index]
+ del self[index]
+ return card
+
+ def top_card(self):
+ return self[-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def draw_from_top(self):
+ index = len(self) - 1
+ card = self[index]
+ del self[index]
+ return card
+
+ def draw_from_bottom(self):
+ card = self[0]
+ del self[0]
+ return card
+
+ def __str__(self, card):
+ card_string = ' Card {} of {}'.format(card.rank, card.suit)
+ return card_string
+
+
+def ClassFactory(name, card_property, BaseClass):
+
+ def __init__(self, **kwargs):
+ if name in SUITS_COLORS.keys():
+ BaseClass.__init__(self, card_property)
+ else:
+ BaseClass.__init__(self, card_property)
+
+ newclass = type(name, (BaseClass,), {"__init__": __init__})
+
+ newclass.card_property = card_property
+
+ return newclass
+
+
+def StandardDeck():
+ cards = []
+ for suit in SUITS.values():
+ for rank in RANKS.values():
+ card = Card(rank, suit)
+ cards.append(card)
+
+ return CardCollection(cards)
+
+
+def BeloteDeck():
+ cards = []
+
+ beloteranks = strip_cards()
+
+ for suit in SUITS.values():
+ for rank in beloteranks.values():
+ card = Card(rank, suit)
+ cards.append(card)
+
+ return CardCollection(cards)
+
+
+def SixtySixDeck():
+ cards = []
+
+ sixty_sixranks = strip_cards()
+ sixty_sixranks.pop('Seven', None)
+ sixty_sixranks.pop('Eight', None)
+
+ for suit in SUITS.values():
+ for rank in sixty_sixranks.values():
+ card = Card(rank, suit)
+ cards.append(card)
+
+ return CardCollection(cards)
+
+
+def strip_cards():
+ newranks = OrderedDict(RANKS)
+ newranks.pop('Two', None)
+ newranks.pop('Three', None)
+ newranks.pop('Four', None)
+ newranks.pop('Five', None)
+ newranks.pop('Six', None)
+ return newranks
+
+
+RANK_KEYS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six',
+ 'Five', 'Four', 'Three', 'Two', 'Ace']
+SUITS_KEYS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+SUITS_COLORS = {'Diamonds': 'red',
+ 'Hearts': 'red',
+ 'Spades': 'black',
+ 'Clubs': 'black'
+ }
+
+
+RANK_SYMBOLS = {'King': 'K',
+ 'Queen': 'Q',
+ 'Jack': 'J',
+ 'Ten': '10',
+ 'Nine': '9',
+ 'Eight': '8',
+ 'Seven': '7',
+ 'Six': '6',
+ 'Five': '5',
+ 'Four': '4',
+ 'Three': '3',
+ 'Two': '2',
+ 'Ace': 'A'
+ }
+
+for rank in RANK_KEYS:
+ cardrank = ClassFactory(rank, rank, Rank)
+ RANKS[rank] = cardrank
+
+for suit in SUITS_KEYS:
+ cardsuit = ClassFactory(suit, suit, Suit)
+ SUITS[suit] = cardsuit