Ралица обнови решението на 24.03.2014 21:48 (преди над 10 години)
+class Rank:
+ symbol = ''
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+
+ def __eq__(self, other):
+ return (self.symbol == other.symbol)
+
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Suit:
+ color = ''
+ def __init__(self, color):
+ self.color = color
+
+
+ def __eq__(self, other):
+ return (self.color == other.color)
+
+
+ def __str__(self):
+ return self.__class__.__name__
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'A')
+ self.symbol = 'A'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, '2')
+ self.symbol = '2'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, '3')
+ self.symbol = '3'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, '4')
+ self.symbol = '4'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, '5')
+ self.symbol = '5'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, '6')
+ self.symbol = '6'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, '7')
+ self.symbol = '7'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, '8')
+ self.symbol = '8'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, '9')
+ self.symbol = '9'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, '10')
+ self.symbol = '10'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'J')
+ self.symbol = 'J'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'Q')
+ self.symbol = 'Q'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, 'K')
+ self.symbol = 'K'
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.color = 'red'
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'red')
+ self.color = 'red'
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.color = 'black'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, 'black')
+ self.color = 'black'
+
+
+class Card:
+ rank = Rank('')
+ suit = Suit('')
+ def __init__(self, rank, suit):
+ super(Card, self).__setattr__("rank", rank())
+ super(Card, self).__setattr__("suit", suit())
+
+
+ def __setattr__(self, name, value):
+ error = "can't set attribute"
+ raise AttributeError(error)
+
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+
+ def __repr__(self):
+ return "<Card {} of {}>".format(self.rank, self.suit)
+
+
+ def __eq__(self, other):
+ return (self.rank == other.rank and self.suit == other.suit)
+
+
+class CardCollection:
+ def __init__(self, collection = []):
+ self.collection = list(collection)
+
+
+ def __len__(self):
+ return len(self.collection)
+
+
+ def __getitem__(self,index):
+ return self.collection[index]
+
+
+ def __repr__(self):
+ return str(self.collection)
+
+
+ def draw(self, index):
+ card = self.collection[index]
+ del(self.collection[index])
+ return card
+
+
+ def draw_from_top(self):
+ top_card = self.collection[-1]
+ del(self.collection[-1])
+ return top_card
+
+
+ def draw_from_bottom(self):
+ bottom_card = self.collection[0]
+ del(self.collection[0])
+ return bottom_card
+
+
+ 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 _ in range(0, len(self.collection)):
+ if(self.collection(_) == card):
+ return _
+ raise ValueError("Card" + str(card) + "is not in list")
+
+
+def StandardDeck():
+ standart_deck = CardCollection()
+ for _ in COLORS:
+ for __ in SYMBOLS:
+ standart_deck.add(Card(__,_))
+ return standart_deck
+
+
+def BeloteDeck():
+ belote_deck = CardCollection()
+ for _ in COLORS:
+ for __ in range(0,len(SYMBOLS)):
+ if(__ <= 6 or __ == 12):
+ belote_deck.add(Card(SYMBOLS[__],_))
+ return belote_deck
+
+
+def SixtySixDeck():
+ sixty_six_deck = CardCollection()
+ for _ in COLORS:
+ for __ in range(0,len(SYMBOLS)):
+ if(__ <= 4 or __ == 12):
+ sixty_six_deck.add(Card(SYMBOLS[__],_))
+ return sixty_six_deck
+
+
+COLORS = [Diamonds, Hearts, Spades, Clubs]
+
+
+SYMBOLS = [King, Queen, Jack, Ten, Nine, Eight, Seven,
+ Six, Five, Four, Three, Two, Ace]
+
+
+RANKS = {'Ace': Ace, 'Two': Two, 'Three': Three, '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, 'Spades': Spades,
+ 'Diamonds': Diamonds}
Семантично е по-подходящо
symbol
иcolor
,rank
да ги направиш атрибути на класа вместо да ги подаваш през__init__
-и и да ги правиш атрибути на инстанциитемежду методите в един клас се оставя по един празен ред
-
скоби при
return
иdel
са противопоказни- прегледай и
help(list.pop)
. Може да ти хрумне по-подходяща имплементация заdraw_...
- прегледай и
-
_
,__
... Не! смени ги с подходящи имена-
COLORS
не съдържа точно това, което би очаквал. Note: Suit е английската дума за боя на карта
-
StandardDeck
,BeloteDeck
,SixtySixDeck
имат голямо количество повтарящ се код. Можеш ли да го преизползваш някак?