Георги обнови решението на 23.03.2014 22:02 (преди над 10 години)
+class Rank:
+ def __init__(self, symbol):
+ self.__symbol = symbol
+
+
+class Suit:
+ def __init__(self, color):
+ self.__color = color
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Ace'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Two'
+
+
+class Tree(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Tree'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Four'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Five'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Six'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Seven'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Eight'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Nine'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Ten'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Jack'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Queen'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'King'
+
+
+class Diamonds(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Diamonds'
+
+
+class Clubs(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Clubs'
+
+
+class Hearts(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'red')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Hearts'
+
+
+class Spades(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'black')
+
+ def __eq__(self, other):
+ return self.__class__ == other.__class__
+
+ def __str__(self):
+ return 'Spades'
+
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Tree': Tree, 'Four': Four, 'Five': Five,
+ 'Six': Six, 'Seven': Seven, 'Eight': Eight, 'Nine': Nine,
+ 'Ten': Ten, 'Jack': Jack, 'Queen': Queen, 'King': King
+ }
+
+
+SUITS = {'Hearts': Hearts, 'Clubs': Clubs,
+ 'Diamonds': Diamonds, 'Spades': Spades
+ }
+
+
+STANDARD_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
+ 'Seven', 'Six', 'Five', 'Four', 'Tree', 'Two', 'Ace'
+ ]
+
+
+BELOTE_RANKS = ['King', 'Queen', 'Jack', 'Ten',
+ 'Nine', 'Eight', 'Seven', 'Ace'
+ ]
+
+
+SIXTY_SIX_RANKS = ['King', 'Queen', 'Jack', 'Ten', 'Nine', 'Ace']
+
+
+ORDERED_SUITS = ['Diamonds', 'Clubs', 'Hearts', 'Spades']
+
+
+class Card:
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __setattr__(self, name, value):
+ if name not in self.__dict__:
+ object.__setattr__(self, name, value)
+ else:
+ raise TypeError("can't set attribute")
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+
+class CardCollection:
+ def __init__(self, collection=[]):
+ self.__cards = list(collection)
+
+ def draw(self, index):
+ return self.__cards.pop(index)
+
+ def draw_from_top(self):
+ return self.__cards.pop(len(self.__cards) - 1)
+
+ 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):
+ for i in range(0, len(self.__cards)):
+ if card == self.__cards[i]:
+ return i
+
+ raise ValueError('<Card ' + str(card) + '>' + ' is not in list')
+
+ def __getitem__(self, index):
+ return self.__cards[index]
+
+ def __len__(self):
+ return len(self.__cards)
+
+
+def create_deck(suits, ranks):
+ return [Card(RANKS[rank], SUITS[suit]) for suit in suits
+ for rank in ranks]
+
+
+def StandardDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, STANDARD_RANKS))
+
+
+def BeloteDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, BELOTE_RANKS))
+
+
+def SixtySixDeck():
+ return CardCollection(create_deck(ORDERED_SUITS, SIXTY_SIX_RANKS))
Tree
означава дърво. Не е необходимо да дефинираш __eq__
във всички подкласове - можеш да гло сложиш само в базовия клас.
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
Това можеш да го замениш само с:
class King(Rank):
symbol = 'K'
Tree
означава дърво. Не е необходимо да дефинираш __eq__
във всички подкласове - можеш да гло сложиш само в базовия клас.
class King(Rank):
def __init__(self):
Rank.__init__(self, 'K')
Това можеш да го замениш само с:
class King(Rank):
symbol = 'K'