Стоян обнови решението на 25.03.2014 00:39 (преди над 10 години)
+#Rank and subclasses
+class Rank(object):
+ symbol = None
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self.symbol
+
+ def __repr__(self):
+ return '<%s.%s object at %s>' % (
+ self.__class__.__module__,
+ self.__class__.__name__,
+ hex(id(self)))
+
+class Two(Rank):
+ symbol = 'Two'
+
+class Three(Rank):
+ symbol = 'Three'
+
+class Four(Rank):
+ symbol = 'Four'
+
+class Five(Rank):
+ symbol = 'Five'
+
+class Six(Rank):
+ symbol = 'Six'
+
+class Seven(Rank):
+ symbol = 'Seven'
+
+class Eight(Rank):
+ symbol = 'Eight'
+
+class Nine(Rank):
+ symbol = 'Nine'
+
+class Ten(Rank):
+ symbol = 'Ten'
+
+class Jack(Rank):
+ symbol = 'Jack'
+
+class Queen(Rank):
+ symbol = 'Queen'
+
+class King(Rank):
+ symbol = 'King'
+
+class Ace(Rank):
+ symbol = 'Ace'
+
+#Suit and subclasses
+class Suit(object):
+ suit = None
+
+ def __eq__(self, other):
+ return type(self) == type(other)
+
+ def __str__(self):
+ return self.suit
+
+ def __repr__(self):
+ return '<%s.%s object at %s>' % (
+ self.__class__.__module__,
+ self.__class__.__name__,
+ hex(id(self)))
+
+class Diamonds(Suit):
+ suit = 'Diamonds'
+
+class Clubs(Suit):
+ suit = 'Clubs'
+
+class Spades(Suit):
+ suit = 'Spades'
+
+class Hearts(Suit):
+ suit = 'Hearts'
+
+#Constant dicts
+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}
+
+SUITS = {'Diamonds': Diamonds, 'Clubs': Clubs,
+ 'Hearts': Hearts, 'Spades': Spades}
+
+#Card class
+class Card(object):
+ def __init__(self, rank, suit):
+ self._rank = rank()
+ self._suit = suit()
+
+ def __eq__(self, other):
+ return self._rank == other._rank and self._suit == other._suit
+
+ def __str__(self):
+ return "{} of {}".format(str(self._rank), str(self._suit))
+
+ def __repr__(self):
+ return '<%s.%s object at %s>' % (
+ self.__class__.__module__,
+ self.__class__.__name__,
+ hex(id(self)))
+
+ @property
+ def rank(self):
+ return self._rank #Rank.__repr__(self._rank)
+ #Don't know why it returns <builtins.type...> instead of
+ #<__main__.someRank...>
+
+ @property
+ def suit(self):
+ return self._suit #Suit.__repr__(self._suit)
+
+ #ommiting setters to make class immutable
+
+
+class CardCollection(object):
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ for card in self.collection:
+ print("<Card {}>".format(str(card)), end=" ")
+ print("\n")
+ return ""
+
+ def __repr__(self):
+ print("so repr, much info, wow!")
+
+ def draw(self, index):
+ card = self.collection[index]
+ del self.collection[index]
+ return card
+
+ def draw_from_top(self):
+ card = self.collection[-1]
+ del self.collection[-1]
+ return card
+
+ def draw_from_bottom(self):
+ card = self.collection[0]
+ del self.collection[0]
+ return card
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ for i in range(len(self.collection)):
+ if self.collection[i] == card:
+ return i
+ else:
+ raise ValueError('could not find %r in deck' % (card))
+
+
+ORDER_OF_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+ORDER_OF_RANKS = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Six',
+ 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+def StandardDeck():
+ std_deck = []
+ for suit in ORDER_OF_SUITS:
+ for rank in ORDER_OF_RANKS:
+ std_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(std_deck)
+
+
+def SixtySixDeck():
+ ss_order_of_ranks = ORDER_OF_RANKS[:-8] + ORDER_OF_RANKS[-1:]
+ ss_deck = []
+ for suit in ORDER_OF_SUITS:
+ for rank in ss_order_of_ranks:
+ ss_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(ss_deck)
+
+
+def BeloteDeck():
+ belote_order_of_ranks = ORDER_OF_RANKS[:-6] + ORDER_OF_RANKS[-1:]
+ belote_deck = []
+ for suit in ORDER_OF_SUITS:
+ for rank in belote_order_of_ranks:
+ belote_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return CardCollection(belote_deck)
(sry, nqmam BG v momenta): Zabravil sym da si razkaram razni komentari, koito sega6nata versiq sa napylno bezmisleni. Shte update-na koda po natam, sled kato eventualno spodelite nqkakvi zabelejki
- Много хубаво си се сетил да използваш
@property
- Погледни отново последните три функции и ще видиш колко много повтарящ се код има. Дали няма начин да го избегнеш?