Емилиан обнови решението на 24.03.2014 00:30 (преди над 10 години)
+from collections import OrderedDict
+
+
+class Rank:
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, other):
+ if self.symbol == other.symbol:
+ return True
+ else:
+ return False
+
+ def __str__(self):
+ return str(self.rank)
+
+
+class Suit:
+ def __init__(self, suit):
+ self.suit = suit
+ if self.suit == 'Diamonds':
+ self.color = 'red'
+ elif self.suit == 'Hearts':
+ self.color = 'red'
+ elif self.suit == 'Spades':
+ self.color = 'black'
+ elif self.suit == 'Clubs':
+ self.color = 'black'
+
+ def __eq__(self, other):
+ self.suit
+ other.suit = self.suit
+ if self.suit == other.suit:
+ return True
+ else:
+ return False
+
+ def __str__(self):
+ return str(self.suit)
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.symbol = 'A'
+ self.rank = 'Ace'
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.symbol = 'K'
+ self.rank = 'King'
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.symbol = 'Q'
+ self.rank = 'Queen'
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.symbol = 'J'
+ self.rank = 'Jack'
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.symbol = '10'
+ self.rank = 'Ten'
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.symbol = '9'
+ self.rank = 'Nine'
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.symbol = '8'
+ self.rank = 'Eight'
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.symbol = '7'
+ self.rank = 'Seven'
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.symbol = '6'
+ self.rank = 'Six'
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.symbol = '5'
+ self.rank = 'Five'
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.symbol = '4'
+ self.rank = 'Four'
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.symbol = '3'
+ self.rank = 'Three'
+
+
+class Two(Rank):
+ def __init__(self):
+ Rank.symbol = '2'
+ self.rank = 'Two'
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ self.suit = 'Diamonds'
+ self.color = 'red'
+
+ def __key__(self):
+ return self.suit
+
+
+class Clubs(Suit):
+ def __init__(self):
+ self.suit = 'Clubs'
+ self.color = 'black'
+
+ def __key__(self):
+ return self.suit
+
+
+class Hearts(Suit):
+ def __init__(self):
+ self.suit = 'Hearts'
+ self.color = 'red'
+
+ def __key__(self):
+ return self.suit
+
+
+class Spades(Suit):
+ def __init__(self):
+ self.suit = 'Spades'
+ self.color = 'black'
+
+ def __key__(self):
+ return self.suit
+
+
+RANKS = {'Ace': Ace,
+ 'King': King,
+ 'Queen': Queen,
+ 'Jack': Jack,
+ 'Ten': Ten,
+ 'Nine': Nine,
+ 'Eight': Eight,
+ 'Seven': Seven,
+ 'Six': Six,
+ 'Five': Five,
+ 'Four': Four,
+ 'Three': Three,
+ 'Two': Two}
+
+RANKS_ORDERED = OrderedDict()
+RANKS_ORDERED['King'] = King
+RANKS_ORDERED['Queen'] = Queen
+RANKS_ORDERED['Jack'] = Jack
+RANKS_ORDERED['Ten'] = Ten
+RANKS_ORDERED['Nine'] = Nine
+RANKS_ORDERED['Eight'] = Eight
+RANKS_ORDERED['Seven'] = Seven
+RANKS_ORDERED['Six'] = Six
+RANKS_ORDERED['Five'] = Five
+RANKS_ORDERED['Four'] = Four
+RANKS_ORDERED['Three'] = Three
+RANKS_ORDERED['Two'] = Two
+RANKS_ORDERED['Ace'] = Ace
+
+SUITS = {'Diamonds': Diamonds,
+ 'Hearts': Hearts,
+ 'Spades': Spades,
+ 'Clubs': Clubs}
+
+SUITS_ORDERED = OrderedDict()
+SUITS_ORDERED['Diamonds'] = Diamonds
+SUITS_ORDERED['Clubs'] = Clubs
+SUITS_ORDERED['Hearts'] = Hearts
+SUITS_ORDERED['Spades'] = Spades
+
+
+class Card:
+ __slots__ = ("rank", "suit")
+
+ def __init__(self, rank, suit):
+ object.__setattr__(self, "rank", rank())
+ object.__setattr__(self, "suit", suit())
+
+ def __eq__(self, other):
+ if self.rank == other.rank and self.suit == other.suit:
+ return True
+ else:
+ return False
+
+ def __lt__(self, other):
+ return self.__key__() < other.__key__()
+
+ def __str__(self):
+ return str(self.rank) + ' of ' + str(self.suit)
+
+ def __repr__(self):
+ return '<Card ' + str(self) + '>'
+
+ def __setattr__(self, *args):
+ raise AttributeError('can\'t set attribute')
+
+ def __delattr__(self, *args):
+ raise AttributeError('can\'t delete attribute')
+
+
+class CardCollection:
+ def __init__(self, collection=''):
+ self.collection = list(collection)
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def draw(self, index):
+ deleted = self.collection[index]
+ del self.collection[index]
+ return deleted
+
+ def draw_from_top(self):
+ deleted = self.collection[len(self.collection) - 1]
+ del self.collection[len(self.collection) - 1]
+ return deleted
+
+ def draw_from_bottom(self):
+ deleted = self.collection[0]
+ del self.collection[0]
+ return deleted
+
+ def top_card(self):
+ return self.collection[len(self.collection) - 1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def index(self, card):
+ if card in self.collection:
+ return self.collection.index(card)
+ else:
+ raise ValueError('<Card {}> is not in list'.format(card))
+
+
+def StandardDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()])
+
+ return deck.collection
+
+
+def BeloteDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()
+ if rank != RANKS_ORDERED['Two']
+ and rank != RANKS_ORDERED['Three']
+ and rank != RANKS_ORDERED['Four']
+ and rank != RANKS_ORDERED['Five']
+ and rank != RANKS_ORDERED['Six']])
+
+ return deck.collection
+
+
+def SixtySixDeck():
+ deck = CardCollection([Card(rank, suit)
+ for suit in SUITS_ORDERED.values()
+ for rank in RANKS_ORDERED.values()
+ if rank != RANKS_ORDERED['Two']
+ and rank != RANKS_ORDERED['Three']
+ and rank != RANKS_ORDERED['Four']
+ and rank != RANKS_ORDERED['Five']
+ and rank != RANKS_ORDERED['Six']
+ and rank != RANKS_ORDERED['Seven']
+ and rank != RANKS_ORDERED['Eight']])
+
+ return deck.collection
На тревожно много места имаш почти еднакви редове:
Например:
RANKS_ORDERED = OrderedDict()
RANKS_ORDERED['King'] = King
RANKS_ORDERED['Queen'] = Queen
RANKS_ORDERED['Jack'] = Jack
...
Това е изключително лоша практика която прави кода много тегав за разбиране защото е няколко екрана по-дълъг от колкото може да е. Горното можеш да замениш с:
RANKS_ORDERED = OrderedDict({"King": King, "Queen": Queen, ... })
като пренасяш на нов ред както е нужно за PEP8. Същото важи за редове 21-28, 280-286 и други. В програмирането не е лошо да си мързелив