class Card (object): cardtype = \'Staff\' class Player(object): def __init__(sel
ID: 3915618 • Letter: C
Question
class Card(object): cardtype = 'Staff'
class Player(object):
def __init__(self, deck, name):
"""Initialize a Player object.
A Player starts the game by drawing 5 cards from their deck. Each turn,
a Player draws another card from the deck and chooses one to play.
>>> test_card = Card('test', 100, 100)
>>> test_deck = Deck([test_card.copy() for _ in range(6)])
>>> test_player = Player(test_deck, 'tester')
>>> len(test_deck.cards)
1
>>> len(test_player.hand)
5
"""
self.deck = deck
self.name = name
implement the draw and play methods in the Player class. The draw method draws a card from the deck and adds it to the player's hand, and the play method removes and returns the card from the player's hand at the given index.
def draw(self):
"""Draw a card from the player's deck and add it to their hand.
>>> test_card = Card('test', 100, 100)
>>> test_deck = Deck([test_card.copy() for _ in range(6)])
>>> test_player = Player(test_deck, 'tester')
>>> test_player.draw()
>>> len(test_deck.cards)
0
>>> len(test_player.hand)
6
"""
assert not self.deck.is_empty(), 'Deck is empty!'
"*** YOUR CODE HERE ***"
! I need help here !
def play(self, card_index):
"""Remove and return a card from the player's hand at the given index.
>>> from cards import *
>>> test_player = Player(standard_deck, 'tester')
>>> test_player.hand = [james, jen, mitas, tammy]
>>> test_player.play(0) is james
True
>>> test_player.play(2) is tammy
True
>>> len(test_player.hand)
2
"""
"*** YOUR CODE HERE ***"
def display_hand(self):
"""
Display the player's current hand to the user.
"""
print('Your hand:')
for card_index, displayed_card in zip(range(len(self.hand)),[str(card) for card in self.hand]):
indent = ' '*(5 - len(str(card_index)))
print(card_index, indent + displayed_card)
def play_random(self):
"""
Play a random card from hand.
"""
return self.play(random.randrange(len(self.hand)))
Explanation / Answer
class Card(object): cardtype = 'Staff' class Player(object): def __init__(self, deck, name): """Initialize a Player object. A Player starts the game by drawing 5 cards from their deck. Each turn, a Player draws another card from the deck and chooses one to play. >>> test_card = Card('test', 100, 100) >>> test_deck = Deck([test_card.copy() for _ in range(6)]) >>> test_player = Player(test_deck, 'tester') >>> len(test_deck.cards) 1 >>> len(test_player.hand) 5 """ self.deck = deck self.name = name self.hand = [deck.draw() for _ in range(5)] def draw(self): """Draw a card from the player's deck and add it to their hand. >>> test_card = Card('test', 100, 100) >>> test_deck = Deck([test_card.copy() for _ in range(6)]) >>> test_player = Player(test_deck, 'tester') >>> test_player.draw() >>> len(test_deck.cards) 0 >>> len(test_player.hand) 6 """ assert not self.deck.is_empty(), 'Deck is empty!' # remove cards from deck and add to hand self.hand.append(self.deck.draw()) def play(self, card_index): """Remove and return a card from the player's hand at the given index. >>> from cards import * >>> test_player = Player(standard_deck, 'tester') >>> test_player.hand = [james, jen, mitas, tammy] >>> test_player.play(0) is james True >>> test_player.play(2) is tammy True >>> len(test_player.hand) 2 """ "*** YOUR CODE HERE ***" # remove card from hand at index return self.hand.pop(card_index) def display_hand(self): """ Display the player's current hand to the user. """ print('Your hand:') for card_index, displayed_card in zip(range(len(self.hand)),[str(card) for card in self.hand]): indent = ' '*(5 - len(str(card_index))) print(card_index, indent + displayed_card) def play_random(self): """ Play a random card from hand. """ return self.play(random.randrange(len(self.hand)))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.