Момчил обнови решението на 25.03.2014 17:16 (преди над 10 години)
+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return "{}".format(self.symbol)
+
+ @property
+ def symbol(self):
+ return self.__symbol
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ace')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Two')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Three')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Four')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Five')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Six')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Seven')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Eight')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Nine')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Ten')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Jack')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Queen')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'King')
+
+RANKS = OrderedDict([
+ ('Ace', Ace), ('King', King), ('Queen', Queen), ('Jack', Jack),
+ ('Ten', Ten), ('Nine', Nine), ('Eight', Eight), ('Seven', Seven),
+ ('Six', Six), ('Five', Five), ('Four', Four), ('Three', Three),
+ ('Two', Two)
+ ])
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return "{}".format(self.color)
+
+ @property
+ def color(self):
+ return self.__color
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Hearts')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Clubs')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Spades')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'Diamonds')
+
+
+SUITS = OrderedDict([
+ ('Clubs', Clubs), ('Diamonds', Diamonds),
+ ('Hearts', Hearts), ('Spades', Spades)
+ ])
+
+
+class Card(Rank, Suit):
+ 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))
+
+ @property
+ def rank(self):
+ return self.__rank
+
+ @property
+ def suit(self):
+ return self.__suit
+
+
+class CardCollection:
+ def __init__(self, collection=None):
+ self.__collection = []
+ if collection is not None:
+ self.__collection = list(collection)
+
+ def __getitem__(self, item_position):
+ return self.__collection[item_position]
+
+ def __delitem__(self, item_position):
+ del self.__collection[item_position]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __str__(self):
+ return str(["{}".format(card) for card in self.collection])
+
+ @property
+ def collection(self):
+ return self.__collection
+
+ def index(self, card):
+ return self.collection.index(card)
+
+ def add(self, card):
+ self.__collection.append(card)
+
+ def top_card(self):
+ return self[-1]
+
+ def bottom_card(self):
+ return self[0]
+
+ def draw(self, index):
+ drawn_card = self[index]
+ del self[index]
+ return drawn_card
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+
+def StandardDeck(lowest_rank='Two'):
+ lowest_rank_index = list(RANKS.keys()).index(lowest_rank)
+ return CardCollection([
+ Card(RANKS[rank], SUITS[suit])
+ for suit in SUITS.keys()
+ for (index, (rank, _)) in enumerate(RANKS.items())
+ if index <= lowest_rank_index
+ ])
+
+
+def BeloteDeck():
+ return StandardDeck(lowest_rank='Seven')
+
+
+def SixtySixDeck():
+ return StandardDeck(lowest_rank='Nine')
+
+
+# std_deck = StandardDeck()
+# print(std_deck)
+#
+# belot_deck = BeloteDeck()
+# print(belot_deck)
+#
+# santase_deck = SixtySixDeck()
+# print(santase_deck)
- За базовите класове няма голям смисъл да ползваш
private
променливи. -
Rank.__init__(self, 'Ace')
- можеш просто да направиш така:self.symbol = 'Ace'
, няма голям смисъл да викаш конструктора на базовия клас. - Помисли за вариант динамично да конструираш под-класовете. Hint: това става с
type
. :)