Диана обнови решението на 24.03.2014 22:18 (преди над 10 години)
+class Rank:
+ symbol = None
+
+
+class Suit:
+ symbol = None
+
+
+class Operator:
+ def __eq__(self, second):
+ return self.symbol == second.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Two(Rank, Operator):
+ symbol = '2'
+
+
+class Three(Rank, Operator):
+ symbol = '3'
+
+
+class Four(Rank, Operator):
+ symbol = '4'
+
+
+class Five(Rank, Operator):
+ symbol = '5'
+
+
+class Six(Rank, Operator):
+ symbol = '6'
+
+
+class Seven(Rank, Operator):
+ symbol = '7'
+
+
+class Eight(Rank, Operator):
+ symbol = '8'
+
+
+class Nine(Rank, Operator):
+ symbol = '9'
+
+
+class Ten(Rank, Operator):
+ symbol = '10'
+
+
+class Jack(Rank, Operator):
+ symbol = 'J'
+
+
+class Queen(Rank, Operator):
+ symbol = 'Q'
+
+
+class King(Rank, Operator):
+ symbol = 'K'
+
+
+class Ace(Rank, Operator):
+ symbol = 'A'
+
+
+class Diamonds(Suit, Operator):
+ color = 'red'
+
+
+class Hearts(Suit, Operator):
+ color = 'red'
+
+
+class Spades(Suit, Operator):
+ color = 'black'
+
+
+class Clubs(Suit, Operator):
+ color = 'black'
+
+SCHNAPSEN_DECK = [Ace, King, Queen, Jack, Ten, Nine]
+BELOTE_DECK = [King, Queen, Jack, Ten, Nine, Eight, Seven]
+STANDARD_DECK = [King, Queen, Jack, Ten, Nine, Eight,
+ Seven, Six, Five, Four, Three, Two, Ace]
+ALL_SUITS = [Diamonds, Hearts, Spades, Clubs]
+
+RANKS = {rank.__name__: rank for rank in STANDARD_DECK}
+SUITS = {suit.__name__: suit for suit in ALL_SUITS}
+
+
+class Card:
+
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return "{} of {}".format(str(self.rank), str(self.suit))
+
+ def __eq__(self, second):
+ return self.rank == second.rank and self.suit == second.suit
+
+
+class CardCollection:
+
+ def __init__(self, collection=[]):
+ self.cards = list(collection)
+
+ def draw(self, index):
+ return self.cards.pop(index)
+
+ def draw_from_top(self):
+ return self.cards.pop()
+
+ def draw_from_bottom(self):
+ return self.cards.pop(0)
+
+ def top_card(self):
+ return self.cards[-1]
+
+ def bottom_card(self):
+ return self.cards[0]
+
+ def add(self, card):
+ self.cards.append(card)
+
+ def index(self, card):
+ return self.cards.index(card)
+
+ def __setitem__(self, index, card):
+ self.cards[index] = card
+
+ def __getitem__(self, index):
+ return self.cards[index]
+
+ def __len__(self):
+ return len(self.cards)
+
+ def __iter__(self):
+ for card in self.cards:
+ yield card
+
+ def __str__(self):
+ card_str = []
+ for card in self.cards:
+ card_str.append(str(card))
+ return', '.join(card_str)
+
+
+def full_deck(ranks):
+ deck = CardCollection([])
+ for suit in ALL_SUITS:
+ for rank in ranks:
+ deck.add(Card(rank, suit))
+ return deck
Идеята да използваш mixin Operator не е много подходяща в случая. По-скоро изнеси дъндър методите в базовия клас Rank
. Помисли си за начин, по който можеш динамично да конструираш подкласовете. Hint: това става с използването на type
.