Теодора обнови решението на 24.03.2014 10:17 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Ace'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Two'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Three'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return 'King'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return isinstance(other, Hearts)
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return isinstance(other, Clubs)
+
+ def __str__(self):
+ return 'Clubs'
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return isinstance(other, Spades)
+
+ def __str__(self):
+ return 'Spades'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return isinstance(other, Diamonds)
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs, 'Spades': Spades,
+ 'Diamonds': Diamonds}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __setattr__(self, *args):
+ raise AttributeError('can\'t set attribute')
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return '{0} of {1}'.format(self.rank, self.suit)
+
+ def __repr__(self):
+ return '<Card {0} of {1}>'.format(self.rank, self.suit)
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = list(collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(len(self.collection) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ 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('{0} is not in list'.format(card))
+
+ def __getitem__(self, index):
+ if index >= 0 and index < len(self.collection):
+ return self.collection[index]
+ else:
+ raise IndexError('list index out of range')
+
+ def __len__(self):
+ return len(self.collection)
+
+
+RANKS_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+
+
+SUITS_ORDERED = [Diamonds, Clubs, Hearts, Spades]
+
+
+def StandardDeck():
+ return list(CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED
+ for rank in RANKS_ORDERED]))
+
+
+def BeloteDeck():
+ return list(CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED
+ for rank in RANKS_ORDERED
+ if rank().symbol not in
+ {'2', '3', '4', '5', '6'}]))
+
+
+def SixtySixDeck():
+ return list(CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED
+ for rank in RANKS_ORDERED
+ if rank().symbol not in
+ {'2', '3', '4', '5', '6', '7', '8'}]))