Question: please write three codes in which 'Your code here' is written. class Player(object): def __init__(self, deck, name): Initialize a Player object. A Player starts the

please write three codes in which 'Your code here' is written.
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
"*** YOUR CODE HERE ***"
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 ***"
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)))
Q3: Making a Player
Now that we have cards, we can make a deck, but we still need players to actually use them. We'll now fill in the implementation of the Player class.
A Player instance has three instance attributes:
name is the player's name. When you play the game, you can enter your name, which will be converted into a string to be passed to the constructor.
deck is an instance of the Deck class. You can draw from it using its .draw() method.
hand is a list of Card instances. Each player should start with 5 cards in their hand, drawn from their deck. Each card in the hand can be selected by its index in the list during the game. When a player draws a new card from the deck, it is added to the end of this list.
Complete the implementation of the constructor for Player so that self.hand is set to a list of 5 cards drawn from the player's deck.
Next, 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.
Call deck.draw() when implementing Player.__init__ and Player.draw. Don't worry about how this function works - leave it all to the abstraction!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!