Мария обнови решението на 26.03.2014 16:29 (преди над 10 години)
+import collections
+class Rank:
+ @classmethod
+ def __eq__(cls, other):
+ return cls == other
+
+ @classmethod
+ def __str__(cls):
+ return cls.__name__
+
+
+class Suit:
+ @classmethod
+ def __eq__(cls, other):
+ return cls == other
+
+ @classmethod
+ def __str__(cls):
+ return cls.__name__
+
+
+SYMBOLS = {
+ 'Two': '2',
+ 'Three': '3',
+ 'Four': '4',
+ 'Five': '5',
+ 'Six': '6',
+ 'Seven': '7',
+ 'Eight': '8',
+ 'Nine': '9',
+ 'Ten': '10',
+ 'Jack': 'J',
+ 'Queen': 'Q',
+ 'King': 'K',
+ 'Ace': 'A',
+}
+
+RANK_KEYS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+SUIT_KEYS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+COLORS = {'Diamonds': 'red', 'Clubs': 'black',
+ 'Hearts': 'red', 'Spades' : 'black'}
+
+RANKS = {name: type(name, (Rank, object), {'symbol': symbol}) for name, symbol in SYMBOLS.items()}
+
+SUITS = {name: type(name, (Suit, object), {'color': color}) for name, color in COLORS.items()}
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self,index):
+ return self.collection[index]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ return self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+
+
+def StandardDeck():
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in RANK_KEYS])
+
+
+def BeloteDeck():
+ rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in rank_names])
+
+
+def SixtySixDeck():
+ rank_names = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ return CardCollection([Card(RANKS[rank_name], SUITS[suit_name])
+ for suit_name in SUIT_KEYS
+ for rank_name in rank_names])
+# class Card:
+# def __init__(self, rank, suit):
+# self.__rank, self.__suit = rank, suit
+# @property
+# def rank(self):
+# return self.__rank
+# @property
+# def suit(self):
+# return self.__suit
+
+# Card = collections.namedtuple('Card',['rank', 'suit'])
+
+class Card(tuple):
+ def __new__(cls, rank, suit):
+ return tuple.__new__(cls, (rank(), suit()))
+
+ @property
+ def rank(self):
+ return self[0]
+
+ @property
+ def suit(self):
+ return self[1]
+
+ def __str__(self):
+ return type(self[0]).__name__ + ' of ' + type(self[1]).__name__
+
+ def __setattr__(self, *ignored):
+ raise AttributeError
+
+ def __delattr__(self, *ignored):
+ raise AttributeError