Димитър обнови решението на 24.03.2014 22:02 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __str__(self):
+ return self.__symbol
+
+ def __eq__(self, other):
+ return self.__symbol == other.__symbol
+
+RANKS_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven',
+ 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace']
+
+RANKS = {}
+for i in RANKS_NAMES:
+ RANKS[i] = type(i, (Rank,),
+ dict(__init__=lambda self, i=i: Rank.__init__(self, i)))
+
+
+#####################################################################
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __str__(self):
+ return self.__color
+
+ def __eq__(self, other):
+ return self.__color == other.__color
+
+SUITS_NAMES = ['Diamonds', 'Hearts', 'Spades', 'Clubs']
+
+SUITS = {}
+for i in SUITS_NAMES:
+ SUITS[i] = type(i, (Suit,),
+ dict(__init__=lambda self, i=i: Suit.__init__(self, i)))
+
+
+#####################################################################
+class Card:
+ def __init__(self, rank, suit):
+ self.__rank = rank()
+ self.__suit = suit()
+
+ def __str__(self):
+ return str(self.__rank) + " of " + str(self.__suit)
+
+ def __eq__(self, other):
+ return self.__rank == other.__rank and self.__suit == other.__suit
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+#####################################################################
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.__collection = list(collection)
+
+ def __getitem__(self, i):
+ return self.__collection[i]
+
+ def __len__(self):
+ return len(self.__collection)
+
+ def draw(self, index):
+ return self.__collection.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ 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):
+ return self.__collection.index(card)
+
+
+#####################################################################
+def StandardDeck():
+ result_deck = []
+ for rank in RANKS_NAMES:
+ for suit in SUITS_NAMES:
+ result_deck.append(Card(RANKS[rank], SUITS[suit]))
+ return result_deck
+
+BELOTE_RANKS = ["King", "Queen", "Jack", "Ten",
+ "Nine", "Eight", "Seven", "Ace"]
+
+
+def BeloteDeck():
+ return list(filter(lambda x: str(x.rank) in BELOTE_RANKS, StandardDeck()))
+
+SIXTY_SIX_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Ace"]
+
+
+def SixtySixDeck():
+ return list(filter(lambda x: str(x.rank) in SIXTY_SIX_RANKS,
+ StandardDeck()))
-
###...###
не е нужно да се ползва като разделител между класовете - Не ми се струва логично
__color
и__rank
да бъдат private атрибути -
i
не е достатъчно добро име, когато става дума за имена, попринцип идва от "index"