Мария обнови решението на 23.03.2014 21:24 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol, order=0):
+ self.symbol = symbol
+ self.order = order
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return self.symbol == other.symbol
+ else:
+ return False
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A', 1)
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2', 2)
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3', 3)
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4', 4)
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5', 5)
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6', 6)
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7', 7)
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8', 8)
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9', 9)
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10', 10)
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J', 11)
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q', 12)
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K', 13)
+
+
+RANKS = {subclass().__class__.__name__: subclass for subclass
+ in Rank.__subclasses__()}
+
+
+class Suit:
+ def __init__(self, color, order=0):
+ self.color = color
+ self.order = order
+
+ @classmethod
+ def __str__(cls):
+ return str(cls.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return self.color == other.color
+ else:
+ return False
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red', 3)
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black', 2)
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red', 1)
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black', 4)
+
+
+SUITS = {subclass().__class__.__name__: subclass for subclass
+ in Suit.__subclasses__()}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank.__class__.__name__ + ' of '
+ + self.suit.__class__.__name__)
+
+ def __eq__(self, other):
+ if isinstance(other, self.__class__):
+ return (self.rank == other.rank and
+ self.suit == other.suit)
+ else:
+ return False
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, i):
+ return (self.collection)[i]
+
+ def add(self, card):
+ (self.collection).append(card)
+
+ def draw(self, index):
+ card = (self.collection)[index]
+ (self.collection).remove(card)
+ return str(card)
+
+ def draw_from_bottom(self):
+ card = (self.collection)[0]
+ (self.collection).remove(card)
+ return str(card)
+
+ def draw_from_top(self):
+ last_position = len(self.collection) - 1
+ card = (self.collection)[last_position]
+ (self.collection).remove(card)
+ return str(card)
+
+ def bottom_card(self):
+ return str((self.collection)[0])
+
+ def top_card(self):
+ last_position = len(self.collection) - 1
+ return str((self.collection)[last_position])
+
+ def index(self, card):
+ index = -1
+ card_rank = card.rank.__class__
+ card_suit = card.suit.__class__
+ for item in range(len(self.collection)):
+ current_card_rank = (self.collection)[item].rank.__class__
+ current_card_suit = (self.collection)[item].suit.__class__
+ index += 1
+ if (current_card_rank == card_rank and
+ current_card_suit == card_suit):
+ return index
+ raise ValueError("<Card {0}> is not in list".
+ format(str(card)))
+
+
+def get_sorted_ranks():
+ ranks = [subclass for subclass in Rank.__subclasses__()]
+ ranks.sort(key=lambda x: -(x().order))
+ ranks = [rank().__class__.__name__ for rank in ranks]
+ return ranks
+
+
+def get_sorted_suits():
+ suits = [subclass for subclass in Suit.__subclasses__()]
+ suits.sort(key=lambda x: x().order)
+ suits = [suit().__class__.__name__ for suit in suits]
+ return suits
+
+
+def StandardDeck():
+ standard_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ for suit in suits:
+ for rank in ranks:
+ standard_deck.append('Card {0} of {1}'.format(rank, suit))
+ return standard_deck
+
+
+def BeloteDeck():
+ belote_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ end = 8
+ for suit in suits:
+ for i in range(end):
+ belote_deck.append('Card {0} of {1}'.format(ranks[i], suit))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = []
+ ranks = get_sorted_ranks()
+ suits = get_sorted_suits()
+ end = 6
+ for suit in suits:
+ for i in range(end):
+ sixty_six_deck.append('Card {0} of {1}'.format(ranks[i], suit))
+ return sixty_six_deck
Прочети пак условието. От StandardDeck и приятелите му се изисква да връщаш инстанция на CardCollection, който съдържа съответните инстанции на класа Card.
В момента връщаш масив от низове. Причината, поради която изглежда така в примера ми е, че съм предефинирал метода __repr__
.