Дарина обнови решението на 23.03.2014 15:32 (преди над 10 години)
+class Rank:
+ symbol = ''
+
+ def __repr__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Suit:
+ color = ''
+
+ def __repr__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+
+class Ace(Rank):
+ symbol = 'A'
+
+
+class King(Rank):
+ symbol = 'K'
+
+
+class Queen(Rank):
+ symbol = 'Q'
+
+
+class Jack(Rank):
+ symbol = 'J'
+
+
+class Ten(Rank):
+ symbol = '10'
+
+
+class Nine(Rank):
+ symbol = '9'
+
+
+class Eight(Rank):
+ symbol = '8'
+
+
+class Seven(Rank):
+ symbol = '7'
+
+
+class Six(Rank):
+ symbol = '6'
+
+
+class Five(Rank):
+ symbol = '5'
+
+
+class Four(Rank):
+ symbol = '4'
+
+
+class Three(Rank):
+ symbol = '3'
+
+
+class Two(Rank):
+ symbol = '2'
+
+
+class Spades(Suit):
+ color = 'black'
+
+
+class Clubs(Suit):
+ color = 'black'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+RANKS = {'Ace': Ace, 'King': King, 'Queen': Queen, 'Jack': Jack, 'Ten':
+ Ten, 'Nine': Nine, 'Eight': Eight, 'Seven': Seven, 'Six': Six,
+ 'Five': Five, 'Four': Four, 'Three': Three, 'Two': Two}
+
+
+SUITS = {'Spades': Spades, 'Clubs': Clubs, 'Diamonds': Diamonds, 'Hearts':
+ Hearts}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __repr__(self):
+ return "{0} of {1}".format(str(self.rank), str(self.suit))
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __setattr__(self):
+ raise "Can't set attribute"
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.cards = list(collection)
+
+ def draw(self, index):
+ drawn_card = self.cards.pop(index)
+ return drawn_card
+
+ def draw_from_top(self):
+ drawn_card = self.cards.pop()
+ return drawn_card
+
+ def draw_from_bottom(self):
+ drawn_card = self.cards.pop(0)
+ return drawn_card
+
+ def top_card(self):
+ card_on_top = self.cards[len(self.cards) - 1]
+ return card_on_top
+
+ def bottom_card(self):
+ card_on_bottom = self.cards[0]
+ return card_on_bottom
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ return self.cards.index(card)
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __repr__(self):
+ cards_in_deck = "[ <Card {0}>".format(str(self.cards[0]))
+ for index in range(1, len(self.cards)):
+ cards_in_deck += ", <Card {0}>".format(str(self.cards[index]))
+ cards_in_deck += " ]"
+ return cards_in_deck
+
+RANKS_IN_ORDER = ('King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace')
+
+
+SUITS_IN_ORDER = ('Diamonds', 'Clubs', 'Hearts', 'Spades')
+
+
+RANKS_NOT_IN_BELOTE_DECK = ('Six', 'Five', 'Four', 'Three', 'Two')
+
+
+RANKS_NOT_IN_SIXTY_SIX_DECK = ('Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two')
+
+
+def StandardDeck():
+ deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ if(rank_name not in RANKS_NOT_IN_BELOTE_DECK):
+ belote_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ for suit_name in SUITS_IN_ORDER:
+ for rank_name in RANKS_IN_ORDER:
+ if(rank_name not in RANKS_NOT_IN_SIXTY_SIX_DECK):
+ sixty_six_deck.add(Card(RANKS[rank_name], SUITS[suit_name]))
+ return sixty_six_deck
Решението ти е валидно. Но:
- Защо използваш
__setattr__
, след като имаш синтаксис за това? - Функциите на трите тестета са ти абсолютно еднакви. Трябва да намериш начин да ги генерализираш :)
- Не ми харесва как си имплементирала
__repr__
. Не конкратенирай стрингове
П.П.: Още веднъж, извинения за късния отговор :)