Емануел обнови решението на 26.03.2014 16:48 (преди над 10 години)
+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.name
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return self.name
+
+
+class Two(Rank):
+ def __init__(self):
+ super().__init__('2')
+
+ name = 'Two'
+
+
+class Three(Rank):
+ def __init__(self):
+ super().__init__('3')
+
+ name = 'Three'
+
+
+class Four(Rank):
+ def __init__(self):
+ super().__init__('4')
+
+ name = 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ super().__init__('5')
+
+ name = 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ super().__init__('6')
+
+ name = 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ super().__init__('7')
+
+ name = 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ super().__init__('8')
+
+ name = 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ super().__init__('9')
+
+ name = 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ super().__init__('T')
+
+ name = 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ super().__init__('J')
+
+ name = 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ super().__init__('Q')
+
+ name = 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ super().__init__('K')
+
+ name = 'King'
+
+
+class Ace(Rank):
+ def __init__(self):
+ super().__init__('A')
+
+ name = 'Ace'
+
+
+RANKS = {'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
+ 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King, 'Ace': Ace}
+
+RANKS_ORDERED = OrderedDict([('King', King), ('Queen', Queen), ('Jack', Jack),
+ ('Ten', Ten), ('Nine', Nine), ('Eight', Eight),
+ ('Seven', Seven), ('Six', Six), ('Five', Five),
+ ('Four', Four), ('Three', Three), ('Two', Two),
+ ('Ace', Ace)])
+
+
+class Hearts(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+ name = 'Hearts'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ super().__init__('red')
+
+ name = 'Diamonds'
+
+
+class Spades(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+ name = 'Spades'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ super().__init__('black')
+
+ name = 'Clubs'
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Diamonds': Diamonds, 'Spades': Spades}
+
+SUITS_ORDERED = OrderedDict([('Diamonds', Diamonds), ('Clubs', Clubs),
+ ('Hearts', Hearts), ('Spades', 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._rank == other._rank and self._suit == other._suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return "<Card {}>".format(str(self))
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, i):
+ return self.collection[i]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def draw_from_top(self):
+ return self.draw(len(self.collection) - 1)
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ if card in self.collection:
+ return self.collection.index(card)
+ else:
+ raise ValueError('<Card {}> is not in list'.format(str(card)))
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def StandardDeck():
+ deck = []
+ for suit in SUITS_ORDERED:
+ for rank in RANKS_ORDERED:
+ deck.append(Card(RANKS[rank], SUITS[suit]))
+ return deck