Решение на Тесте карти от Ивайло Бъчваров

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

Към профила на Ивайло Бъчваров

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 12 успешни тест(а)
  • 4 неуспешни тест(а)

Код

class Rank():
def __init__(self, symbol=""):
self.symbol = symbol
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
class Suit():
def __init__(self, color=""):
self.color = color
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
class Card():
def __init__(self, rank, suit):
self.rank = rank(rank.symbol)
self.suit = suit(suit.color)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __repr__(self):
return "Card " + str(self)
def __eq__(self, other):
if self.rank == other.rank and self.suit == other.suit:
return True
else:
return False
def create_rank(name, symbol):
return type(name, (Rank, ), dict(symbol=symbol))
def create_suit(name, color):
return type(name, (Suit,), dict(color=color))
RANKS = {
'Ace': create_rank('Ace', 'A'),
'Two': create_rank('Two', '2'),
'Three': create_rank('Three', '3'),
'Four': create_rank('Four', '4'),
'Five': create_rank('Five', '5'),
'Six': create_rank('Six', '6'),
'Seven': create_rank('Seven', '7'),
'Eight': create_rank('Eight', '8'),
'Nine': create_rank('Nine', '9'),
'Ten': create_rank('Ten', '10'),
'Jack': create_rank('Jack', 'J'),
'Queen': create_rank('Queen', 'Q'),
'King': create_rank('King', 'K'),
}
RANKS_BY_VALUE = [
'King',
'Queen',
'Jack',
'Ten',
'Nine',
'Eight',
'Seven',
'Six',
'Five',
'Four',
'Three',
'Two',
'Ace',
]
SUITS = {
'Hearts': create_suit('Hearts', 'red'),
'Clubs': create_suit('Clubs', 'black'),
'Spades': create_suit('Spades', 'black'),
'Diamonds': create_suit('Diamonds', 'red'),
}
SUIT_BY_VALUE = [
'Diamonds',
'Clubs',
'Hearts',
'Spades',
]
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
return self.collection[index]
def __iter__(self):
return iter(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def index(self, card):
print(self.collection.index(card))
def __len__(self):
return len(self.collection)
def __str__(self):
return str(self.collection)
def create_collection(discarded=[]):
cards = []
for suit in SUIT_BY_VALUE:
for rank in RANKS_BY_VALUE:
if rank in discarded:
break
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
def StandardDeck():
return create_collection()
def BeloteDeck():
discarded = ['Two', 'Three', 'Four', 'Five', 'Six']
return create_collection(discarded)
def SixtySixDeck():
discarded = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
return create_collection(discarded)

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

F..F
Stdout:
2
.FF.........
======================================================================
FAIL: test_belote_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-1aqckup/test.py", line 175, in test_belote_deck
    self.assertEqual(BELOTE_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 7:
Ace of Diamonds
King of Clubs

First list contains 4 additional elements.
First extra element 28:
Nine of Spades

Diff is 752 characters long. Set self.maxDiff to None to see it.

======================================================================
FAIL: test_deck_index (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-1aqckup/test.py", line 160, in test_deck_index
    self.assertEqual(0, deck.index(card1))
AssertionError: 0 != None

Stdout:
2

======================================================================
FAIL: test_deck_order (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-1aqckup/test.py", line 134, in test_deck_order
    self.assertEqual(deck.bottom_card(), card1)
AssertionError: Card Two of Spades != Card King of Clubs

======================================================================
FAIL: test_sixtysix_deck (test.CardCollectionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140407-19315-1aqckup/test.py", line 179, in test_sixtysix_deck
    self.assertEqual(SIXTY_SIX_DECK, cards)
AssertionError: Lists differ: ['King of Diamonds', 'Queen of... != ['King of Diamonds', 'Queen of...

First differing element 5:
Ace of Diamonds
King of Clubs

First list contains 4 additional elements.
First extra element 20:
Jack of Spades

  ['King of Diamonds',
   'Queen of Diamonds',
   'Jack of Diamonds',
   'Ten of Diamonds',
   'Nine of Diamonds',
-  'Ace of Diamonds',
   'King of Clubs',
   'Queen of Clubs',
   'Jack of Clubs',
   'Ten of Clubs',
   'Nine of Clubs',
-  'Ace of Clubs',
   'King of Hearts',
   'Queen of Hearts',
   'Jack of Hearts',
   'Ten of Hearts',
   'Nine of Hearts',
-  'Ace of Hearts',
   'King of Spades',
   'Queen of Spades',
   'Jack of Spades',
   'Ten of Spades',
-  'Nine of Spades',
?                  ^

+  'Nine of Spades']
?                  ^

-  'Ace of Spades']

----------------------------------------------------------------------
Ran 16 tests in 0.041s

FAILED (failures=4)

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

Ивайло обнови решението на 24.03.2014 02:04 (преди над 10 години)

+class Rank():
+ def __init__(self, symbol):
+ self._symbol = symbol
+
+ def __eq__(self, other):
+ return type(self) is type(other)
+
+ def __str__(self):
+ return self._symbol
+
+
+class Suit():
+ def __init__(self, color):
+ self._color = color
+
+ def __eq__(self, other):
+ return type(self) is type(other)
+
+ def __str__(self):
+ return type(self).__name__
+
+
+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, '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, "red")
+
+
+class Clubs(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Spades(Suit):
+ def __init__(self):
+ Suit.__init__(self, "black")
+
+
+class Diamonds(Suit):
+ def __init__(self):
+ Suit.__init__(self, "red")
+
+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
+}
+
+RANKS_BY_VALUE = [
+ 'Ace',
+ 'Two',
+ 'Three',
+ 'Four',
+ 'Five',
+ 'Six',
+ 'Seven',
+ 'Eight',
+ 'Nine',
+ 'Ten',
+ 'Jack',
+ 'Queen',
+ 'King',
+]
+
+SUITS = {
+ 'Hearts': Hearts,
+ 'Clubs': Clubs,
+ 'Spades': Spades,
+ 'Diamonds': Diamonds,
+}
+
+SUIT_BY_VALUE = [
+ 'Diamonds',
+ 'Hearts',
+ 'Spades',
+ 'Clubs',
+]
+
+
+class Card():
+ def __init__(self, Rank, Suit):
+ self.rank = Rank()
+ self.suit = Suit()
+
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
+
+ def __eq__(self, other):
+ if self.rank == other.rank and self.suit == other.suit:
+ return True
+ else:
+ return False
+
+
+class CardCollection():
+ def __init__(self, collection=[]):
+ self.collection = collection
+
+ def add(self, card):
+ self.collection.append(card)
+
+ def __getitem__(self, index):
+ return self.collection[index]
+
+ def __iter__(self):
+ return iter(self.collection)
+
+ def draw(self, index):
+ return self.collection.pop(index)
+
+ def draw_from_top(self):
+ return self.collection.pop()
+
+ def draw_from_bottom(self):
+ return self.collection.pop(0)
+
+ def top_card(self):
+ return self.collection[-1]
+
+ def bottom_card(self):
+ return self.collection[0]
+
+ def index(self, card):
+ print(self.collection.index(card))
+
+ def __len__(self):
+ return len(self.collection)
+
+
+def StandardDeck():
+ cards = []
+ for rank in RANKS_BY_VALUE:
+ for suit in SUIT_BY_VALUE:
+ new_card = Card(RANKS[rank], SUITS[suit])
+ cards.append(new_card)
+
+ return CardCollection(cards)
+
+
+def BeloteDeck():
+ cards = []
+ discarded = ['Two', 'Three', 'Four', 'Five', 'Six']
+
+ for rank in RANKS_BY_VALUE:
+ if rank in discarded:
+ break
+ for suit in SUIT_BY_VALUE:
+ new_card = Card(RANKS[rank], SUITS[suit])
+ cards.append(new_card)
+
+ return CardCollection(cards)
+
+
+def SixtySixDeck():
+ cards = []
+ discarded = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
+
+ for rank in RANKS_BY_VALUE:
+ if rank in discarded:
+ break
+ for suit in SUIT_BY_VALUE:
+ new_card = Card(RANKS[rank], SUITS[suit])
+ cards.append(new_card)
+
+ return CardCollection(cards)

:bear: Имам чувството, че правя нещо много грешно. Даже няколко неща. Не мога да схвана защо SixtySixDeck е написано като клас а пише, че трябва да е функция. (Знам, че имам един и същи код на няколко места, ще го оправя) За всяка suit и type ли трябва да правя отделен клас?

Първо, предаването на код посред нощ е вредно :D.

Ако имаш чувството че правиш нещо грешно като пишеш 15 почти еднакви класа, чувството ти е правилно. Виж за функцията type(). Тя може динамично да създава класове.

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

Ивайло обнови решението на 24.03.2014 13:59 (преди над 10 години)

class Rank():
def __init__(self, symbol):
- self._symbol = symbol
+ self.symbol = symbol
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
- return self._symbol
+ return self.symbol
class Suit():
def __init__(self, color):
- self._color = color
+ self.color = color
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
-class Two(Rank):
- def __init__(self):
- Rank.__init__(self, '2')
+class Card():
+ def __init__(self, Rank, Suit):
+ self.rank = Rank
+ self.suit = Suit
+ def __str__(self):
+ return "{} of {}".format(self.rank, self.suit)
-class Three(Rank):
- def __init__(self):
- Rank.__init__(self, '3')
+ def __eq__(self, other):
+ if self.rank == other.rank and self.suit == other.suit:
+ return True
+ else:
+ return False
-class Four(Rank):
- def __init__(self):
- Rank.__init__(self, '4')
+def create_rank(name, symbol):
+ return type(name, (Rank, ), dict(symbol=symbol))
-class Five(Rank):
- def __init__(self):
- Rank.__init__(self, '5')
+def create_suit(name, color):
+ return type(name, (Suit,), dict(color=color))
-
-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, '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, "red")
-
-
-class Clubs(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
-
-
-class Spades(Suit):
- def __init__(self):
- Suit.__init__(self, "black")
-
-
-class Diamonds(Suit):
- def __init__(self):
- Suit.__init__(self, "red")
-
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
+ 'Ace': create_rank('Ace', 'A'),
+ 'Two': create_rank('Two', '2'),
+ 'Three': create_rank('Three', '3'),
+ 'Four': create_rank('Four', '4'),
+ 'Five': create_rank('Five', '5'),
+ 'Six': create_rank('Six', '6'),
+ 'Seven': create_rank('Seven', '7'),
+ 'Eight': create_rank('Eight', '8'),
+ 'Nine': create_rank('Nine', '9'),
+ 'Ten': create_rank('Ten', '10'),
+ 'Jack': create_rank('Jack', 'J'),
+ 'Queen': create_rank('Queen', 'Q'),
+ 'King': create_rank('King', 'K'),
}
RANKS_BY_VALUE = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Jack',
'Queen',
'King',
]
SUITS = {
- 'Hearts': Hearts,
- 'Clubs': Clubs,
- 'Spades': Spades,
- 'Diamonds': Diamonds,
+ 'Hearts': create_suit('Hearts', 'red'),
+ 'Clubs': create_suit('Clubs', 'black'),
+ 'Spades': create_suit('Spades', 'black'),
+ 'Diamonds': create_suit('Diamonds', 'red'),
}
SUIT_BY_VALUE = [
'Diamonds',
'Hearts',
'Spades',
'Clubs',
]
-class Card():
- def __init__(self, Rank, Suit):
- self.rank = Rank()
- self.suit = Suit()
-
- def __str__(self):
- return "{} of {}".format(self.rank, self.suit)
-
- def __eq__(self, other):
- if self.rank == other.rank and self.suit == other.suit:
- return True
- else:
- return False
-
-
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
return self.collection[index]
def __iter__(self):
return iter(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def index(self, card):
print(self.collection.index(card))
def __len__(self):
return len(self.collection)
def StandardDeck():
cards = []
for rank in RANKS_BY_VALUE:
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
def BeloteDeck():
cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six']
for rank in RANKS_BY_VALUE:
if rank in discarded:
break
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
def SixtySixDeck():
cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
for rank in RANKS_BY_VALUE:
if rank in discarded:
break
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
- return CardCollection(cards)
+ return CardCollection(cards)

Ивайло обнови решението на 24.03.2014 17:50 (преди над 10 години)

class Rank():
- def __init__(self, symbol):
+ def __init__(self, symbol=""):
self.symbol = symbol
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
- return self.symbol
+ return type(self).__name__
class Suit():
- def __init__(self, color):
+ def __init__(self, color=""):
self.color = color
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
class Card():
- def __init__(self, Rank, Suit):
- self.rank = Rank
- self.suit = Suit
+ def __init__(self, rank, suit):
+ self.rank = rank(rank.symbol)
+ self.suit = suit(suit.color)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
def __eq__(self, other):
if self.rank == other.rank and self.suit == other.suit:
return True
else:
return False
def create_rank(name, symbol):
return type(name, (Rank, ), dict(symbol=symbol))
def create_suit(name, color):
return type(name, (Suit,), dict(color=color))
RANKS = {
'Ace': create_rank('Ace', 'A'),
'Two': create_rank('Two', '2'),
'Three': create_rank('Three', '3'),
'Four': create_rank('Four', '4'),
'Five': create_rank('Five', '5'),
'Six': create_rank('Six', '6'),
'Seven': create_rank('Seven', '7'),
'Eight': create_rank('Eight', '8'),
'Nine': create_rank('Nine', '9'),
'Ten': create_rank('Ten', '10'),
'Jack': create_rank('Jack', 'J'),
'Queen': create_rank('Queen', 'Q'),
'King': create_rank('King', 'K'),
}
RANKS_BY_VALUE = [
'Ace',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten',
'Jack',
'Queen',
'King',
]
SUITS = {
'Hearts': create_suit('Hearts', 'red'),
'Clubs': create_suit('Clubs', 'black'),
'Spades': create_suit('Spades', 'black'),
'Diamonds': create_suit('Diamonds', 'red'),
}
SUIT_BY_VALUE = [
'Diamonds',
'Hearts',
'Spades',
'Clubs',
]
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
return self.collection[index]
def __iter__(self):
return iter(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def index(self, card):
print(self.collection.index(card))
def __len__(self):
return len(self.collection)
def StandardDeck():
cards = []
for rank in RANKS_BY_VALUE:
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
def BeloteDeck():
cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six']
for rank in RANKS_BY_VALUE:
if rank in discarded:
break
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
def SixtySixDeck():
cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
for rank in RANKS_BY_VALUE:
if rank in discarded:
break
for suit in SUIT_BY_VALUE:
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)

Ивайло обнови решението на 24.03.2014 19:21 (преди над 10 години)

class Rank():
def __init__(self, symbol=""):
self.symbol = symbol
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
class Suit():
def __init__(self, color=""):
self.color = color
def __eq__(self, other):
return type(self) is type(other)
def __str__(self):
return type(self).__name__
class Card():
def __init__(self, rank, suit):
self.rank = rank(rank.symbol)
self.suit = suit(suit.color)
def __str__(self):
return "{} of {}".format(self.rank, self.suit)
+ def __repr__(self):
+ return "Card " + str(self)
+
def __eq__(self, other):
if self.rank == other.rank and self.suit == other.suit:
return True
else:
return False
def create_rank(name, symbol):
return type(name, (Rank, ), dict(symbol=symbol))
def create_suit(name, color):
return type(name, (Suit,), dict(color=color))
RANKS = {
'Ace': create_rank('Ace', 'A'),
'Two': create_rank('Two', '2'),
'Three': create_rank('Three', '3'),
'Four': create_rank('Four', '4'),
'Five': create_rank('Five', '5'),
'Six': create_rank('Six', '6'),
'Seven': create_rank('Seven', '7'),
'Eight': create_rank('Eight', '8'),
'Nine': create_rank('Nine', '9'),
'Ten': create_rank('Ten', '10'),
'Jack': create_rank('Jack', 'J'),
'Queen': create_rank('Queen', 'Q'),
'King': create_rank('King', 'K'),
}
RANKS_BY_VALUE = [
- 'Ace',
- 'Two',
- 'Three',
- 'Four',
- 'Five',
- 'Six',
- 'Seven',
- 'Eight',
- 'Nine',
- 'Ten',
- 'Jack',
- 'Queen',
'King',
+ 'Queen',
+ 'Jack',
+ 'Ten',
+ 'Nine',
+ 'Eight',
+ 'Seven',
+ 'Six',
+ 'Five',
+ 'Four',
+ 'Three',
+ 'Two',
+ 'Ace',
]
SUITS = {
'Hearts': create_suit('Hearts', 'red'),
'Clubs': create_suit('Clubs', 'black'),
'Spades': create_suit('Spades', 'black'),
'Diamonds': create_suit('Diamonds', 'red'),
}
SUIT_BY_VALUE = [
'Diamonds',
+ 'Clubs',
'Hearts',
'Spades',
- 'Clubs',
]
class CardCollection():
def __init__(self, collection=[]):
self.collection = collection
def add(self, card):
self.collection.append(card)
def __getitem__(self, index):
return self.collection[index]
def __iter__(self):
return iter(self.collection)
def draw(self, index):
return self.collection.pop(index)
def draw_from_top(self):
return self.collection.pop()
def draw_from_bottom(self):
return self.collection.pop(0)
def top_card(self):
return self.collection[-1]
def bottom_card(self):
return self.collection[0]
def index(self, card):
print(self.collection.index(card))
def __len__(self):
return len(self.collection)
-
-def StandardDeck():
+ def __str__(self):
+ return str(self.collection)
+
+
+def create_collection(discarded=[]):
cards = []
- for rank in RANKS_BY_VALUE:
- for suit in SUIT_BY_VALUE:
+ for suit in SUIT_BY_VALUE:
+ for rank in RANKS_BY_VALUE:
+ if rank in discarded:
+ break
new_card = Card(RANKS[rank], SUITS[suit])
cards.append(new_card)
return CardCollection(cards)
+def StandardDeck():
+ return create_collection()
+
+
def BeloteDeck():
- cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six']
+ return create_collection(discarded)
- for rank in RANKS_BY_VALUE:
- if rank in discarded:
- break
- for suit in SUIT_BY_VALUE:
- new_card = Card(RANKS[rank], SUITS[suit])
- cards.append(new_card)
- return CardCollection(cards)
-
-
def SixtySixDeck():
- cards = []
discarded = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']
-
- for rank in RANKS_BY_VALUE:
+ return create_collection(discarded)
- if rank in discarded:
- break
- for suit in SUIT_BY_VALUE:
- new_card = Card(RANKS[rank], SUITS[suit])
- cards.append(new_card)
-
- return CardCollection(cards)