Цветелина обнови решението на 21.03.2014 00:55 (преди над 10 години)
+RANKS_SYMBOLS = ['K', 'Q', 'J', '10', '9', '8',
+ '7', '6', '5', '4', '3', '2', 'A']
+
+
+ORDERED_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+
+ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class CardElement:
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == self.__class__
+
+
+class Suit(CardElement):
+
+ def __init__(self):
+ if ORDERED_SUITS.index(self.__class__.__name__) % 2:
+ self.__color = 'red'
+ else:
+ self.__color = 'black'
+
+
+class Rank(CardElement):
+
+ def __init__(self):
+ index = ORDERED_RANKS.index(self.__class__.__name__)
+ self.__symbol = RANKS_SYMBOLS[index]
+
+
+class Two(Rank):
+ pass
+
+
+class Three(Rank):
+ pass
+
+
+class Four(Rank):
+ pass
+
+
+class Five(Rank):
+ pass
+
+
+class Six(Rank):
+ pass
+
+
+class Seven(Rank):
+ pass
+
+
+class Eight(Rank):
+ pass
+
+
+class Nine(Rank):
+ pass
+
+
+class Ten(Rank):
+ pass
+
+
+class Ace(Rank):
+ pass
+
+
+class King(Rank):
+ pass
+
+
+class Queen(Rank):
+ pass
+
+
+class Jack(Rank):
+ pass
+
+
+class Hearts(Suit):
+ pass
+
+
+class Clubs(Suit):
+ pass
+
+
+class Spades(Suit):
+ pass
+
+
+class Diamonds(Suit):
+ pass
+
+
+RANKS = {klass: eval(klass) for klass in ORDERED_RANKS}
+
+
+SUITS = {klass: eval(klass) for klass in ORDERED_SUITS}
+
+
+class Card:
+
+ def __init__(self, rank_class, suit_class):
+ self.__rank = rank_class()
+ self.__suit = suit_class()
+
+ def __str__(self):
+ return "{0} of {1}".format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.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 draw(self, index):
+ return self.__collection.pop(index)
+
+ def draw_from_top(self):
+ return self.__collection.pop(-1)
+
+ 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):
+ self.__collection.append(card)
+
+ def index(self, card):
+ return self.__collection.index(card)
+
+ def __str__(self):
+ return str([str(card) for card in self.__collection])
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def __getitem__(self, index):
+ return self.__collection[index]
+
+ def __iter__(self):
+ return self.__collection.__iter__()
+
+
+def StandardDeck():
+ return [Card(eval(rank), eval(suit)) for suit in ORDERED_SUITS
+ for rank in ORDERED_RANKS]
+
+
+def BelotDeck():
+ ranks = ORDERED_RANKS[0:7:]
+ ranks.append('Ace')
+ return [Card(eval(rank), eval(suit)) for suit in ORDERED_SUITS[0:7:]
+ for rank in ranks]
+
+
+def SixtySixDeck():
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ return [Card(eval(rank), eval(suit)) for suit in ORDERED_SUITS[0:7:]
+ for rank in ranks]
Правилно си се ориентирала, че не е нужно да напишеш всички класове по отделно в колекцията, обаче Eval is evil. За целта можеш да използваш locals() a може и да съставиш самите класове с тази функция
Освен това се пише BeloteDeck :)
Благодаря за коментара :)