Тихомир обнови решението на 25.03.2014 23:37 (преди над 10 години)
+# Used list, so we can get the sorting order from here
+RANKS_INFO = [
+ {'symbol': 'K', 'name': 'King'},
+ {'symbol': 'Q', 'name': 'Queen'},
+ {'symbol': 'J', 'name': 'Jack'},
+ {'symbol': '10', 'name': 'Ten'},
+ {'symbol': '9', 'name': 'Nine'},
+ {'symbol': '8', 'name': 'Eight'},
+ {'symbol': '7', 'name': 'Seven'},
+ {'symbol': '6', 'name': 'Six'},
+ {'symbol': '5', 'name': 'Five'},
+ {'symbol': '4', 'name': 'Four'},
+ {'symbol': '3', 'name': 'Three'},
+ {'symbol': '2', 'name': 'Two'},
+ {'symbol': 'A', 'name': 'Ace'},
+]
+
+
+def is_rank_equal(self, other):
+ return self.symbol == other.symbol
+
+
+def stringify_object(self):
+ return self.__name
+
+
+def stringify_object(self):
+ return self.__name
+
+
+stringify_rank = stringify_object
+
+
+class Rank:
+ def __init__(self):
+ self.symbol = ""
+
+ def __str__(self):
+ return ""
+
+
+RANKS = dict()
+for rank in RANKS_INFO:
+ RANKS[rank['name']] = type(
+ rank['name'], (Rank,),
+ {
+ 'symbol': rank['symbol'],
+ '__name': rank['name'],
+ '__eq__': is_rank_equal,
+ '__str__': stringify_rank
+ }
+ )
+ #setattr(RANKS[name], '__str__', staticmethod(stringify_rank))
+ #'__str__': stringify_rank
+ #RANKS[name].__str__ = stringify_rank
+
+
+# Used list, so we can get the sorting order from here
+SUIT_VALUES = [
+ {'name': 'Diamonds', 'color': 'red'},
+ {'name': 'Hearts', 'color': 'red'},
+ {'name': 'Spades', 'color': 'black'},
+ {'name': 'Clubs', 'color': 'black'}
+]
+
+
+class Suit(object):
+ def __init__(self):
+ self.color = ''
+
+
+def is_suit_equal(self, other):
+ return self.__name == other.__name
+
+stringify_suit = stringify_object
+
+SUITS = dict()
+for suit in SUIT_VALUES:
+ SUITS[suit['name']] = type(
+ suit['name'], (Suit,),
+ {
+ 'color': suit['color'],
+ '__name': suit['name'],
+ '__eq__': is_suit_equal,
+ '__str__': stringify_suit
+ }
+ )
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __eq__(self, other):
+ return (self.rank, self.suit) == (other.rank, other.suit)
+
+ def __str__(self):
+ return '{0} of {1}'.format(
+ str(self.rank), str(self.suit)
+ )
+
+
+class CardIterator:
+ def __init__(self, cards):
+ self.__cards = cards
+ self.__current_index = 0
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if (self.__current_index >= len(self.__cards)):
+ raise StopIteration()
+
+ index = self.__current_index
+ self.__current_index += 1
+
+ return self.__cards[index]
+
+
+class CardCollection:
+ def __init__(self, collection=list()):
+ self.__cards = list(collection)
+
+ def __iter__(self):
+ return CardIterator(self.__cards)
+
+ def __getitem__(self, num):
+ return self.__cards[num]
+
+ def __len__(self):
+ return len(self.__cards)
+
+ def draw(self, index):
+ return self.__cards.pop(index)
+
+ def draw_from_top(self):
+ return self.__cards.pop()
+
+ def draw_from_bottom(self):
+ return self.__cards.pop(0)
+
+ def top_card(self):
+ return self.__cards[len(self.__cards) - 1]
+
+ def bottom_card(self):
+ return self.__cards[0]
+
+ def add(self, card):
+ self.__cards.append(card)
+
+ def index(self, card):
+ return self.__cards.index(card)
+
+
+def GenerateDeck(exclusions):
+ cards = CardCollection()
+
+ for suit in SUIT_VALUES:
+ for rank in RANKS_INFO:
+ if rank['symbol'] in exclusions:
+ continue
+ cards.add(Card(RANKS[rank['name']], SUITS[suit['name']]))
+
+ return cards
+
+
+def StandardDeck():
+ return GenerateDeck([])
+
+
+def StandardDeck():
+ return GenerateDeck(['2', '3', '4', '5', '6'])
+
+
+def SixtySixDeck():
+ return GenerateDeck(['2', '3', '4', '5', '6', '7', '8'])