Теодор обнови решението на 23.03.2014 22:42 (преди над 10 години)
+class Rank:
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ def __eq__(self, other):
+ return str(self) == str(other)
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Hearts(Suit):
+ colour = "red"
+
+
+class Spades(Suit):
+ colour = "black"
+
+
+class Diamonds(Suit):
+ colour = "red"
+
+
+class Clubs(Suit):
+ colour = "black"
+
+
+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"
+
+SUITS = {"Hearts": Hearts, "Spades": Spades,
+ "Diamonds": Diamonds, "Clubs": Clubs}
+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}
+
+
+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 __str__(self):
+ return ("{0} of {1}".format(self.rank, self.suit))
+
+ def __setattr__(self, name, value):
+ raise AttributeError("can't set attribute")
+
+ def __delattr__(self, name):
+ raise AttributeError("can't delete attribute")
+
+
+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.collection.pop(len(self.collection) - 1)
+
+ 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 __getitem__(self, position):
+ return self.collection[position]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ for card in self.collection:
+ yield card
+
+
+SUITS_ORDERED = [Diamonds, Clubs, Hearts, Spades]
+RANKS_ORDERED = [King, Queen, Jack, Ten, Nine, Eight, Seven, Six, Five, Four,
+ Three, Two, Ace]
+
+
+def StandardDeck():
+ standard_deck = [str(Card(rank, suit)) for suit in SUITS_ORDERED
+ for rank in RANKS_ORDERED]
+ return standard_deck
+
+
+def BeloteDeck():
+ belote_deck = [str(Card(RANKS_ORDERED[rank], suit))
+ for suit in SUITS_ORDERED for rank in range(0, 13)
+ if rank < 7 or rank > 11]
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixtysix_deck = [str(Card(RANKS_ORDERED[rank], suit))
+ for suit in SUITS_ORDERED for rank in range(0, 13)
+ if rank < 5 or rank > 11]
+ return sixtysix_deck
Последните ти три функции доста си приличат, помисли дали не можеш да се справиш в общата част, а и не е нужно да си запазваш резултата преди да го върнеш