Елица обнови решението на 20.03.2014 19:03 (преди над 10 години)
+#class Rank
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+#all 13 ranks of cards
+class Ace(Rank):
+ card_name = 'Ace'
+ card_position = 0
+ def __init__(self):
+ self.symbol = 'A'
+ def __str__(self):
+ return self.card_name
+
+class Two(Rank):
+ card_name = 'Two'
+ card_position = 1
+ def __init__(self):
+ self.symbol = '2'
+ def __str__(self):
+ return self.card_name
+
+class Three(Rank):
+ card_name = 'Three'
+ card_position = 2
+ def __init__(self):
+ self.symbol = '3'
+ def __str__(self):
+ return self.card_name
+
+class Four(Rank):
+ card_name = 'Four'
+ card_position = 3
+ def __init__(self):
+ self.symbol = '4'
+ def __str__(self):
+ return self.card_name
+
+class Five(Rank):
+ card_name = 'Five'
+ card_position = 4
+ def __init__(self):
+ self.symbol = '5'
+ def __str__(self):
+ return self.card_name
+
+class Six(Rank):
+ card_name = 'Six'
+ card_position = 5
+ def __init__(self):
+ self.symbol = '6'
+ def __str__(self):
+ return self.card_name
+
+class Seven(Rank):
+ card_name = 'Seven'
+ card_position = 6
+ def __init__(self):
+ self.symbol = '7'
+ def __str__(self):
+ return self.card_name
+
+class Eight(Rank):
+ card_name = 'Eight'
+ card_position = 7
+ def __init__(self):
+ self.symbol = '8'
+ def __str__(self):
+ return self.card_name
+
+class Nine(Rank):
+ card_name = 'Nine'
+ card_position = 6
+ def __init__(self):
+ self.symbol = '9'
+ def __str__(self):
+ return self.card_name
+
+class Ten(Rank):
+ card_name = 'Ten'
+ card_position = 9
+ def __init__(self):
+ self.symbol = '10'
+ def __str__(self):
+ return self.card_name
+
+class Jack(Rank):
+ card_name = 'Jack'
+ card_position = 10
+ def __init__(self):
+ self.symbol = 'J'
+ def __str__(self):
+ return self.card_name
+
+class Queen(Rank):
+ card_name = 'Queen'
+ card_position = 11
+ def __init__(self):
+ self.symbol = 'Q'
+ def __str__(self):
+ return self.card_name
+
+class King(Rank):
+ card_name = 'King'
+ card_position = 12
+ def __init__(self):
+ self.symbol = 'K'
+ def __str__(self):
+ return self.card_name
+
+#class Suit
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+#All four suits of cards
+class Clubs(Suit):
+ card_suit = 'Clubs'
+ suit_order = 1
+ def __init__(self):
+ self.color = 'black'
+ def __str__(self):
+ return self.card_suit
+ def __eq__ (self, other):
+ return self.card_suit == other.card_suit
+
+class Diamonds(Suit):
+ card_suit = 'Diamonds'
+ suit_order = 0
+ def __init__(self):
+ self.color = 'red'
+ def __str__(self):
+ return self.card_suit
+ def __eq__ (self, other):
+ return self.card_suit == other.card_suit
+
+class Hearts(Suit):
+ card_suit = 'Hearts'
+ suit_order = 2
+ def __init__(self):
+ self.color = 'red'
+ def __str__(self):
+ return self.card_suit
+ def __eq__ (self, other):
+ return self.card_suit == other.card_suit
+
+class Spades(Suit):
+ card_suit = 'Spades'
+ suit_order = 3
+ def __init__(self):
+ self.color = 'black'
+ def __str__(self):
+ return self.card_suit
+ def __eq__ (self, other):
+ return self.card_suit == other.card_suit
+
+#class Card
+class Card(Rank, Suit):
+ def __init__(self, rank, suit):
+ super(Card, self).__setattr__('rank', rank())
+ super(Card, self).__setattr__('suit', suit())
+ def __setattr__(self, *args):
+ raise AttributeError("can't set attribute")
+ def __eq__ (self, other):
+ return self.suit == other.suit and self.rank == self.rank
+ def __str__(self):
+ return self.rank.card_name + ' of ' + self.suit.card_suit
+ def __repr__(self):
+ return '<Card {0} of {1}>'.format(self.rank.card_name,
+ self.suit.card_suit)
+
+class CardCollection():
+ def __init__(self, collection=None):
+ if collection == None:
+ collection = []
+ self.collection = list(collection)
+ 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[-1]
+ def bottom_card(self):
+ return self.collection[0]
+ def add(self, card):
+ return self.collection.append(card)
+ def index(self, card):
+ for i in range(len(self.collection)):
+ if str(self.collection[i]) == str(card):
+ return i
+ raise ValueError('{0} is not in list'.format(card))
+ def __len__(self):
+ return len(self.collection)
+ def __str__(self):
+ return str(self.collection)
+ def __repr__(self):
+ return str(self.collection)
+ def __iter__(self):
+ return (card for card in self.collection)
+ def __getitem__(self, key):
+ return self.collection[key]
+
+#
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine, 'Ten': Ten,
+ 'Jack': Jack, 'Queen': Queen, 'King': King}
+SUITS = {'Diamonds': Diamonds, 'Clubs': Clubs, 'Hearts': Hearts,
+ 'Spades': Spades}
+
+#decks
+def order_deck():
+ suit_order = [Diamonds, Clubs, Hearts, Spades]
+ rank_order = [King, Queen, Jack, Ten, Nine, Eight, Seven, Six,
+ Five, Four, Three, Two, Ace]
+ ordered_suits = sorted(list(SUITS.values()),
+ key = lambda x: suit_order.index(x))
+ ordered_ranks = sorted(list(RANKS.values()),
+ key = lambda x: rank_order.index(x))
+ return ordered_suits, ordered_ranks
+
+def StandardDeck():
+ standard_deck = CardCollection()
+ ordered_suits, ordered_ranks = order_deck()
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ standard_deck.add(Card(rank, suit))
+ return standard_deck
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ ordered_suits, ordered_ranks = order_deck()
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ if rank not in [Two, Three, Four, Five, Six]:
+ belote_deck.add(Card(rank, suit))
+ return belote_deck
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ ordered_suits, ordered_ranks = order_deck()
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ if rank not in [Two, Three, Four, Five, Six, Seven, Eight]:
+ sixty_six_deck.add(Card(rank, suit))
+ return sixty_six_deck
+
Отлично решение.
Ако искаш да го съкратиш, не е нужно да имаш __str__
и __init__
методи във всеки клас, можеш да преместиш функционалността им в базовия клас и "малките" класове да се състоят само от атрибути, без собствени методи. Освен това можеш да използваш изхода от StandardDeck за да генерираш BeloteDeck и съответно изхода от BeloteDeck за да генерираш SixtySixDeck.
Надявам се да ме разбра :)