Антон обнови решението на 26.03.2014 16:25 (преди над 10 години)
+#!/usr/bin/python
+#-*- coding: cp1251 -*-
+
+#====================================RANK COLLECTION============================
+class Rank:
+ def __init__(self):
+ self.symbol = ''
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+ def __str__(self):
+ return self.__class__.__name__
+
+class Two(Rank):
+ pass
+
+class Three(Rank):
+ pass
+
+class Four(Rank):
+ pass
+
+class Five(Rank):
+ pass
+
+class Six(Rank):
+ pass
+
+class Seven(Rank):
+ pass
+
+class Eight(Rank):
+ pass
+
+class Nine(Rank):
+ pass
+
+class Ten(Rank):
+ pass
+
+class Jack(Rank):
+ pass
+
+class Queen(Rank):
+ pass
+
+class Ace(Rank):
+ pass
+
+class King(Rank):
+ pass
+
+RANKS = {Ace.__name__:Ace,
+ Two.__name__:Two,
+ Three.__name__:Three,
+ Four.__name__:Four,
+ Five.__name__:Five,
+ Six.__name__:Six,
+ Seven.__name__:Seven,
+ Eight.__name__:Eight,
+ Nine.__name__:Nine,
+ Ten.__name__:Ten,
+ Jack.__name__:Jack,
+ Queen.__name__:Queen,
+ King.__name__:King}
+
+#===============================SUIT COLLECTION=================================
+class Suit:
+ def __init__(self):
+ self.color = ''
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+ def __str__(self):
+ return self.__class__.__name__
+
+class Clubs(Suit):
+ pass
+
+class Diamonds(Suit):
+ pass
+
+class Hearts(Suit):
+ pass
+
+class Spades(Suit):
+ pass
+
+SUITS = {Clubs.__name__:Clubs,
+ Diamonds.__name__:Diamonds,
+ Hearts.__name__:Hearts,
+ Spades.__name__:Spades}
+
+#===============================CARDS REPRESENTATION============================
+class Card:
+ def __init__(self, rank, suit):
+ self._rank = rank
+ self._suit = suit
+
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
+ def __eq__(self, other):
+ return self.__dict__ == other.__dict__
+
+ def __str__(self):
+ return self._rank.__name__+' of '+self._suit.__name__
+
+#================================CARDS COLLECTION===============================
+class CardCollection:
+ def __init__(self, collection):
+ self._deck = list(collection)
+
+ def __getitem__(self, i):
+ return 'Card '+ str((self._deck)[i])
+
+ def __str__(self):
+ if self._deck:
+ rep = ""
+ for card in self._deck:
+ rep += '<Card '+str(card) + '>'
+ else:
+ rep = "<empty>"
+ return rep
+
+ @property
+ def deck(self):
+ return self._deck
+
+ #Метода draw връща картата на определен индекс и я премахва от колекцията
+ def draw(self, index):
+ self.__custome_delete(index)
+
+ def draw_from_top(self):
+ return self.__custome_delete(len(self._deck)-1)
+
+ def draw_from_bottom(self):
+ return self.__custome_delete(0)
+
+ def top_card(self):
+ return self._deck[(len(self._deck)-1)]
+
+ def bottom_card(self):
+ return self._deck[0]
+
+ #Още карти могат да се добавят с метода add, Метода add добавя карти отгоре
+ def add(self, card):
+ self._deck.append(card)
+
+ #Метода index търси за определена карта в тестето и връща индекса и.
+ def index(self, card):
+ try:
+ return self._deck.index(card)
+ except ValueError:
+ return ("ValueError: {0} is not in list".format(card))
+
+ def __custome_delete(self, element_index):
+ temp = self._deck[element_index]
+ del self._deck[element_index]
+ return temp
+
+#================================Standard Deck===============================
+
+def StandardDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+def BeloteDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ if(ranks_key[0] not in ['Two', 'Three', 'Four','Five','Six']):
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+def SixtySixDeck():
+ Collection = []
+ for suit_key in SUITS.items():
+ for ranks_key in RANKS.items():
+ if(ranks_key[0] not in ['Two', 'Three', 'Four','Five',
+ 'Six','Seven','Eight']):
+ temp = Card(RANKS[ranks_key[0]], SUITS[suit_key[0]])
+ Collection.append(temp)
+ return CardCollection(Collection)
+
+
+
+
Всичко би трябвало да работи само не можах да реализирам OrderedDict или нещо подобно. И не можах да ги сортирам по правилният начин. Може би ако вие го довършите (да подредите dict) и тогава да тествате. Поздрави.