Иван обнови решението на 24.03.2014 23:54 (преди над 10 години)
+from collections import OrderedDict
+import itertools
+
+
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+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')
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+ self.suit = self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.suit == other.suit
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+RANKS = OrderedDict([('King', type(King())),
+ ('Queen', type(Queen())),
+ ('Jack', type(Jack())),
+ ('Ten', type(Ten())),
+ ('Nine', type(Nine())),
+ ('Eight', type(Eight())),
+ ('Seven', type(Seven())),
+ ('Six', type(Six())),
+ ('Five', type(Five())),
+ ('Four', type(Four())),
+ ('Three', type(Three())),
+ ('Two', type(Two())),
+ ('Ace', type(Ace()))])
+
+
+SIXTY_SIX_RANKS = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine']
+
+
+BELOTE_RANKS = SIXTY_SIX_RANKS + ['Eight', 'Seven']
+
+
+SUITS = OrderedDict([('Diamonds', type(Diamonds())), ('Clubs', type(Clubs())),
+ ('Hearts', type(Hearts())), ('Spades', type(Spades()))])
+
+
+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 self._suit == other._suit and self._rank == other._rank
+
+ def __str__(self):
+ return "{0} of {1}".format(str(self._rank()), str(self._suit()))
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.cards = []
+ for item in collection:
+ self.cards.append(item)
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __getitem__(self, i):
+ return self.cards[i]
+
+ def draw(self, index):
+ return str(self.cards.pop(index))
+
+ def draw_from_top(self):
+ return str(self.cards.pop())
+
+ def draw_from_bottom(self):
+ return str(self.cards.pop(0))
+
+ def top_card(self):
+ return str(self.cards[len(self.cards)-1])
+
+ def bottom_card(self):
+ return str(self.cards[0])
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ for i in range(len(self.cards)):
+ if card == self.cards[i]:
+ return i
+ raise ValueError('{0} is not in list'.format(str(card)))
+
+
+def StandardDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values()]
+ standard_deck = CardCollection(self_deck)
+ return standard_deck
+
+
+def BeloteDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values() if str(rank()) in BELOTE_RANKS]
+ belote_deck = CardCollection(self_deck)
+ return belote_deck
+
+
+def SixtySixDeck():
+ self_deck = [Card(rank, suit) for suit in SUITS.values()
+ for rank in RANKS.values() if str(rank()) in SIXTY_SIX_RANKS]
+ sixty_six_deck = CardCollection(self_deck)
+ return sixty_six_deck
Това не е най-добрия начин да зададеш стойност на атрибут: Rank.__init__(self, '2')
. Можеш просто да го сложиш към self
. Също така помисли за вариант динамично да дефинираш подкласовете на Suit
и Rank
. Hint: за целта можеш да ползваш type
.