Даяна обнови решението на 25.03.2014 01:35 (преди над 10 години)
+'''
+Извинявам се че ти пиша тук, но test_deck_add ми прави безкраен цикъл и не мога да разбера защо. Като си го тествам си ми добава карти, уж, без проблем.
+'''
+
+
+class Rank():
+ """docstring for Rank"""
+ def __init__(self, symbol=''):
+ self.symbol = symbol
+
+ def __str__(self):
+ return str(self.__class__.__name__)
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+RANKS = {
+ 'King': type('King', (Rank, ), {'symbol': 'K'}),
+ 'Six': type('Six', (Rank, ), {'symbol': '6'}),
+ 'Jack': type('Jack', (Rank, ), {'symbol': 'J'}),
+ 'Five': type('Five', (Rank, ), {'symbol': '5'}),
+ 'Queen': type('Queen', (Rank, ), {'symbol': 'Q'}),
+ 'Ten': type('King', (Rank, ), {'symbol': 'k'}),
+ 'Ace': type('Ace', (Rank, ), {'symbol': 'A'}),
+ 'Three': type('Three', (Rank, ), {'symbol': '3'}),
+ 'Eight': type('Eight', (Rank, ), {'symbol': '8'}),
+ 'Four': type('Four', (Rank, ), {'symbol': '4'}),
+ 'Two': type('Two', (Rank, ), {'symbol': '2'}),
+ 'Seven': type('Seven', (Rank, ), {'symbol': '7'}),
+ 'Nine': type('Nine', (Rank, ), {'symbol': '9'})
+}
+
+
+class Suit():
+ """docstring for Suit"""
+ def __init__(self, color=''):
+ self.color = color
+
+ def __str__(self):
+ return str(self.__class__.__name__)
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+
+SUITS = {
+ 'Diamonds': type('Diamonds', (Suit, ), {'color': 'red'}),
+ 'Hearts': type('Hearts', (Suit, ), {'color': 'red'}),
+ 'Spades': type('Spades', (Suit, ), {'color': 'black'}),
+ 'Clubs': type('Clubs', (Suit, ), {'color': 'black'})
+}
+
+
+class Card():
+ """docstring for Card"""
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection():
+ """docstring for CardCollection"""
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __str__(self):
+ return str(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def drawn_from_botton(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):
+ if card in self.collection:
+ return self.collection.index(card)
+
+
+def StandardDeck():
+ standard_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ standard_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return standard_deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ belote_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Ace']
+ suits = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+ for suit in range(0, len(suits)):
+ for rank in range(0, len(ranks)):
+ sixty_six_deck.add(Card(RANKS[ranks[rank]], SUITS[suits[suit]]))
+ return sixty_six_deck
RANKS = {
'King': type('King', (Rank, ), {'symbol': 'K'}),
'Six': type('Six', (Rank, ), {'symbol': '6'}),
...
Това може да стане доста по-кратко, ако използваш dictionary comprehension. Иначе, си се ориентирала правилно да дефинираш класовете с type
.
Решението ти може да бъде и по-кратко. Например, имаш повтарящ се код. Предимно, имената на картите. Помисли, как да ги изнесеш в константи.