def reset(self) -> CardPile: if len(self) < 1: raise IllegalMove( 'DiscardPile() must have at least 1 Card to use .reset()') cards = self.cards self.cards = [] return CardPile(*cards)
def deal(self, num: int = 1): '''returns cardpile taken from deck''' if not isinstance(num, int): raise TypeError( 'Deck.deal() num argument must be an integer, not a ' + str(type(num))) cards = CardPile() while num > 0: card = self.take() cards.add(card) num -= 1 return cards
def take(self, n: int = 1) -> CardPile: if not isinstance(n, int): raise TypeError( 'DiscardPile.take() n argument must be an integer, not a ' + str(type(n))) if n > len(self): raise IndexError('Can only .take() up to ' + str(len(self)) + ' cards, not ' + str(n)) cards = CardPile() for i in range(n): cards.add(self.topCard()) self.cards.pop() return cards
def take(self, n: int = 1): if not isinstance(n, int): raise TypeError( 'BulidingPile.take() n argument must be an integer, not a ' + str(type(n))) if len(self) < 1: raise IllegalMove('an empty build pile cannot be taken from') num = 0 for card in self: if not card.facedown: num += 1 if n > num: raise IllegalMove( 'only faceup cards may be taken from a build pile') cards = CardPile() for i in range(n): cards.add(self.topCard(), False) self.cards.pop() if len(self) > 0 and self.topCard().facedown: self.topCard().flip() return cards
def __repr__(self) -> str: display = 'DiscardPile(' if len(self) > 0: for card in self.cards: display += repr(card) + ',' return display[0:-1] + ')' else: return display + ')' assert len(DiscardPile()) == 0 assert len(DiscardPile(('spade', 'ace'))) == 1 assert DiscardPile(('diamond', 'eight'), ('club', 'jack')).topCard() == Card('club', 'jack', False) assert DiscardPile(('spade', 'three'), ('heart', 'king')).reset() == CardPile( ('spade', 'three', False), ('heart', 'king', False)) assert str(DiscardPile()) == '[]' assert repr(DiscardPile()) == 'DiscardPile()' assert str(DiscardPile(('diamond', 'six'), ('club', 'ace'))) == 'cA' assert repr( DiscardPile(('spade', 'two'), ('spade', 'queen')) ) == 'DiscardPile(Card(Suit(\'spade\'),Rank(\'two\'),False),Card(Suit(\'spade\'),Rank(\'queen\'),False))' assert DiscardPile().stack( ('diamond', 'four'), ('heart', 'jack')) == DiscardPile(('diamond', 'four'), ('heart', 'jack')) assert error_test.expect('DiscardPile().reset()', IllegalMove, global_variables=globals()) assert error_test.expect('DiscardPile().stack()', IndexError, global_variables=globals())
'building piles must stack cards in decending ranks') self.add(card) return self assert BuildingPile( ('spade', 'king', False)).stack(Card('heart', 'queen', False)) == BuildingPile( ('spade', 'king', False), ('heart', 'queen', False)) assert BuildingPile(('diamond', 'six'), ('heart', 'eight'), ('club', 'ace')).topCard() == Card('club', 'ace') assert error_test.expect( 'BuildingPile(("heart","king",False)).stack(Card("diamond","queen",False))', IllegalMove, global_variables=globals()) assert error_test.expect( 'BuildingPile(("spade","king",False)).stack(Card("diamond","three",False))', IllegalMove, global_variables=globals()) assert BuildingPile(('spade', 'three'), ('spade', 'king', False), ('heart', 'queen', False)).take(2) == CardPile( ('spade', 'king', False), ('heart', 'queen', False)) assert error_test.expect( "BuildingPile(('diamond','four'),('club','ace'),('club','ten',False)).take(2)", IllegalMove, global_variables=globals()) assert error_test.expect("BuildingPile().take()", IllegalMove, global_variables=globals())
Card('club', 'seven'), Card('club', 'eight'), Card('club', 'nine'), Card('club', 'ten'), Card('club', 'jack'), Card('club', 'queen'), Card('club', 'king'), Card('diamond', 'ace'), Card('diamond', 'two'), Card('diamond', 'three'), Card('diamond', 'four'), Card('diamond', 'five'), Card('diamond', 'six'), Card('diamond', 'seven'), Card('diamond', 'eight'), Card('diamond', 'nine'), Card('diamond', 'ten'), Card('diamond', 'jack'), Card('diamond', 'queen'), Card('diamond', 'king') ] assert Deck().deal() == CardPile(('diamond', 'king')) assert Deck().deal(3) == CardPile(('diamond', 'king'), ('diamond', 'queen'), ('diamond', 'jack')) assert random_test.possibleShuffle(Deck().cards, Deck().shuffle().cards) assert repr( Deck((('spade', 'ace'), ('heart', 'two'))) ) == 'Deck(Card(Suit(\'spade\'),Rank(\'ace\'),True),Card(Suit(\'heart\'),Rank(\'two\'),True))' assert str(Deck()) == '**' assert str(Deck([])) == '[]'