Марио обнови решението на 26.03.2014 02:20 (преди над 10 години)
+from collections import OrderedDict
+from functools import reduce
+
+
+class Rank:
+
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+# Attention : Ace of Hacks
+RANKS_NAMES = OrderedDict([("A", "Ace"), ("2", "Two"), ("3", "Three"),
+ ("4", "Four"), ("5", "Five"), ("6", "Six"),
+ ("7", "Seven"), ("8", "Eight"), ("9", "Nine"),
+ ("10", "Ten"), ("J", "Jack"), ("Q", "Queen"),
+ ("K", "King")])
+
+RANKS_ORD = OrderedDict()
+for symbol in RANKS_NAMES.keys():
+ RANKS_ORD[RANKS_NAMES.get(symbol)] = type(
+ RANKS_NAMES.get(symbol), (Rank,),
+ dict(__init__=lambda self, symbol=symbol:
+ Rank.__init__(self, symbol)))
+
+RANKS_ORD = OrderedDict(reversed(list(RANKS_ORD.items())))
+RANKS = dict(RANKS_ORD)
+
+
+class Suit:
+
+ def __init__(self, color):
+ self.__color = color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+SUITS_NAMES = OrderedDict(
+ [("Diamonds", "red"), ("Clubs", "black"), ("Hearts", "red"),
+ ("Spades", "black")])
+SUITS_ORD = OrderedDict()
+for name in SUITS_NAMES.keys():
+ SUITS_ORD[name] = type(name, (Suit,),
+ dict(__init__=lambda self,
+ color=SUITS_NAMES.get(name):
+ Suit.__init__(self, color)))
+SUITS = dict(SUITS_ORD)
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ def __str__(self):
+ return str(self.__rank) + " of " + str(self.__suit)
+
+ def __eq__(self, other):
+ return self.__rank == other.__rank and self.__suit == other.__suit
+
+ def __repr__(self):
+ return "<Card " + str(self.__rank) + " of " + str(self.__suit) + ">"
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+class CardCollection: # Представлява колекция (тесте) от карти
+
+ def __init__(self, collection=[]):
+ self.__collection = collection
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def __getitem__(self, index):
+ return self.__collection[index]
+
+ def draw(self, index):
+ result = self.__collection[index]
+ del self.__collection[index]
+ return result
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def top_card(self):
+ return self.__collection[-1]
+
+ def bottom_card(self):
+ return self.__collection[0]
+
+ def add(self, card):
+ self.__collection.append(card)
+
+ def index(self, card):
+ for index in self.__collection:
+ if self.__collection[index] == card:
+ return index
+
+ def __repr__(self):
+ return reduce(lambda a, b: a + repr(b) +
+ ", " * (not b == self.__collection[-1]),
+ [x for x in self.__collection], "[") + "]"
+# str([repr(x) for x in self.__collection])
+
+
+def GenerateDeck(ranks_exclude={}):
+ deck = CardCollection()
+ for suit in SUITS_ORD:
+ for rank in RANKS_ORD:
+ if not RANKS_ORD[str(rank)] in ranks_exclude:
+ deck.add(Card(RANKS_ORD[str(rank)],
+ SUITS_ORD[str(suit)]))
+ return deck
+
+
+def StandardDeck():
+ return GenerateDeck({})
+
+RANKS_EXCLUDE_BELOTE = {
+ RANKS["Two"], RANKS["Three"], RANKS["Four"], RANKS["Five"], RANKS["Six"]}
+
+
+def BeloteDeck():
+ return GenerateDeck(RANKS_EXCLUDE_BELOTE)
+
+
+def SixtySixDeck():
+ RANKS_EXCLUDE_SIXTY_SIX = RANKS_EXCLUDE_BELOTE + \
+ [RANKS["Seven"], RANKS["Eight"]]
+ return GenerateDeck(RANKS_EXCLUDE_SIXTY_SIX)