Решение на Тесте карти от Христо Петков

Обратно към всички решения

Към профила на Христо Петков

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 16 успешни тест(а)
  • 0 неуспешни тест(а)

Код

SYMB = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
TYPES = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank():
def __eq__(self, right):
return self.symbol == right.symbol
def __str__(self):
return self.__class__.__name__
class Suit():
def __eq__(self, right):
return self.__class__.__name__ == right.__class__.__name__
def __str__(self):
return self.__class__.__name__
class Two(Rank):
symbol = "2"
class Three(Rank):
symbol = "3"
class Four(Rank):
symbol = "4"
class Five(Rank):
symbol = "5"
class Six(Rank):
symbol = "6"
class Seven(Rank):
symbol = "7"
class Eight(Rank):
symbol = "8"
class Nine(Rank):
symbol = "9"
class Ten(Rank):
symbol = "10"
class Jack(Rank):
symbol = "J"
class Queen(Rank):
symbol = "Q"
class King(Rank):
symbol = "K"
class Ace(Rank):
symbol = "A"
class Hearts(Suit):
color = "red"
class Diamonds(Suit):
color = "red"
class Spades(Suit):
color = "black"
class Clubs(Suit):
color = "black"
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 = {'Diamonds': Diamonds,
'Clubs': Clubs,
'Hearts': Hearts,
'Spades': Spades}
class Card(object):
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 __eq__(self, right):
return self.rank == right.rank and self.suit == right.suit
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<Card " + str(self.rank) + " of " + str(self.suit) + ">"
class CardCollection():
def __init__(self, collection=None):
if collection is None:
self.collection = []
else:
self.collection = []
for i in collection:
self.collection.append(i)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
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 i in range(len(self.collection)):
if card == self.collection[i]:
return i
raise ValueError(repr(card) + " is not in list")
def __iter__(self):
return (card for card in self.collection)
def __repr__(self):
return str(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def card_value(card):
return SYMB.index(card.rank.symbol) + 13*TYPES.index(str(card.suit))
def create_deck(ranks, suits, ranges):
range_index = []
for i in ranges:
range_index += list(i)
deck = []
for i in ranks:
for j in suits:
card = Card(ranks[i], suits[j])
if SYMB.index(card.rank.symbol) in range_index:
deck.append(card)
return CardCollection(sorted(deck, key=lambda x: card_value(x)))
def StandardDeck():
return create_deck(RANKS, SUITS, [range(0, 13)])
def BeloteDeck():
return create_deck(RANKS, SUITS, [range(0, 7), range(12, 13)])
def SixtySixDeck():
return create_deck(RANKS, SUITS, [range(0, 5), range(12, 13)])

Лог от изпълнението

................
----------------------------------------------------------------------
Ran 16 tests in 0.020s

OK

История (4 версии и 5 коментара)

Христо обнови решението на 20.03.2014 19:53 (преди над 10 години)

+SYMB = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
+TYPES = ["Diamonds", "Clubs", "Hearts", "Spades"]
+
+class Rank():
+ def __init__(self, symbol):
+ self.symbol = symbol
+
+ def __eq__(self, right):
+ return self.symbol == right.symbol
+
+ def __str__(self):
+ return str(self.symbol)
+
+
+class Suit():
+ def __init__(self, color):
+ self.color = color
+
+
+
+class CardTwo(Rank):
+ def __init__(self):
+ self.name = "Two"
+ self.symbol = "2"
+ def __str__(self):
+ return str(self.name)
+
+class CardThree(Rank):
+ def __init__(self):
+ self.name = "Three"
+ self.symbol = "3"
+ def __str__(self):
+ return str(self.name)
+
+class CardFour(Rank):
+ def __init__(self):
+ self.name = "Four"
+ self.symbol = "4"
+ def __str__(self):
+ return str(self.name)
+
+class CardFive(Rank):
+ def __init__(self):
+ self.name = "Five"
+ self.symbol = "5"
+ def __str__(self):
+ return str(self.name)
+
+class CardSix(Rank):
+ def __init__(self):
+ self.name = "Six"
+ self.symbol = "6"
+ def __str__(self):
+ return str(self.name)
+
+class CardSeven(Rank):
+ def __init__(self):
+ self.name = "Seven"
+ self.symbol = "7"
+ def __str__(self):
+ return str(self.name)
+
+class CardEight(Rank):
+ def __init__(self):
+ self.name = "Eight"
+ self.symbol = "8"
+ def __str__(self):
+ return str(self.name)
+
+class CardNine(Rank):
+ def __init__(self):
+ self.name = "Nine"
+ self.symbol = "9"
+ def __str__(self):
+ return str(self.name)
+
+class CardTen(Rank):
+ def __init__(self):
+ self.name = "Ten"
+ self.symbol = "10"
+ def __str__(self):
+ return str(self.name)
+
+class CardJack(Rank):
+ def __init__(self):
+ self.name = "Jack"
+ self.symbol = "J"
+ def __str__(self):
+ return str(self.name)
+
+class CardQueen(Rank):
+ def __init__(self):
+ self.name = "Queen"
+ self.symbol = "Q"
+ def __str__(self):
+ return str(self.name)
+
+class CardKing(Rank):
+ def __init__(self):
+ self.name = "King"
+ self.symbol = "K"
+ def __str__(self):
+ return str(self.name)
+
+class CardAce(Rank):
+ def __init__(self):
+ self.name = "Ace"
+ self.symbol = "A"
+ def __str__(self):
+ return str(self.name)
+
+class SuitHearts(Suit):
+ def __init__(self):
+ self.color = "red"
+ self.type = "Hearts"
+ def __eq__(self, right):
+ return self.type == right.type
+ def __str__(self):
+ return str(self.type)
+
+class SuitDiamonds(Suit):
+ def __init__(self):
+ self.color = "red"
+ self.type = "Diamonds"
+ def __eq__(self, right):
+ return self.type == right.type
+ def __str__(self):
+ return str(self.type)
+
+class SuitSpades(Suit):
+ def __init__(self):
+ self.color = "black"
+ self.type = "Spades"
+ def __eq__(self, right):
+ return self.type == right.type
+ def __str__(self):
+ return str(self.type)
+
+class SuitClubs(Suit):
+ def __init__(self):
+ self.color = "black"
+ self.type = "Clubs"
+ def __eq__(self, right):
+ return self.type == right.type
+ def __str__(self):
+ return str(self.type)
+
+RANKS = {'Ace': CardAce,
+ 'Two': CardTwo,
+ 'Three': CardThree,
+ 'Four': CardFour,
+ 'Five': CardFive,
+ 'Six': CardSix,
+ 'Seven': CardSeven,
+ 'Eight': CardEight,
+ 'Nine': CardNine,
+ 'Ten': CardTen,
+ 'Jack': CardJack,
+ 'Queen': CardQueen,
+ 'King': CardKing}
+SUITS = {'Diamonds': SuitDiamonds,
+ 'Clubs': SuitClubs,
+ 'Hearts': SuitHearts,
+ 'Spades': SuitSpades}
+
+class Card(object):
+ def __init__(self,rank,suit):
+ self.tup = (rank(), suit())
+
+ def myget(self):
+ return self.tup
+
+ def __getattribute__(self,attribute):
+ if attribute == "rank":
+ return self.tup[0]
+ elif attribute == "suit":
+ return self.tup[1]
+ else:
+ return object.__getattribute__(self, attribute)
+
+ def __setattr__(self, name, value):
+ if name in ("rank", "suit"):
+ raise AttributeError("can't set attribute")
+ else:
+ super().__setattr__(name, value)
+
+ def __eq__(self, right):
+ return self.tup == right.tup
+
+ def __str__(self):
+ return str(self.rank) + " of " + str(self.suit)
+
+ def __repr__(self):
+ return "<Card " + str(self.rank) + " of " + str(self.suit) + ">"
+
+
+class CardCollection():
+ def __init__(self, collection = None):
+ if collection == None:
+ self.collection = []
+ else:
+ self.collection = []
+ for i in collection:
+ self.collection.append(i)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.draw(-1)
+
+ def draw_from_bottom(self):
+ return self.draw(0)
+
+ 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 i in range(len(self.collection)):
+ if card == self.collection[i]:
+ return i
+ raise ValueError(repr(card) + " is not in list")
+
+ def __iter__(self):
+ return (card for card in self.collection)
+
+ def __repr__(self):
+ return str(self.collection)
+
+ def __getitem__(self,index):
+ return self.collection[index]
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def card_value(card):
+ return SYMB.index(card.rank.symbol) + 13*TYPES.index(card.suit.type)
+
+
+def create_deck(ranks,suits, ranges):
+ range_index = []
+ for i in ranges:
+ range_index += list(i)
+ deck = []
+ for i in ranks:
+ for j in suits:
+ card = Card(ranks[i],suits[j])
+ if SYMB.index(card.rank.symbol) in range_index:
+ deck.append(card)
+ return CardCollection(sorted(deck, key=lambda x: card_value(x)))
+
+def StandardDeck():
+ return create_deck(RANKS, SUITS, [range(0,13)])
+
+def BeloteDeck():
+ return create_deck(RANKS, SUITS, [range(0,7),range(12,13)])
+
+def SixtySixDeck():
+ return create_deck(RANKS, SUITS, [range(0,5),range(12,13)])
+
+
+

Кръсти си класовете Ace а не CardAce, Spades а не SuitSpades. Така няма нужда да съхраняваш атрибут Type. Името на всеки клас е достъпно в self.__class__.__name__ или в Ace.__name__ например.

Освен това ако ще пишеш конструктор, който добавя атрибути без да приема аргументи е по-добре да не го пишеш изобщо:

class SuitDiamonds(Suit):
    def __init__(self):
        self.color = "red"
        self.type = "Diamonds"

e същото като

class SuitDiamonds(Suit):
    color = "red"
    type = "Diamonds"

Всъщност не е нужно да дефинираш методи за да дефинираш клас. За решението на задачата по принцип можеш да преместиш всички методи от отделните карти и бои в базовият им клас.

Христо обнови решението на 20.03.2014 20:18 (преди над 10 години)

SYMB = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
TYPES = ["Diamonds", "Clubs", "Hearts", "Spades"]
+
class Rank():
def __init__(self, symbol):
self.symbol = symbol
-
+
def __eq__(self, right):
return self.symbol == right.symbol
def __str__(self):
return str(self.symbol)
-
+
class Suit():
def __init__(self, color):
self.color = color
-
-
+
class CardTwo(Rank):
def __init__(self):
self.name = "Two"
self.symbol = "2"
+
def __str__(self):
return str(self.name)
-
+
+
class CardThree(Rank):
def __init__(self):
self.name = "Three"
self.symbol = "3"
+
def __str__(self):
return str(self.name)
-
+
+
class CardFour(Rank):
def __init__(self):
self.name = "Four"
self.symbol = "4"
+
def __str__(self):
return str(self.name)
+
class CardFive(Rank):
def __init__(self):
self.name = "Five"
self.symbol = "5"
+
def __str__(self):
return str(self.name)
-
+
+
class CardSix(Rank):
def __init__(self):
self.name = "Six"
self.symbol = "6"
+
def __str__(self):
return str(self.name)
+
class CardSeven(Rank):
def __init__(self):
self.name = "Seven"
self.symbol = "7"
+
def __str__(self):
return str(self.name)
-
+
+
class CardEight(Rank):
def __init__(self):
self.name = "Eight"
self.symbol = "8"
+
def __str__(self):
return str(self.name)
+
class CardNine(Rank):
def __init__(self):
self.name = "Nine"
self.symbol = "9"
+
def __str__(self):
return str(self.name)
-
+
+
class CardTen(Rank):
def __init__(self):
self.name = "Ten"
self.symbol = "10"
+
def __str__(self):
return str(self.name)
+
class CardJack(Rank):
def __init__(self):
self.name = "Jack"
self.symbol = "J"
+
def __str__(self):
return str(self.name)
-
+
+
class CardQueen(Rank):
def __init__(self):
self.name = "Queen"
self.symbol = "Q"
+
def __str__(self):
return str(self.name)
-
+
+
class CardKing(Rank):
def __init__(self):
self.name = "King"
self.symbol = "K"
+
def __str__(self):
return str(self.name)
-
+
+
class CardAce(Rank):
def __init__(self):
self.name = "Ace"
self.symbol = "A"
+
def __str__(self):
return str(self.name)
+
class SuitHearts(Suit):
def __init__(self):
self.color = "red"
self.type = "Hearts"
+
def __eq__(self, right):
return self.type == right.type
+
def __str__(self):
return str(self.type)
+
class SuitDiamonds(Suit):
def __init__(self):
self.color = "red"
self.type = "Diamonds"
+
def __eq__(self, right):
return self.type == right.type
+
def __str__(self):
return str(self.type)
+
class SuitSpades(Suit):
def __init__(self):
self.color = "black"
self.type = "Spades"
+
def __eq__(self, right):
return self.type == right.type
+
def __str__(self):
return str(self.type)
+
class SuitClubs(Suit):
def __init__(self):
self.color = "black"
self.type = "Clubs"
+
def __eq__(self, right):
return self.type == right.type
+
def __str__(self):
return str(self.type)
RANKS = {'Ace': CardAce,
'Two': CardTwo,
'Three': CardThree,
'Four': CardFour,
'Five': CardFive,
'Six': CardSix,
'Seven': CardSeven,
'Eight': CardEight,
'Nine': CardNine,
'Ten': CardTen,
'Jack': CardJack,
'Queen': CardQueen,
'King': CardKing}
SUITS = {'Diamonds': SuitDiamonds,
'Clubs': SuitClubs,
'Hearts': SuitHearts,
'Spades': SuitSpades}
+
class Card(object):
- def __init__(self,rank,suit):
+ def __init__(self, rank, suit):
self.tup = (rank(), suit())
def myget(self):
return self.tup
- def __getattribute__(self,attribute):
+ def __getattribute__(self, attribute):
if attribute == "rank":
return self.tup[0]
elif attribute == "suit":
return self.tup[1]
else:
return object.__getattribute__(self, attribute)
def __setattr__(self, name, value):
if name in ("rank", "suit"):
raise AttributeError("can't set attribute")
else:
super().__setattr__(name, value)
-
+
def __eq__(self, right):
return self.tup == right.tup
-
+
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
-
+
def __repr__(self):
return "<Card " + str(self.rank) + " of " + str(self.suit) + ">"
class CardCollection():
- def __init__(self, collection = None):
- if collection == None:
+ def __init__(self, collection=None):
+ if collection is None:
self.collection = []
else:
self.collection = []
for i in collection:
self.collection.append(i)
-
+
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(-1)
-
+
def draw_from_bottom(self):
return self.draw(0)
-
+
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 i in range(len(self.collection)):
if card == self.collection[i]:
return i
raise ValueError(repr(card) + " is not in list")
def __iter__(self):
return (card for card in self.collection)
def __repr__(self):
return str(self.collection)
- def __getitem__(self,index):
+ def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def card_value(card):
return SYMB.index(card.rank.symbol) + 13*TYPES.index(card.suit.type)
-
-def create_deck(ranks,suits, ranges):
+
+def create_deck(ranks, suits, ranges):
range_index = []
for i in ranges:
range_index += list(i)
deck = []
for i in ranks:
for j in suits:
- card = Card(ranks[i],suits[j])
+ card = Card(ranks[i], suits[j])
if SYMB.index(card.rank.symbol) in range_index:
deck.append(card)
return CardCollection(sorted(deck, key=lambda x: card_value(x)))
+
def StandardDeck():
- return create_deck(RANKS, SUITS, [range(0,13)])
+ return create_deck(RANKS, SUITS, [range(0, 13)])
+
def BeloteDeck():
- return create_deck(RANKS, SUITS, [range(0,7),range(12,13)])
+ return create_deck(RANKS, SUITS, [range(0, 7), range(12, 13)])
+
def SixtySixDeck():
- return create_deck(RANKS, SUITS, [range(0,5),range(12,13)])
+ return create_deck(RANKS, SUITS, [range(0, 5), range(12, 13)])
-
+def test(deck):
+ for card in deck:
+ print(card)
+ return

Христо обнови решението на 20.03.2014 21:32 (преди над 10 години)

SYMB = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
TYPES = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank():
- def __init__(self, symbol):
- self.symbol = symbol
-
def __eq__(self, right):
return self.symbol == right.symbol
def __str__(self):
- return str(self.symbol)
+ return str(self.name)
class Suit():
- def __init__(self, color):
- self.color = color
+ def __eq__(self, right):
+ return self.type == right.type
-
-class CardTwo(Rank):
- def __init__(self):
- self.name = "Two"
- self.symbol = "2"
-
def __str__(self):
- return str(self.name)
+ return str(self.type)
-class CardThree(Rank):
- def __init__(self):
- self.name = "Three"
- self.symbol = "3"
+class Two(Rank):
+ name = "Two"
+ symbol = "2"
- def __str__(self):
- return str(self.name)
+class Three(Rank):
+ name = "Three"
+ symbol = "3"
-class CardFour(Rank):
- def __init__(self):
- self.name = "Four"
- self.symbol = "4"
- def __str__(self):
- return str(self.name)
+class Four(Rank):
+ name = "Four"
+ symbol = "2"
-class CardFive(Rank):
- def __init__(self):
- self.name = "Five"
- self.symbol = "5"
+class Five(Rank):
+ name = "Five"
+ symbol = "2"
- def __str__(self):
- return str(self.name)
+class Six(Rank):
+ name = "Six"
+ symbol = "2"
-class CardSix(Rank):
- def __init__(self):
- self.name = "Six"
- self.symbol = "6"
- def __str__(self):
- return str(self.name)
+class Seven(Rank):
+ name = "Seven"
+ symbol = "2"
-class CardSeven(Rank):
- def __init__(self):
- self.name = "Seven"
- self.symbol = "7"
+class Eight(Rank):
+ name = "Eight"
+ symbol = "2"
- def __str__(self):
- return str(self.name)
+class Nine(Rank):
+ name = "Nine"
+ symbol = "2"
-class CardEight(Rank):
- def __init__(self):
- self.name = "Eight"
- self.symbol = "8"
- def __str__(self):
- return str(self.name)
+class Ten(Rank):
+ name = "Ten"
+ symbol = "2"
-class CardNine(Rank):
- def __init__(self):
- self.name = "Nine"
- self.symbol = "9"
+class Jack(Rank):
+ name = "Jack"
+ symbol = "2"
- def __str__(self):
- return str(self.name)
+class Queen(Rank):
+ name = "Queen"
+ symbol = "2"
-class CardTen(Rank):
- def __init__(self):
- self.name = "Ten"
- self.symbol = "10"
- def __str__(self):
- return str(self.name)
+class King(Rank):
+ name = "King"
+ symbol = "2"
-class CardJack(Rank):
- def __init__(self):
- self.name = "Jack"
- self.symbol = "J"
+class Ace(Rank):
+ name = "Ace"
+ symbol = "2"
- def __str__(self):
- return str(self.name)
+class Hearts(Suit):
+ color = "red"
+ type = "Hearts"
-class CardQueen(Rank):
- def __init__(self):
- self.name = "Queen"
- self.symbol = "Q"
- def __str__(self):
- return str(self.name)
+class Diamonds(Suit):
+ color = "red"
+ type = "Diamonds"
-class CardKing(Rank):
- def __init__(self):
- self.name = "King"
- self.symbol = "K"
+class Spades(Suit):
+ color = "black"
+ type = "Spades"
- def __str__(self):
- return str(self.name)
+class Clubs(Suit):
+ color = "black"
+ type = "Clubs"
-class CardAce(Rank):
- def __init__(self):
- self.name = "Ace"
- self.symbol = "A"
+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 = {'Diamonds': Diamonds,
+ 'Clubs': Clubs,
+ 'Hearts': Hearts,
+ 'Spades': Spades}
- def __str__(self):
- return str(self.name)
-
-class SuitHearts(Suit):
- def __init__(self):
- self.color = "red"
- self.type = "Hearts"
-
- def __eq__(self, right):
- return self.type == right.type
-
- def __str__(self):
- return str(self.type)
-
-
-class SuitDiamonds(Suit):
- def __init__(self):
- self.color = "red"
- self.type = "Diamonds"
-
- def __eq__(self, right):
- return self.type == right.type
-
- def __str__(self):
- return str(self.type)
-
-
-class SuitSpades(Suit):
- def __init__(self):
- self.color = "black"
- self.type = "Spades"
-
- def __eq__(self, right):
- return self.type == right.type
-
- def __str__(self):
- return str(self.type)
-
-
-class SuitClubs(Suit):
- def __init__(self):
- self.color = "black"
- self.type = "Clubs"
-
- def __eq__(self, right):
- return self.type == right.type
-
- def __str__(self):
- return str(self.type)
-
-RANKS = {'Ace': CardAce,
- 'Two': CardTwo,
- 'Three': CardThree,
- 'Four': CardFour,
- 'Five': CardFive,
- 'Six': CardSix,
- 'Seven': CardSeven,
- 'Eight': CardEight,
- 'Nine': CardNine,
- 'Ten': CardTen,
- 'Jack': CardJack,
- 'Queen': CardQueen,
- 'King': CardKing}
-SUITS = {'Diamonds': SuitDiamonds,
- 'Clubs': SuitClubs,
- 'Hearts': SuitHearts,
- 'Spades': SuitSpades}
-
-
class Card(object):
def __init__(self, rank, suit):
self.tup = (rank(), suit())
def myget(self):
return self.tup
def __getattribute__(self, attribute):
if attribute == "rank":
return self.tup[0]
elif attribute == "suit":
return self.tup[1]
else:
return object.__getattribute__(self, attribute)
def __setattr__(self, name, value):
if name in ("rank", "suit"):
raise AttributeError("can't set attribute")
else:
super().__setattr__(name, value)
def __eq__(self, right):
return self.tup == right.tup
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<Card " + str(self.rank) + " of " + str(self.suit) + ">"
class CardCollection():
def __init__(self, collection=None):
if collection is None:
self.collection = []
else:
self.collection = []
for i in collection:
self.collection.append(i)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
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 i in range(len(self.collection)):
if card == self.collection[i]:
return i
raise ValueError(repr(card) + " is not in list")
def __iter__(self):
return (card for card in self.collection)
def __repr__(self):
return str(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def card_value(card):
return SYMB.index(card.rank.symbol) + 13*TYPES.index(card.suit.type)
def create_deck(ranks, suits, ranges):
range_index = []
for i in ranges:
range_index += list(i)
deck = []
for i in ranks:
for j in suits:
card = Card(ranks[i], suits[j])
if SYMB.index(card.rank.symbol) in range_index:
deck.append(card)
return CardCollection(sorted(deck, key=lambda x: card_value(x)))
def StandardDeck():
return create_deck(RANKS, SUITS, [range(0, 13)])
def BeloteDeck():
return create_deck(RANKS, SUITS, [range(0, 7), range(12, 13)])
def SixtySixDeck():
return create_deck(RANKS, SUITS, [range(0, 5), range(12, 13)])
def test(deck):
for card in deck:
print(card)
- return
+ return

Да, обаче си сбъркал малко с copy/paste и имаш грешки в подредбата на тестетата. Освен това нямаш нужда от атрибут name. Погледни какво показва self.__class__.__name__ на различни класове:

>>> class Test:
...   def thing(self): return self.__class__.__name__
... 
>>> t = Test()
>>> t.thing()
'Test'

Освен това по-добрият начин да направиш rank/suit immutable е да използваш @property.

Христо обнови решението на 21.03.2014 01:47 (преди над 10 години)

SYMB = ["K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2", "A"]
TYPES = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank():
def __eq__(self, right):
return self.symbol == right.symbol
def __str__(self):
- return str(self.name)
+ return self.__class__.__name__
class Suit():
def __eq__(self, right):
- return self.type == right.type
+ return self.__class__.__name__ == right.__class__.__name__
def __str__(self):
- return str(self.type)
+ return self.__class__.__name__
class Two(Rank):
- name = "Two"
symbol = "2"
class Three(Rank):
- name = "Three"
symbol = "3"
class Four(Rank):
- name = "Four"
- symbol = "2"
+ symbol = "4"
class Five(Rank):
- name = "Five"
- symbol = "2"
+ symbol = "5"
class Six(Rank):
- name = "Six"
- symbol = "2"
+ symbol = "6"
class Seven(Rank):
- name = "Seven"
- symbol = "2"
+ symbol = "7"
class Eight(Rank):
- name = "Eight"
- symbol = "2"
+ symbol = "8"
class Nine(Rank):
- name = "Nine"
- symbol = "2"
+ symbol = "9"
class Ten(Rank):
- name = "Ten"
- symbol = "2"
+ symbol = "10"
class Jack(Rank):
- name = "Jack"
- symbol = "2"
+ symbol = "J"
class Queen(Rank):
- name = "Queen"
- symbol = "2"
+ symbol = "Q"
class King(Rank):
- name = "King"
- symbol = "2"
+ symbol = "K"
class Ace(Rank):
- name = "Ace"
- symbol = "2"
+ symbol = "A"
class Hearts(Suit):
color = "red"
- type = "Hearts"
class Diamonds(Suit):
color = "red"
- type = "Diamonds"
class Spades(Suit):
color = "black"
- type = "Spades"
class Clubs(Suit):
color = "black"
- type = "Clubs"
+
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 = {'Diamonds': Diamonds,
'Clubs': Clubs,
'Hearts': Hearts,
'Spades': Spades}
class Card(object):
def __init__(self, rank, suit):
- self.tup = (rank(), suit())
+ self._rank = rank()
+ self._suit = suit()
- def myget(self):
- return self.tup
-
- def __getattribute__(self, attribute):
- if attribute == "rank":
- return self.tup[0]
- elif attribute == "suit":
- return self.tup[1]
- else:
- return object.__getattribute__(self, attribute)
-
- def __setattr__(self, name, value):
- if name in ("rank", "suit"):
- raise AttributeError("can't set attribute")
- else:
- super().__setattr__(name, value)
-
+ @property
+ def rank(self):
+ return self._rank
+
+ @property
+ def suit(self):
+ return self._suit
+
def __eq__(self, right):
- return self.tup == right.tup
+ return self.rank == right.rank and self.suit == right.suit
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
def __repr__(self):
return "<Card " + str(self.rank) + " of " + str(self.suit) + ">"
class CardCollection():
def __init__(self, collection=None):
if collection is None:
self.collection = []
else:
self.collection = []
for i in collection:
self.collection.append(i)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.draw(-1)
def draw_from_bottom(self):
return self.draw(0)
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 i in range(len(self.collection)):
if card == self.collection[i]:
return i
raise ValueError(repr(card) + " is not in list")
def __iter__(self):
return (card for card in self.collection)
def __repr__(self):
return str(self.collection)
def __getitem__(self, index):
return self.collection[index]
def __len__(self):
return len(self.collection)
def card_value(card):
- return SYMB.index(card.rank.symbol) + 13*TYPES.index(card.suit.type)
+ return SYMB.index(card.rank.symbol) + 13*TYPES.index(str(card.suit))
def create_deck(ranks, suits, ranges):
range_index = []
for i in ranges:
range_index += list(i)
deck = []
for i in ranks:
for j in suits:
card = Card(ranks[i], suits[j])
if SYMB.index(card.rank.symbol) in range_index:
deck.append(card)
return CardCollection(sorted(deck, key=lambda x: card_value(x)))
def StandardDeck():
return create_deck(RANKS, SUITS, [range(0, 13)])
def BeloteDeck():
return create_deck(RANKS, SUITS, [range(0, 7), range(12, 13)])
def SixtySixDeck():
- return create_deck(RANKS, SUITS, [range(0, 5), range(12, 13)])
-
+ return create_deck(RANKS, SUITS, [range(0, 5), range(12, 13)])
-
-def test(deck):
- for card in deck:
- print(card)
- return