Ангел обнови решението на 24.03.2014 20:55 (преди над 10 години)
+ranks = {'Four' : '4', 'Queen': 'Q', 'Nine': '9', 'Eight': '8', 'Jack': 'J',
+ 'Three': '3', 'Two' : '2', 'Six' : '6', 'Seven': '7', 'Ten' : '10',
+ 'King' : 'K', 'Five' : '5', 'Ace' : 'A'}
+
+suits = {'Hearts': 'red', 'Clubs': 'black', 'Spades': 'black', 'Diamonds': 'red'}
+
+
+class Rank:
+ def __init__(self):
+ self.symbol = None
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.symbol == other.symbol
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+
+
+class Suit:
+ def __init__(self):
+ self.color = None
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return isinstance(other, self.__class__) and self.color == other.color
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+
+RANKS = {rank: type(rank, (Rank,), dict(symbol=ranks[rank])) for rank in ranks}
+SUITS = {suit: type(suit, (Suit,), dict(color=suits[suit])) for suit in suits}
+
+
+class Card:
+ def __init__(self, _rank, _suit):
+ self.card = (_rank(), _suit())
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ @property
+ def rank(self):
+ return self.card[0]
+
+ @property
+ def suit(self):
+ return self.card[1]
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ if collection is None:
+ self.deck = []
+ else:
+ self.deck = list(collection)
+
+ def __getitem__(self, key):
+ return self.deck[key]
+
+ def __iter__(self):
+ for card in self.deck:
+ yield card
+
+ def __len__(self):
+ return len(self.deck)
+
+ def draw(self, index):
+ return self.deck.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.deck[-1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ return self.deck.index(card)
+
+ranks_hierarchy = ['King' , 'Queen', 'Jack', 'Ten' , 'Nine' , 'Eight',
+ 'Seven', 'Six' , 'Five', 'Four', 'Three', 'Two' , 'Ace']
+
+suits_hierarchy = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+def StandardDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def BeloteDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy[:7] + [ranks_hierarchy[-1]]:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def SixtySixDeck():
+ result = []
+ for suit in suits_hierarchy:
+ for rank in ranks_hierarchy[:5] + [ranks_hierarchy[-1]]:
+ result.append(Card(RANKS[rank], SUITS[suit]))
+ return result