Таня обнови решението на 24.03.2014 12:59 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+
+class King(Rank):
+ def __init__(self):
+ self.symbol = 'K'
+
+
+class Queen(Rank):
+ def __init__(self):
+ self.symbol = 'Q'
+
+
+class Jack(Rank):
+ def __init__(self):
+ self.symbol = 'J'
+
+
+class Ace(Rank):
+ def __init__(self):
+ self.symbol = 'A'
+
+
+class Ten(Rank):
+ def __init__(self):
+ self.symbol = '10'
+
+
+class Nine(Rank):
+ def __init__(self):
+ self.symbol = '9'
+
+
+class Eight(Rank):
+ def __init__(self):
+ self.symbol = '8'
+
+
+class Seven(Rank):
+ def __init__(self):
+ self.symbol = '7'
+
+
+class Six(Rank):
+ def __init__(self):
+ self.symbol = '6'
+
+
+class Five(Rank):
+ def __init__(self):
+ self.symbol = '5'
+
+
+class Four(Rank):
+ def __init__(self):
+ self.symbol = '4'
+
+
+class Three(Rank):
+ def __init__(self):
+ self.symbol = '3'
+
+
+class Two(Rank):
+ def __init__(self):
+ self.symbol = '2'
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __str__(self):
+ return self.__class__.__name__
+
+ def __eq__(self, other):
+ return (self.color == other.color) and (str(self) == str(other))
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.color = 'black'
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank
+ self._suit = suit
+
+ def __repr__(self):
+ return '<{0} {1}>'.format(self.__class__.__name__, str(self))
+
+ def __str__(self):
+ return '{0} of {1}'.format(self.rank, self.suit)
+
+ @property
+ def rank(self):
+ return self._rank()
+
+ @rank.setter
+ def rank(self, value):
+ raise AttributeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self._rank == other._rank and self._suit == other._suit
+
+ @property
+ def suit(self):
+ return self._suit()
+
+ @suit.setter
+ def suit(self, value):
+ raise AttributeError("can't set attribute")
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ for card in self.collection:
+ yield card
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ drawn_card = self.collection[index]
+ del self.collection[index]
+ return drawn_card
+
+ def draw_from_top(self):
+ drawn_card = self.collection[len(self) - 1]
+ del self.collection[len(self) - 1]
+ return drawn_card
+
+ def draw_from_bottom(self):
+ drawn_card = self.collection[0]
+ del self.collection[0]
+ return drawn_card
+
+ def top_card(self):
+ return self.collection[len(self) - 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)
+
+
+RANKS = {'King': King, 'Six': Six, 'Jack': Jack, 'Five': Five, 'Queen': Queen,
+ 'Ten': Ten, 'Ace': Ace, 'Three': Three, 'Eight': Eight, 'Four': Four,
+ 'Two': Two, 'Seven': Seven, 'Nine': Nine}
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Spades': Spades, 'Diamonds': Diamonds}
+
+
+def StandardDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine',
+ 'Eight', 'Seven', 'Six', 'Five', 'Four',
+ 'Three', 'Two', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ standard_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ standard_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return standard_deck
+
+
+def BeloteDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ belote_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ belote_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return belote_deck
+
+
+def SixtySixDeck():
+ ordered_ranks = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+ ordered_suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+ sixty_six_deck = []
+ for suit in ordered_suits:
+ for rank in ordered_ranks:
+ sixty_six_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return sixty_six_deck
На подкласовете може би е по-добра идея да сетваш атрибутите color
и symbol
като атрибути на класа, не на инстансите.
Помисли за вариант динамично да създадеш всичките под-класове на Suit
и Rank
.
collection=[]
- в Python не е добра идея да използваш mutable обект за аргумент по подразбиране. В този конкретен случай, няма да ти попречи, но не е добра практика.
2 теста не ти минават - единия е свързан с index
метода, другия с bottom_card
. Разгледай всички възможни сценарии.