Стоян обнови решението на 23.03.2014 15:59 (преди над 10 години)
+
+
+class Rank:
+ symbol = ''
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __repr__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ color = ''
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __repr__(self):
+ return self.__class__.__name__
+
+
+class Two(Rank):
+ symbol = '2'
+
+
+class Three(Rank):
+ symbol = '3'
+
+
+class Four(Rank):
+ symbol = '4'
+
+
+class Five(Rank):
+ symbol = '5'
+
+
+class Six(Rank):
+ symbol = '6'
+
+
+class Seven(Rank):
+ symbol = '7'
+
+
+class Eight(Rank):
+ symbol = '8'
+
+
+class Nine(Rank):
+ symbol = '9'
+
+
+class Ten(Rank):
+ symbol = '10'
+
+
+class Jack(Rank):
+ symbol = 'J'
+
+
+class Queen(Rank):
+ symbol = 'Q'
+
+
+class King(Rank):
+ symbol = 'K'
+
+
+class Ace(Rank):
+ symbol = 'A'
+
+
+class Diamonds(Suit):
+ color = 'red'
+
+
+class Hearts(Suit):
+ color = 'red'
+
+
+class Spades(Suit):
+ color = 'black'
+
+
+class Clubs(Suit):
+ color = '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 = {'Diamonds': Diamonds, 'Hearts': Hearts,
+ 'Spades': Spades, 'Clubs': Clubs}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, 'rank', rank())
+ object.__setattr__(self, 'suit', suit())
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __repr__(self):
+ return '{0} of {1}'.format(str(self.rank), str(self.suit))
+
+ def __setattr__(self, name, value):
+ raise TypeError('can\'t set attribute')
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.collection = list(collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.pop(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):
+ return self.collection.index(card)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __repr__(self):
+ return ', '.join(['Card ' + str(card) for card in self.collection])
+
+
+ORDERED_RANKS = (King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace)
+ORDERED_SUITS = (Diamonds, Clubs, Hearts, Spades)
+
+
+def CreateDeck(not_included):
+ deck = []
+ for suit in ORDERED_SUITS:
+ for rank in ORDERED_RANKS:
+ if rank not in not_included:
+ deck.append(Card(rank, suit))
+
+ return CardCollection(deck)
+
+
+def StandardDeck():
+ return CreateDeck([])
+
+
+def BeloteDeck():
+ return CreateDeck([Six, Five, Four, Three, Two])
+
+
+def SixtySixDeck():
+ return CreateDeck([Eight, Seven, Six, Five, Four, Three, Two])
Решението ти е валидно. Но:
- Защо използваш
__setattr__
, след като имаш синтаксис за това?