Стефан обнови решението на 26.03.2014 16:22 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + self.__str__() + '>'
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self._cards = list(collection)
+
+ def __getitem__(self, index):
+ # TODO; add custom IndexError
+ return self._cards[index]
+
+ def __iter__(self):
+ return iter(self._cards)
+
+ def __len__(self):
+ return len(self._cards)
+
+ def __str__(self):
+ return str(self._cards)
+
+ def __repr__(self):
+ return str(self)
+
+ def draw(self, index):
+ return self._cards.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self._cards[-1]
+
+ def bottom_card(self):
+ return self._cards[0]
+
+ def add(self, card):
+ return self._cards.append(card)
+
+ def index(self, card):
+ # TODO: add custom ValueError
+ return self._cards.index(card)
+
+
+def StandardDeck():
+ deck = CardCollection([])
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def BeloteDeck():
+ deck = StandardDeck()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['Six', 'Five', 'Four', 'Three', 'Two']:
+ deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
+ return deck
+
+
+def SixtySixDeck():
+ deck = StandardDeck()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two']:
+ deck.draw(deck.index(Card(RANKS[rank], SUITS[suit])))
+ return deck
+
+RANKS = {str(King()): King, str(Queen()): Queen, str(Jack()): Jack,
+ str(Ten()): Ten, str(Nine()): Nine, str(Eight()): Eight,
+ str(Seven()): Seven, str(Six()): Six, str(Five()): Five,
+ str(Four()): Four, str(Three()): Three, str(Two()): Two,
+ str(Ace()): Ace}
+
+SUITS = {str(Diamonds()): Diamonds, str(Hearts()): Hearts,
+ str(Spades()): Spades, str(Clubs()): Clubs}
Този път малко я проспах задачата и за малко да изтърва срока. Видях няколко глупости, които в последствие май оправих:
- На 128-ми ред
self.__str__()
може да се замени просто съсstr(self)
. - Мога веднъж да декларирам валидните стойности и бои в правилния ред и да ги ползвам направо при създаването на класовете за картите (
Jack
,Hearts
и.т.н.) и трите функции за генерация на стандартни тестета. -
CardCollection
може просто да наследяваlist
.
Изглежда ти си единственият който се е сетил че може да се наследи list
.