Решение на Тесте карти от Владимир Начев

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

Към профила на Владимир Начев

Резултати

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

Код

ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
"Six", "Five", "Four", "Three", "Two", "Ace"]
ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank:
def __init__(self, card_symbol):
self.symbol = card_symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return str(self) == str(other)
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
def __str__(self):
return "Two"
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
def __str__(self):
return "Three"
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
def __str__(self):
return "Four"
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
def __str__(self):
return "Five"
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
def __str__(self):
return "Six"
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
def __str__(self):
return "Seven"
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
def __str__(self):
return "Eight"
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
def __str__(self):
return "Nine"
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
def __str__(self):
return "Ten"
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
def __str__(self):
return "Jack"
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
def __str__(self):
return "Queen"
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
def __str__(self):
return "King"
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
def __str__(self):
return "Ace"
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Hearts"
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Clubs"
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Spades"
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Diamonds"
RANKS = {"Two": Two, "Three": Three, "Four": Four, "Five": Five,
"Six": Six, "Seven": Seven, "Eight": Eight, "Nine": Nine,
"Ten": Ten, "Jack": Jack, "Queen": Queen, "King": King, "Ace": Ace}
SUITS = {"Hearts": Hearts, "Clubs": Clubs,
"Spades": Spades, "Diamonds": Diamonds}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(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
def __setattr__(self, key, value):
if hasattr(self, key):
raise AttributeError("can`t set attribute")
class CardCollection:
def __init__(self, collection=list()):
self.collection = [card for card in collection]
def draw(self, index):
result = self.collection[index]
self.collection.__delitem__(index)
return result
def draw_from_top(self):
index = self.__len__() - 1
result = self.__getitem__(index)
self.collection.__delitem__(index)
return result
def draw_from_bottom(self):
result = self.__getitem__(0)
self.collection.__delitem__(0)
return result
def top_card(self):
return self.__getitem__(self.__len__() - 1)
def bottom_card(self):
return self.__getitem__(0)
def add(self, card):
if isinstance(card, Card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __len__(self):
return len(self.collection)
def __getitem__(self, item):
return self.collection[item]
def __str__(self):
result = "["
for card in self.collection:
result +="{}".format(card)
if card != self.collection[len(self.collection)-1]:
result += ", "
result += "]"
return result
def custom_deck(index):
result = CardCollection()
for suit in ORDERED_SUITS:
for rank in range(index):
result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
result.add(Card(RANKS["Ace"], SUITS[suit]))
return result
def StandardDeck():
return custom_deck(12)
def BeloteDeck():
return custom_deck(7)
def SixtySixDeck():
return custom_deck(5)

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

................
----------------------------------------------------------------------
Ran 16 tests in 0.023s

OK

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

Владимир обнови решението на 24.03.2014 00:23 (преди около 10 години)

+class Rank:
+ def __init__(self, card_symbol):
+ self.symbol = card_symbol
+
+ def __eq__(self, other):
+ return self.symbol == other.symbol
+
+ def __str__(self):
+ return self.symbol
+
+
+class Suit:
+ def __init__(self, color):
+ self.color = color
+
+ def __eq__(self, other):
+ return self.color == other.color
+
+ def __str__(self):
+ return self.color
+
+class Two(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Two")
+
+
+class Three(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Three")
+
+
+
+class Four(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Four")
+
+
+class Five(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Five")
+
+
+class Six(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Six")
+
+
+class Seven(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Seven")
+
+
+class Eight(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Eight")
+
+
+class Nine(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Nine")
+
+
+class Ten(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Ten")
+
+
+class Jack(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Jack")
+
+
+class Queen(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Queen")
+
+
+class King(Rank):
+ def __init__(self):
+ Rank.__init__(self, "King")
+
+
+class Ace(Rank):
+ def __init__(self):
+ Rank.__init__(self, "Ace")
+
+
+class Hearts(Suit):
+ def __init__(self):
+ Suit.__init__(self, "Hearts")
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "Clubs")
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "Spades")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "Diamonds")
+
+
+RANKS = {"Two": Two, "Three": Three, "Four": Four, "Five": Five,
+ "Six": Six, "Seven": Seven, "Eight": Eight, "Nine": Nine,
+ "Ten": Ten, "Jack": Jack, "Queen": Queen, "King": King, "Ace": Ace}
+
+SUITS = {"Hearts": Hearts, "Clubs": Clubs,
+ "Spades": Spades, "Diamonds": Diamonds}
+
+
+class Card:
+ def __init__(self, rank, suit):
+ object.__setattr__(self, "rank", rank)
+ object.__setattr__(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
+
+ def __setattr__(self, key, value):
+ if hasattr(self, key):
+ raise AttributeError
+
+
+class CardCollection:
+ def __init__(self, collection=list()):
+ self.collection = [card for card in collection if isinstance(card, Card)]
+
+ def draw(self, index):
+ result = self.collection[index]
+ self.collection.__delitem__(index)
+ return result
+
+ def draw_from_top(self):
+ index = self.__len__() - 1
+ result = self.__getitem__(index)
+ self.collection.__delitem__(index)
+ return result
+
+ def draw_from_bottom(self):
+ result = self.__getitem__(0)
+ self.collection.__delitem__(0)
+ return result
+
+ def top_card(self):
+ return self.__getitem__(self.__len__() - 1)
+
+ def bottom_card(self):
+ return self.__getitem__(0)
+
+ def add(self, card):
+ if isinstance(card, Card):
+ self.collection.append(card)
+
+ def index(self, card):
+ for index in range(self.__len__()):
+ if self.collection[index] == card:
+ return index
+ raise ValueError
+
+ def __len__(self):
+ return len(self.collection)
+
+ def __getitem__(self, item):
+ return self.collection[item]
+
+ def __str__(self):
+ result = []
+ for card in self.collection:
+ result.append(card)
+ return str(result)
+
+ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
+ "Six", "Five", "Four", "Three", "Two", "Ace"]
+
+ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
+
+def StandartDeck():
+ result = CardCollection()
+ for suit in ORDERED_SUITS:
+ for rank in ORDERED_RANKS:
+ result.add(Card(RANKS[rank], SUITS[suit]))
+ return result
+
+def BeloteDeck():
+ result = CardCollection()
+ for suit in ORDERED_SUITS:
+ for rank in range(7):
+ result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
+ result.add(Card(RANKS["Ace"], SUITS[suit]))
+ return result
+
+
+
+def SixtySixDeck():
+ result = CardCollection()
+ for suit in ORDERED_SUITS:
+ for rank in range(5):
+ result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
+ result.add(Card(RANKS["Ace"], SUITS[suit]))
+ return result
+

За прозводните класове на Rank трябва ли символа за класа King да е "K" или може да си остане и "King"?

Не съм много сигурен дали правилно хвърлям ValueError-а и AttributeError-а. Имам няколко неща по pep8, които ще оправа преди крайния срок. Също така трябва да си оправа представянето на CardCollection като низ, май?

Ок.

А за боите само 'red'/'black' ли са, или 'D','H','C','S' са за предпочитане?

Относно index: направих си едно стандартно тесте и му поисках асо пика, най-дружелюбно ми върна 51 без никакви грешки?

Относно атрибутите на Card, продължавам във форума.

Владимир обнови решението на 24.03.2014 16:01 (преди около 10 години)

+ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
+ "Six", "Five", "Four", "Three", "Two", "Ace"]
+ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
+
+
class Rank:
def __init__(self, card_symbol):
self.symbol = card_symbol
def __eq__(self, other):
return self.symbol == other.symbol
- def __str__(self):
- return self.symbol
-
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
- return self.color == other.color
+ return str(self) == str(other)
def __str__(self):
return self.color
+
class Two(Rank):
def __init__(self):
- Rank.__init__(self, "Two")
+ Rank.__init__(self, "2")
+ def __str__(self):
+ return "Two"
+
class Three(Rank):
def __init__(self):
- Rank.__init__(self, "Three")
+ Rank.__init__(self, "3")
+ def __str__(self):
+ return "Three"
class Four(Rank):
def __init__(self):
- Rank.__init__(self, "Four")
+ Rank.__init__(self, "4")
+ def __str__(self):
+ return "Four"
+
class Five(Rank):
def __init__(self):
- Rank.__init__(self, "Five")
+ Rank.__init__(self, "5")
+ def __str__(self):
+ return "Five"
+
class Six(Rank):
def __init__(self):
- Rank.__init__(self, "Six")
+ Rank.__init__(self, "6")
+ def __str__(self):
+ return "Six"
+
class Seven(Rank):
def __init__(self):
- Rank.__init__(self, "Seven")
+ Rank.__init__(self, "7")
+ def __str__(self):
+ return "Seven"
+
class Eight(Rank):
def __init__(self):
- Rank.__init__(self, "Eight")
+ Rank.__init__(self, "8")
+ def __str__(self):
+ return "Eight"
+
class Nine(Rank):
def __init__(self):
- Rank.__init__(self, "Nine")
+ Rank.__init__(self, "9")
+ def __str__(self):
+ return "Nine"
+
class Ten(Rank):
def __init__(self):
- Rank.__init__(self, "Ten")
+ Rank.__init__(self, "10")
+ def __str__(self):
+ return "Ten"
+
class Jack(Rank):
def __init__(self):
- Rank.__init__(self, "Jack")
+ Rank.__init__(self, "J")
+ def __str__(self):
+ return "Jack"
+
class Queen(Rank):
def __init__(self):
- Rank.__init__(self, "Queen")
+ Rank.__init__(self, "Q")
+ def __str__(self):
+ return "Queen"
+
class King(Rank):
def __init__(self):
- Rank.__init__(self, "King")
+ Rank.__init__(self, "K")
+ def __str__(self):
+ return "King"
+
class Ace(Rank):
def __init__(self):
- Rank.__init__(self, "Ace")
+ Rank.__init__(self, "A")
+ def __str__(self):
+ return "Ace"
+
class Hearts(Suit):
def __init__(self):
- Suit.__init__(self, "Hearts")
+ Suit.__init__(self, "red")
+ def __str__(self):
+ return "Hearts"
+
class Clubs(Suit):
def __init__(self):
- Suit.__init__(self, "Clubs")
+ Suit.__init__(self, "black")
+ def __str__(self):
+ return "Clubs"
+
class Spades(Suit):
def __init__(self):
- Suit.__init__(self, "Spades")
+ Suit.__init__(self, "black")
+ def __str__(self):
+ return "Spades"
+
class Diamonds(Suit):
def __init__(self):
- Suit.__init__(self, "Diamonds")
+ Suit.__init__(self, "red")
+ def __str__(self):
+ return "Diamonds"
RANKS = {"Two": Two, "Three": Three, "Four": Four, "Five": Five,
"Six": Six, "Seven": Seven, "Eight": Eight, "Nine": Nine,
"Ten": Ten, "Jack": Jack, "Queen": Queen, "King": King, "Ace": Ace}
SUITS = {"Hearts": Hearts, "Clubs": Clubs,
"Spades": Spades, "Diamonds": Diamonds}
class Card:
def __init__(self, rank, suit):
- object.__setattr__(self, "rank", rank)
- object.__setattr__(self, "suit", suit)
+ object.__setattr__(self, "rank", rank())
+ object.__setattr__(self, "suit", suit())
def __str__(self):
- return str(self.rank()) + " of " + str(self.suit())
+ return str(self.rank) + " of " + str(self.suit)
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __setattr__(self, key, value):
if hasattr(self, key):
raise AttributeError
class CardCollection:
def __init__(self, collection=list()):
- self.collection = [card for card in collection if isinstance(card, Card)]
+ self.collection = [card for card in collection
+ if isinstance(card, Card)]
def draw(self, index):
result = self.collection[index]
self.collection.__delitem__(index)
return result
def draw_from_top(self):
index = self.__len__() - 1
result = self.__getitem__(index)
self.collection.__delitem__(index)
return result
def draw_from_bottom(self):
result = self.__getitem__(0)
self.collection.__delitem__(0)
return result
def top_card(self):
return self.__getitem__(self.__len__() - 1)
def bottom_card(self):
return self.__getitem__(0)
def add(self, card):
if isinstance(card, Card):
self.collection.append(card)
def index(self, card):
for index in range(self.__len__()):
if self.collection[index] == card:
return index
raise ValueError
def __len__(self):
return len(self.collection)
def __getitem__(self, item):
return self.collection[item]
def __str__(self):
- result = []
+ result = "["
for card in self.collection:
- result.append(card)
- return str(result)
+ if card == self.collection[len(self.collection)-1]:
+ result += "<Card {}>]".format(card)
+ else:
+ result += "<Card {}>, ".format(card)
+ return result
-ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
- "Six", "Five", "Four", "Three", "Two", "Ace"]
-ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
-
-def StandartDeck():
+def StandardDeck():
result = CardCollection()
for suit in ORDERED_SUITS:
for rank in ORDERED_RANKS:
result.add(Card(RANKS[rank], SUITS[suit]))
return result
+
def BeloteDeck():
result = CardCollection()
for suit in ORDERED_SUITS:
for rank in range(7):
result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
result.add(Card(RANKS["Ace"], SUITS[suit]))
return result
-
def SixtySixDeck():
result = CardCollection()
for suit in ORDERED_SUITS:
for rank in range(5):
result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
result.add(Card(RANKS["Ace"], SUITS[suit]))
- return result
-
+ return result

Владимир обнови решението на 26.03.2014 16:27 (преди около 10 години)

ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
"Six", "Five", "Four", "Three", "Two", "Ace"]
ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank:
def __init__(self, card_symbol):
self.symbol = card_symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return str(self) == str(other)
- def __str__(self):
- return self.color
-
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
def __str__(self):
return "Two"
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
def __str__(self):
return "Three"
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
def __str__(self):
return "Four"
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
def __str__(self):
return "Five"
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
def __str__(self):
return "Six"
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
def __str__(self):
return "Seven"
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
def __str__(self):
return "Eight"
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
def __str__(self):
return "Nine"
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
def __str__(self):
return "Ten"
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
def __str__(self):
return "Jack"
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
def __str__(self):
return "Queen"
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
def __str__(self):
return "King"
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
def __str__(self):
return "Ace"
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Hearts"
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Clubs"
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Spades"
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Diamonds"
RANKS = {"Two": Two, "Three": Three, "Four": Four, "Five": Five,
"Six": Six, "Seven": Seven, "Eight": Eight, "Nine": Nine,
"Ten": Ten, "Jack": Jack, "Queen": Queen, "King": King, "Ace": Ace}
SUITS = {"Hearts": Hearts, "Clubs": Clubs,
"Spades": Spades, "Diamonds": Diamonds}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
+ def __repr__(self):
+ return "<Card {}>".format(str(self))
+
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __setattr__(self, key, value):
if hasattr(self, key):
- raise AttributeError
+ raise AttributeError("can`t set attribute")
class CardCollection:
def __init__(self, collection=list()):
- self.collection = [card for card in collection
- if isinstance(card, Card)]
+ self.collection = [card for card in collection]
def draw(self, index):
result = self.collection[index]
self.collection.__delitem__(index)
return result
def draw_from_top(self):
index = self.__len__() - 1
result = self.__getitem__(index)
self.collection.__delitem__(index)
return result
def draw_from_bottom(self):
result = self.__getitem__(0)
self.collection.__delitem__(0)
return result
def top_card(self):
return self.__getitem__(self.__len__() - 1)
def bottom_card(self):
return self.__getitem__(0)
def add(self, card):
if isinstance(card, Card):
self.collection.append(card)
def index(self, card):
- for index in range(self.__len__()):
- if self.collection[index] == card:
- return index
- raise ValueError
+ return self.collection.index(card)
def __len__(self):
return len(self.collection)
def __getitem__(self, item):
return self.collection[item]
def __str__(self):
result = "["
for card in self.collection:
- if card == self.collection[len(self.collection)-1]:
- result += "<Card {}>]".format(card)
- else:
- result += "<Card {}>, ".format(card)
+ result +="{}".format(card)
+ if card != self.collection[len(self.collection)-1]:
+ result += ", "
+ result += "]"
return result
-def StandardDeck():
+def custom_deck(index):
result = CardCollection()
for suit in ORDERED_SUITS:
- for rank in ORDERED_RANKS:
- result.add(Card(RANKS[rank], SUITS[suit]))
+ for rank in range(index):
+ result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
+ result.add(Card(RANKS["Ace"], SUITS[suit]))
return result
+def StandardDeck():
+ return custom_deck(12)
+
+
def BeloteDeck():
- result = CardCollection()
- for suit in ORDERED_SUITS:
- for rank in range(7):
- result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
- result.add(Card(RANKS["Ace"], SUITS[suit]))
- return result
+ return custom_deck(7)
def SixtySixDeck():
- result = CardCollection()
- for suit in ORDERED_SUITS:
+ return custom_deck(5)
- for rank in range(5):
- result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
- result.add(Card(RANKS["Ace"], SUITS[suit]))
- return result

Владимир обнови решението на 26.03.2014 16:56 (преди около 10 години)

ORDERED_RANKS = ["King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven",
"Six", "Five", "Four", "Three", "Two", "Ace"]
ORDERED_SUITS = ["Diamonds", "Clubs", "Hearts", "Spades"]
class Rank:
def __init__(self, card_symbol):
self.symbol = card_symbol
def __eq__(self, other):
return self.symbol == other.symbol
class Suit:
def __init__(self, color):
self.color = color
def __eq__(self, other):
return str(self) == str(other)
class Two(Rank):
def __init__(self):
Rank.__init__(self, "2")
def __str__(self):
return "Two"
class Three(Rank):
def __init__(self):
Rank.__init__(self, "3")
def __str__(self):
return "Three"
class Four(Rank):
def __init__(self):
Rank.__init__(self, "4")
def __str__(self):
return "Four"
class Five(Rank):
def __init__(self):
Rank.__init__(self, "5")
def __str__(self):
return "Five"
class Six(Rank):
def __init__(self):
Rank.__init__(self, "6")
def __str__(self):
return "Six"
class Seven(Rank):
def __init__(self):
Rank.__init__(self, "7")
def __str__(self):
return "Seven"
class Eight(Rank):
def __init__(self):
Rank.__init__(self, "8")
def __str__(self):
return "Eight"
class Nine(Rank):
def __init__(self):
Rank.__init__(self, "9")
def __str__(self):
return "Nine"
class Ten(Rank):
def __init__(self):
Rank.__init__(self, "10")
def __str__(self):
return "Ten"
class Jack(Rank):
def __init__(self):
Rank.__init__(self, "J")
def __str__(self):
return "Jack"
class Queen(Rank):
def __init__(self):
Rank.__init__(self, "Q")
def __str__(self):
return "Queen"
class King(Rank):
def __init__(self):
Rank.__init__(self, "K")
def __str__(self):
return "King"
class Ace(Rank):
def __init__(self):
Rank.__init__(self, "A")
def __str__(self):
return "Ace"
class Hearts(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Hearts"
class Clubs(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Clubs"
class Spades(Suit):
def __init__(self):
Suit.__init__(self, "black")
def __str__(self):
return "Spades"
class Diamonds(Suit):
def __init__(self):
Suit.__init__(self, "red")
def __str__(self):
return "Diamonds"
RANKS = {"Two": Two, "Three": Three, "Four": Four, "Five": Five,
"Six": Six, "Seven": Seven, "Eight": Eight, "Nine": Nine,
"Ten": Ten, "Jack": Jack, "Queen": Queen, "King": King, "Ace": Ace}
SUITS = {"Hearts": Hearts, "Clubs": Clubs,
"Spades": Spades, "Diamonds": Diamonds}
class Card:
def __init__(self, rank, suit):
object.__setattr__(self, "rank", rank())
object.__setattr__(self, "suit", suit())
def __str__(self):
return str(self.rank) + " of " + str(self.suit)
- def __repr__(self):
- return "<Card {}>".format(str(self))
-
def __eq__(self, other):
return self.rank == other.rank and self.suit == other.suit
def __setattr__(self, key, value):
if hasattr(self, key):
raise AttributeError("can`t set attribute")
class CardCollection:
def __init__(self, collection=list()):
self.collection = [card for card in collection]
def draw(self, index):
result = self.collection[index]
self.collection.__delitem__(index)
return result
def draw_from_top(self):
index = self.__len__() - 1
result = self.__getitem__(index)
self.collection.__delitem__(index)
return result
def draw_from_bottom(self):
result = self.__getitem__(0)
self.collection.__delitem__(0)
return result
def top_card(self):
return self.__getitem__(self.__len__() - 1)
def bottom_card(self):
return self.__getitem__(0)
def add(self, card):
if isinstance(card, Card):
self.collection.append(card)
def index(self, card):
return self.collection.index(card)
def __len__(self):
return len(self.collection)
def __getitem__(self, item):
return self.collection[item]
def __str__(self):
result = "["
for card in self.collection:
result +="{}".format(card)
if card != self.collection[len(self.collection)-1]:
result += ", "
result += "]"
return result
def custom_deck(index):
result = CardCollection()
for suit in ORDERED_SUITS:
for rank in range(index):
result.add(Card(RANKS[ORDERED_RANKS[rank]], SUITS[suit]))
result.add(Card(RANKS["Ace"], SUITS[suit]))
return result
def StandardDeck():
return custom_deck(12)
def BeloteDeck():
return custom_deck(7)
def SixtySixDeck():
return custom_deck(5)