Драгомир обнови решението на 24.03.2014 21:46 (преди над 10 години)
+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
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+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}
+
+
+SUITS = {'Spades': Spades, 'Hearts': Hearts,
+ 'Diamonds': Diamonds, 'Clubs': Clubs}
+
+
+class Card: # to make it immutable
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __repr__(self):
+ return '<Card ' + str(self.rank) + ' of ' + str(self.suit) + '>'
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ self.deck = []
+ if collection is not None:
+ for card in collection:
+ self.deck.append(card)
+
+ def __getitem__(self, index):
+ return self.deck[index]
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __repr__(self):
+ return str([repr(card) for card in self.deck])
+
+ def draw(self, index):
+ card = self.deck[index]
+ del self.deck[index]
+ return card
+
+ def draw_from_top(self):
+ card = self.deck[-1]
+ del self.deck[-1]
+ return card
+
+ def draw_from_bottom(self):
+ card = self.deck[0]
+ del self.deck[0]
+ return card
+
+ 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))
+
+
+def StandardDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def BeloteDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck
+
+
+def SixtySixDeck():
+ deck = CardCollection()
+ for suit in ['Diamonds', 'Clubs', 'Hearts', 'Spades']:
+ for rank in ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']:
+ deck.add(Card(RANKS[rank], SUITS[suit]))
+ return deck