Милица обнови решението на 22.03.2014 01:04 (преди над 10 години)
+SYMBOLS_AND_RANKS = {'A': 'Ace', 'K': 'King', 'Q': 'Queen', 'J': 'Jack',
+ '10': 'Ten', '9': 'Nine', '8': 'Eight', '7': 'Seven',
+ '6': 'Six', '5': 'Five', '4': 'Four', '3': 'Three',
+ '2': 'Two'}
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+ self.rank = SYMBOLS_AND_RANKS[self.symbol]
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.suit = 'Diamonds'
+
+ def __str__(self):
+ return self.suit
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.suit = 'Hearts'
+
+ def __str__(self):
+ return self.suit
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.suit = 'Clubs'
+
+ def __str__(self):
+ return self.suit
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.suit = 'Spades'
+
+ def __str__(self):
+ return self.suit
+
+
+RANK_CLASSES = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,
+ Ten, Jack, Queen, King]
+
+
+SUIT_CLASSES = [Diamonds, Hearts, Clubs, Spades]
+
+
+RANKS = {rank_class().rank: rank_class for rank_class in RANK_CLASSES}
+
+
+SUITS = {suit_class().suit: suit_class for suit_class in SUIT_CLASSES}
+
+
+class Card():
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+
+ __delattr__ = __setattr__
+
+ def __init__(self, Rank, Suit):
+ super(Card, self).__setattr__('rank', Rank())
+ super(Card, self).__setattr__('suit', Suit())
+
+ def __eq__(self, other):
+ if type(other) is type(self):
+ return self.__dict__ == other.__dict__
+ return False
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __lt__(self, other):
+ if SUIT_CLASSES.index(type(self.suit)) < \
+ SUIT_CLASSES.index(type(other.suit)):
+ return True
+ elif SUIT_CLASSES.index(type(self.suit)) == \
+ SUIT_CLASSES.index(type(other.suit)):
+ return RANK_CLASSES.index(type(self.rank)) > \
+ RANK_CLASSES.index(type(other.rank))
+ else:
+ return False
+
+ def __gt__(self, other):
+ return not self < other and self != other
+
+
+class CardCollection:
+ def __init__(self, collection = []):
+ if type(collection) == dict:
+ self.deck = [(item, collection[item]) for item in collection]
+ else:
+ self.deck = list(collection)
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __getitem__(self, key):
+ if key >= len(self):
+ raise IndexError("list index out of range")
+ else:
+ return self.deck[key]
+
+ def draw(self, index):
+ removed_card = self.deck[index]
+ self.deck.remove(self.deck[index])
+ return removed_card
+
+ def draw_from_top(self):
+ return self.draw(len(self) - 1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ def top_card(self):
+ return self.deck[len(self) - 1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ if card not in self.deck:
+ raise ValueError("< Card " + card + " > is not list")
+ else:
+ return self.deck.index(card)
+
+ def __str__(self):
+ return str(self.deck)
+
+ def __repr__(self):
+ return str(self)
+
+
+def StandartDeck():
+ deck = [Card(rank, suit) for rank in RANKS.values()
+ for suit in SUITS.values()]
+ deck.sort()
+ return CardCollection(deck)
+
+
+def BeloteDeck():
+ return [card for card in StandartDeck() if
+ RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Six) or
+ type(card.rank) == Ace]
+
+
+def SixtySixDeck():
+ return [card for card in StandartDeck() if
+ RANK_CLASSES.index(type(card.rank)) > RANK_CLASSES.index(Eight) or
+ type(card.rank) == Ace]