Ирина обнови решението на 26.03.2014 15:07 (преди над 10 години)
+RANKS_AND_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']]
+
+
+SUITS_AND_COLORS = [['Diamonds', 'red'], ['Clubs', 'black'],
+ ['Hearts', 'red'], ['Spades', 'black']]
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+
+def ClassFactory(name, value, base_class):
+ def __init__(self):
+ base_class.__init__(self, value)
+ return type(name, (base_class,), {"__init__": __init__})
+
+
+RANKS = {x[0]: ClassFactory(x[0], x[1], Rank) for x in RANKS_AND_SYMBOLS}
+SUITS = {x[0]: ClassFactory(x[0], x[1], Suit) for x in SUITS_AND_COLORS}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return '{} of {}'.format(self.rank, self.suit)
+
+ def __repr__(self):
+ return "<Card {}>".format(str(self))
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return "[{}]".format(", ".join(repr(x) for x in self.collection))
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(len(self) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.collection[len(self) - 1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ return self.collection.index(card)
+
+
+def StandartDeck():
+ return CardCollection([Card(RANKS[rank[0]], SUITS[suit[0]])
+ for suit in SUITS_AND_COLORS
+ for rank in RANKS_AND_SYMBOLS])
+
+
+def BeloteDeck():
+ return CardCollection([x for x in StandartDeck()
+ if not x.rank._Rank__symbol.isdigit()
+ or (x.rank._Rank__symbol.isdigit()
+ and int(x.rank._Rank__symbol) > 6)])
+
+
+def SixtySixDeck():
+ return CardCollection([x for x in StandartDeck()
+ if not x.rank._Rank__symbol.isdigit()
+ or (x.rank._Rank__symbol.isdigit()
+ and int(x.rank._Rank__symbol) > 8)])