Skip to content

Commit

Permalink
Extended discard functionality
Browse files Browse the repository at this point in the history
Extended the __init__ method and adjusted some other methods to allow the user to specify another Deck object to act as the discard pile.

Helpful if you want to be able to manipulate the discard pile without accessing the _discard_pile, whilst maintaining the functionality shuffle_back(), discarded, etc.

Also added a unit test for this new functionality.
  • Loading branch information
n-Holmes committed Oct 20, 2018
1 parent c9c7804 commit 8ed22e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pyCardDeck/deck.py
Expand Up @@ -27,9 +27,11 @@ class Deck:
:type reshuffle: bool
:param name: Name of the deck, used when converting the Deck instance into string
:type name: string
:param discard: optional Deck object to use as discard pile
:type discard: Union[Deck, None]
"""

def __init__(self, cards: object = None, reshuffle: object = True, name: object = None):
def __init__(self, cards: object = None, reshuffle: object = True, name: object = None, discard: Union['Deck', None] = None):
"""
Create the deck
"""
Expand All @@ -38,7 +40,10 @@ def __init__(self, cards: object = None, reshuffle: object = True, name: object
self._cards = cards
if self._cards is None:
self._cards = []
self._discard_pile = []
if discard is None:
self._discard_pile = []
else:
self._discard_pile = discard
self._reshuffle = reshuffle
self._save_location = None

Expand Down Expand Up @@ -198,7 +203,10 @@ def shuffle_back(self) -> object:
for card in self._discard_pile:
self._cards.append(card)
self.shuffle()
self._discard_pile = []
if isinstance(self._discard_pile, Deck):
self._discard_pile.clear()
else:
self._discard_pile = []
log.debug('Cards have been shuffled back from the discard pile')

def discard(self, card: CardType) -> None:
Expand All @@ -211,7 +219,10 @@ def discard(self, card: CardType) -> None:
"""
log.debug("Card being discarded: %s", card)
if card or type(card) == int:
self._discard_pile.append(card)
if isinstance(self._discard_pile, Deck):
self._discard_pile.add_single(card, 0)
else:
self._discard_pile.append(card)
log.debug('Card %s discarded', card)
# This had a reason, I remember testing something and ending
# up with False/None in discard_pile - if anyone knows what
Expand Down
17 changes: 17 additions & 0 deletions tests/test_deck.py
Expand Up @@ -170,6 +170,23 @@ def test_discard():
assert d.discarded == 3


def test_deck_discard():
cardlist = [
Card('One'), Card('Two'), Card('Three'), Card('Four')
]
discardpile = Deck(reshuffle=False)
d = Deck(cards=cardlist[:], reshuffle=False, discard=discardpile)
d.discard(d.draw())
d.discard(d.draw())
assert d.discarded == 2
assert len(discardpile) == 2
assert discardpile[1] == cardlist[0]
d.shuffle_back()
assert d.discarded == 0
assert len(d) == 4
assert len(discardpile) == 0


def test_shuffle_back():
d = Deck(cards=[
Card('One'), Card('Two'), Card('Three'), Card('Four')
Expand Down

0 comments on commit 8ed22e9

Please sign in to comment.