Никола обнови решението на 20.03.2014 14:07 (преди над 10 години)
+class Suit:
+ def __str__(self):
+ return self.color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+class Rank:
+ def __str__(self):
+ return self.symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+# Sorted!
+RANK_NAMES = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three', 'Two', 'Ace', ]
+RANK_SYMBOLS = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
+SUIT_NAMES = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+SUIT_COLORS = [ "red", "black" ]
+
+# Go functional
+def create_constructor(attribute_name, attribute_value):
+ def constructor(self):
+ setattr(self, attribute_name, attribute_value)
+
+ return constructor;
+
+def create_rank(rank, symbol):
+ return type(rank, (Rank, ), { "__init__": create_constructor("symbol", symbol)})
+
+def create_suit(suit, color):
+ return type(suit, (Suit, ), { "__init__": create_constructor("color", color)})
+
+RANKS = { rank: create_rank(rank, RANK_SYMBOLS[RANK_NAMES.index(rank)]) for rank in RANK_NAMES }
+SUITS = { suit: create_suit(suit, SUIT_COLORS[SUIT_NAMES.index(suit) % 2]) for suit in SUIT_NAMES }
+
+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 __str__(self):
+ return "{0} of {1}".format(self.rank.__class__.__name__, self.suit.__class__.__name__)
+
+ def __repr__(self):
+ return "<Card {0} >".format(str(self))
+
+ def __eq__(self, other):
+ return (self.rank, self.suit) == (other.rank, other.suit)
+
+ def __int__(self):
+ return SUIT_NAMES.index(self.suit.__class__.__name__) * len(RANKS) + \
+ RANK_NAMES.index(self.rank.__class__.__name__)
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.deck = list(collection)
+
+ def draw(self, index):
+ return self.deck.pop(index)
+
+ def draw_from_top(self):
+ return self.deck.pop()
+
+ def draw_from_bottom(self):
+ return self.deck.pop(0)
+
+ def top_card(self):
+ return self.deck[len(self.deck) - 1]
+
+ def bottom_card(self):
+ return self.deck[0]
+
+ def add(self, card):
+ self.deck.append(card)
+
+ def index(self, card):
+ return self.deck.index(card)
+
+ def __getitem__(self, i):
+ return self.deck[i]
+
+ def __len__(self):
+ return len(self.deck)
+
+ def __repr__(self):
+ return repr(self.deck)
+
+
+def StandardDeck():
+ deck = [Card(rank, suit) for rank in RANKS.values() for suit in SUITS.values()]
+ sorted_deck = sorted(deck, key=lambda x: int(x))
+ return sorted_deck
+
+def rank_less_than(card, rank):
+ if card.rank == RANKS["Ace"]():
+ return False
+ return RANK_NAMES.index(card.rank.__class__.__name__) < RANK_NAMES.index(rank)
+
+def BeloteDeck():
+ return CardCollection([card for card in StandardDeck()
+ if rank_less_than(card, "Seven")])
+
+def SixtySixDeck():
+ return CardCollection([card for card in StandardDeck()
+ if rank_less_than(card, "Nine")])
+
+
Стандартните ти тестета са подредени в различен ред всеки път когато рестартираш програмата си :)
Освен това не е нужно да имаш конструктури на всяка от видовете карти и бои, можеш да ги направиш просто да имат различен атрибут и всякакви методи да са в базовите класове Rank
и Suit
.