Божидар обнови решението на 22.03.2014 16:26 (преди над 10 години)
+from collections import deque
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+RANKS_LIST = [King, Queen, Jack, Ten, Nine, Eight,
+ Seven, Six, Five, Four, Three, Two, Ace]
+RANKS = {str(rank()): rank for rank in RANKS_LIST}
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+SUITS_LIST = [Diamonds, Clubs, Hearts, Spades]
+SUITS = {str(suit()): suit for suit in SUITS_LIST}
+
+
+class Card:
+ def __setattr__(self, *args):
+ raise TypeError('can\'t set attribute')
+
+ def __delattr__(self, *args):
+ raise TypeError('can\'t delete attribute')
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ if collection is not None:
+ self.__card_buffer = deque(collection)
+ else:
+ self.__card_buffer = deque()
+
+ def __len__(self):
+ return len(self.__card_buffer)
+
+ def __getitem__(self, index):
+ return self.__card_buffer[index]
+
+ def __str__(self):
+ representation = "["
+ for i in range(len(self.__card_buffer)-1):
+ representation += str(self.__card_buffer[i]) + ", "
+ representation += str(self.__card_buffer[-1]) + "]"
+ return representation
+
+ def draw(self, index):
+ drawn = self[index]
+ self.__card_buffer.remove(drawn)
+ return drawn
+
+ def draw_from_top(self):
+ return self.__card_buffer.pop()
+
+ def draw_from_bottom(self):
+ return self.__card_buffer.popleft()
+
+ def top_card(self):
+ return self.__card_buffer[-1]
+
+ def bottom_card(self):
+ return self.__card_buffer[0]
+
+ def add(self, card):
+ self.__card_buffer.append(card)
+
+ def index(self, card):
+ for i, element in enumerate(self.__card_buffer):
+ if card == element:
+ return i
+ raise ValueError("<Card {}> is not in the list".format(card))
+
+
+def StandardDeck():
+ return CardCollection(Card(rank, suit) for suit in SUITS_LIST
+ for rank in RANKS_LIST)
+
+
+def BeloteDeck():
+ def is_belote_card(card):
+ return card.rank not in [rank() for rank in RANKS_LIST[7:12]]
+ return CardCollection(card for card in StandardDeck()
+ if is_belote_card(card))
+
+
+def SixtySixDeck():
+ def is_sixty_six_card(card):
+ return card.rank not in [rank() for rank in RANKS_LIST[5:12]]
+ return CardCollection(card for card in StandardDeck()
+ if is_sixty_six_card(card))
- Възползвай се от това, че наследяваш клас и не дефинирай
__init__
във всеки наследник - Защо правиш това
object.__setattr__(self, 'rank', rank())
? В python си има синтаксис за тази операция -
CardCollection.__str__
методът е сравнително нечетим. Виж документацията наstr.format