Skip to content

Commit

Permalink
Allow drawing of multiple cards at once
Browse files Browse the repository at this point in the history
  • Loading branch information
hackedhead committed May 31, 2017
1 parent e069077 commit 2748ee2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
14 changes: 12 additions & 2 deletions game/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,18 @@ class Deck:
def __init__(self, cards):
self.cards = cards

def draw(self):
return self.cards.pop()
def draw(self, count):
"""
Draw <count> cards from the deck, or as many as are available.
return a list of cards drawn.
"""
drawn = []
while len(drawn) < count:
try:
drawn.append(self.cards.pop())
except IndexError:
break
return drawn

def shuffle(self):
random.shuffle(self.cards)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ def test_deck_draw_removes_items():
d = Deck(list(range(16)))
assert len(d.cards) == 16
d.shuffle()
d.draw()
assert len(d.cards) == 15
d.draw(4)
assert len(d.cards) == 12


def test_drawing_past_end_of_deck():
d = Deck(list(range(3)))
drawn = d.draw(4)
assert len(d.cards) == 0
assert len(drawn) == 3


def test_deck_add():
Expand Down

0 comments on commit 2748ee2

Please sign in to comment.