Илиян обнови решението на 26.03.2014 16:52 (преди над 10 години)
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.__class__.__name__ == other.__class__.__name__
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+
+
+RANKS = {
+ Ace.__name__: Ace().__class__, Two.__name__: Two().__class__,
+ Three.__name__: Three().__class__, Four.__name__: Four().__class__,
+ Five.__name__: Five().__class__, Six.__name__: Six().__class__,
+ Seven.__name__: Seven().__class__, Eight.__name__: Eight().__class__,
+ Nine.__name__: Nine().__class__, Ten.__name__: Ten().__class__,
+ Jack.__name__: Jack().__class__, Queen.__name__: Queen().__class__,
+ King.__name__: King().__class__
+ }
+
+SUITS = {
+ Diamonds.__name__: Diamonds().__class__, Clubs.__name__: Clubs().__class__,
+ Hearts.__name__: Hearts().__class__, Spades.__name__: Spades().__class__
+ }
+
+
+class Card():
+ def __init__(self, rank, suit):
+ self.rank = rank()
+ self.suit = suit()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __eq__(self, other):
+ return self.rank == other.rank and self.suit == other.suit
+
+
+class CardCollection():
+
+ def __init__(self, collection=[]):
+ self.collection = collection
+ self.num = 0
+
+ def __getitem__(self, index):
+ self.index = index
+ return self.collection[self.index]
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self.num >= len(self.collection):
+ raise StopIteration
+ else:
+ current = self.collection[self.num]
+ self.num += 1
+ return current
+
+ def draw(self, index):
+ drawed = self.collection[index]
+ self.collection.remove(drawed)
+ return drawed
+
+ def draw_from_top(self, index=-1):
+ '''return self.collection.pop() would be
+ better but it raises an error in the test ! '''
+ return self.draw(index)
+
+ def draw_from_bottom(self, index=0):
+ # same problem as draw_from_top !
+ return self.draw(index)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ for item in self.collection:
+ if item == card:
+ return self.collection.index(item)
+ return ValueError