Question

Player Class Represents a participant in the game of blackjack.

Must meet the following requirements: Stores the individual cards that are dealt (i.e. cannot just store the sum of the cards, you need to track which specific cards you receive) Provided methods: decide_hit(self): decides whether hit or stand by randomly selecting one of the two options.

Parameters: None

Returns: True to indicate a hit, False to indicate a stand

Must implement the following methods:

Hi!! i just need help with the player class for this. Ive added comments into my python where I need help with codes so if you could help with the red that would be great!

import random class Player: def __init__(self, name, dealer): #creates a new player with the given name, and initializes win,def play_round(self): >>> from dealer import Dealer >>> import random; random.seed(1) >>> dealer = Dealer) >>> dealer. shuffl@property def wins (self): #Cproperty method): returns the number of wins for this player return None @property def ties(selfdef reset_stats(self): >>> player = Player (None, None) >>> player.record_tie >>> player.record_loss >>> player.record_wino >

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Working code implemented in Python and appropriate comments provided for better understanding:

Here I am attaching code for these 4 files:

  • player.py
  • dealer.py
  • card_deck.py
  • blackjack_game.py

Source code for player.py:

import random


class Player:

def __init__(self, name, dealer):
self.name = name
self.dealer = dealer
self.hand = []

self.__wins = 0
self.__ties = 0
self.__losses = 0
self.busted = False

def decide_hit(self):
# DO NOT MODIFY
return random.choice([True, True, False])

def deal_to(self, card_value):
"""
>>> player = Player(None, None)
>>> player.deal_to(11)
>>> player.hand
[11]
>>> player.deal_to(10)
>>> player.hand
[11, 10]
"""
self.hand.append(card_value)
if self.card_sum > 21:
self.busted = True

@property
def card_sum(self):
"""
>>> player = Player(None, None)
>>> player.deal_to(2)
>>> player.card_sum
2
>>> player.deal_to(10)
>>> player.card_sum
12
"""
return sum(self.hand)

def play_round(self):
"""
>>> from dealer import Dealer
>>> import random; random.seed(1)
>>> dealer = Dealer()
>>> dealer.shuffle_deck()
>>> player = Player(None, dealer)

We see that after the first call the play_round, we have only hit once as dictated by decide_hit
>>> player.play_round()
>>> player.hand
[10]

After calling play_round again, decide_hit has decided to stand
>>> player.play_round()
>>> player.hand
[10]

After a third call to play_round, we've decided to hit, but busted!
>>> player.play_round()
>>> player.hand
[10, 8, 10]

After a final call to play_round, our hand shouldn't change since we've already busted
>>> player.play_round()
>>> player.hand
[10, 8, 10]
"""
while not self.busted and self.decide_hit():
self.dealer.signal_hit(self)

def discard_hand(self):
"""
>>> player = Player(None, None)
>>> player.deal_to(11)
>>> player.deal_to(5)
>>> player.discard_hand()
>>> player.hand
[]
"""
self.busted = False
self.hand = []

@property
def wins(self):
return self.__wins

@property
def ties(self):
return self.__ties

@property
def losses(self):
return self.__losses

def record_win(self):
"""
>>> player = Player(None, None)
>>> player.record_win()
>>> player.wins
1
"""
self.__wins += 1

def record_loss(self):
"""
>>> player = Player(None, None)
>>> player.record_loss()
>>> player.losses
1
"""
self.__losses += 1

def record_tie(self):
"""
>>> player = Player(None, None)
>>> player.record_tie()
>>> player.ties
1
"""
self.__ties += 1

def reset_stats(self):
"""
>>> player = Player(None, None)
>>> player.record_tie()
>>> player.record_loss()
>>> player.record_win()
>>> player.reset_stats()
>>> player.ties
0
>>> player.wins
0
>>> player.losses
0
"""
self.__ties = self.__wins = self.__losses = 0

def __repr__(self):
"""
Output should include the player name, their current hand, and their wins/ties/losses in that order
>>> player = Player("Eric", None)
>>> player.record_loss()
>>> player.record_win()
>>> player.record_win()
>>> player
Eric: [] 2/0/1
"""

return f"{self.name}: {self.hand} {self.wins}/{self.ties}/{self.losses}"


if __name__ == "__main__":
import doctest
doctest.testmod()

Code Screenshots:

import random class Player: def __init_(self, name, dealer): self.name = name self.dealer = dealer self.hand = [] self. _wins>>> player = Player(None, None) >>> player.record_tie >>> player.ties 124 125 126 self._ties += 1 128 129 130 def reset_stats

Source code for dealer.py:

from player import Player
from card_deck import CardDeck

class Dealer(Player):

def __init__(self):
super().__init__("Dealer", None)
self.deck = CardDeck()

def shuffle_deck(self):
"""
>>> import random; random.seed(1)
>>> dealer = Dealer()
>>> dealer.shuffle_deck()
>>> str(dealer.deck)[0:20]
'10 8 10 6 8 9 3 10 2'
"""
self.deck.shuffle()

def signal_hit(self, player):
"""
A method called by players when they want to hit
Player objects should pass their `self` references to this method
Should deal one card to the player that signalled a hit

These doctests will not run properly if the `deal_to` method
in the `Player` class is not properly implemented

>>> import random; random.seed(1)
>>> dealer = Dealer()
>>> dealer.shuffle_deck()
>>> player = Player(None, None)
>>> dealer.signal_hit(player)
>>> player.hand
[10]
"""
card = self.deck.draw()
if card:
player.deal_to(card)

def play_round(self):
"""
A dealer should hit if his hand totals to 16 or less

>>> import random; random.seed(1)
>>> dealer = Dealer()
>>> dealer.shuffle_deck()
>>> dealer.play_round()
>>> dealer.hand
[10, 8]
"""
while not self.busted and self.card_sum <= 16:
self.signal_hit(self)

if __name__ == "__main__":
import doctest
doctest.testmod()

Code Screenshots:

from player import Player from card_deck import CardDeck class Dealer (PLayer): def init__(self): super(). __init_(Dealer,

Source code for card_deck.py:

import random
class CardDeck:
class Card:
def __init__(self, value):
self.value = value
self.next = None

def __repr__(self):
return "{}".format(self.value)

def __init__(self):
self.top = None

def shuffle(self):
card_list = 4 * [x for x in range(2, 12)] + 12 * [10]
random.shuffle(card_list)

self.top = None

for card in card_list:
new_card = self.Card(card)
new_card.next = self.top
self.top = new_card

def __repr__(self):
curr = self.top
out = ""
card_list = []
while curr is not None:
card_list.append(str(curr.value))
curr = curr.next
return " ".join(card_list)

def draw(self):
"""
>>> import random; random.seed(1)
>>> deck = CardDeck()
>>> deck.shuffle()
>>> deck.draw()
10
>>> deck.draw()
8
>>> deck.draw()
10
>>> deck.draw()
6
>>> deck
8 9 3 10 2 10 6 5 8 10 3 10 9 2 10 9 6 10 5 5 2 6 8 7 2 4 10 4 11 10 3 10 10 5 7 10 10 11 7 7 3 11 10 4 4 9 11 10
"""

if self.top is not None:
node = self.top
self.top = self.top.next
return node.value
else:
return None

if __name__ == "__main__":
import doctest
doctest.testmod()

Code Screenshots:1 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def repr__(self):

Source code for blackjack_game.py:

from player import Player
from dealer import Dealer


class BlackjackGame:
def __init__(self, player_names):
self.dealer = Dealer()
self.player_list = [Player(name, self.dealer) for name in player_names]

def play_rounds(self, num_rounds=1):
"""
>>> import random; random.seed(1)
>>> game = BlackjackGame(["Lawrence","Melissa"])
>>> print(game.play_rounds(2))
Round 1
Dealer: [10, 9] 0/0/0
Lawrence: [10, 6, 3] 0/1/0
Melissa: [8, 8] 0/0/1
Round 2
Dealer: [10, 10] 0/0/0
Lawrence: [10, 3] 0/1/1
Melissa: [9, 10] 0/0/2
"""
string = ""
for i in range(0, num_rounds):
self.dealer.hand = []
for p in self.player_list:
p.hand = []
self.dealer.shuffle_deck()
# deal first two cards
for _ in [1, 2]:
for p in self.player_list:
self.dealer.signal_hit(p)
self.dealer.signal_hit(self.dealer)

# play out round
for p in self.player_list:
p.play_round()
self.dealer.play_round()

# score round
if sum(self.dealer.hand[:2]) == 21:
for p in self.player_list:
if sum(p.hand[:2]) == 21:
p.record_tie()
else:
p.record_loss()
elif self.dealer.busted:
for p in self.player_list:
p.record_win()
else:
for p in self.player_list:
if sum(p.hand[:2]) == 21:
p.record_win()
elif p.busted or p.card_sum < self.dealer.card_sum:
p.record_loss()
elif p.card_sum > self.dealer.card_sum:
p.record_win()
elif p.card_sum == self.dealer.card_sum:
p.record_tie()

# build report
string += f"Round {i + 1}\n"
string += str(self.dealer) + "\n"
for p in self.player_list:
string += str(p) + "\n"
return string[0:-1]

def reset_game(self):
"""
>>> game = BlackjackGame(["Lawrence", "Melissa"])
>>> _ = game.play_rounds()
>>> game.reset_game()
>>> game.player_list[0]
Lawrence: [] 0/0/0
>>> game.player_list[1]
Melissa: [] 0/0/0
"""
self.dealer = Dealer()
self.player_list = [Player(p.name, self.dealer) for p in self.player_list]

if __name__ == "__main__":
import doctest
doctest.testmod()

Code Screenshots:

1 2 from player import Player from dealer import Dealer class BlackjackGame: def __init__(self, player_names): self.dealer =# build report string += FRound {i + 1}\n string += str(self.dealer) + \n for p in self.player_list: = str() + \n retur

Output Screenshots:

? player = Player (None, None) > player.deal_to (11) player.hand [11] > player.deal_to (10) > player.hand [11, 10] player = P> player = Player (None, None) > player.record win() > player.wins > player = Player (None, None) > player.record_loss () pla

Hope it helps, if you like the answer give it thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Player Class Represents a participant in the game of blackjack. Must meet the following requirements: Stores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

    Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program. 1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand. 2) Ask...

  • Using C++ Create a Blackjack program with the following features: Single player game against the dealer....

    Using C++ Create a Blackjack program with the following features: Single player game against the dealer. Numbered cards count as the number they represent (example: 2 of any suit counts as 2) except the Aces which can count as either 1 or 11, at the discretion of the player holding the card. Face cards count as 10. Player starts with $500. Player places bet before being dealt a card in a new game. New game: Deal 2 cards to each...

  • Need a blackjack code for my assignment its due in 3 hours: it has to include...

    Need a blackjack code for my assignment its due in 3 hours: it has to include classes and object C++. Create a fully functioning Blackjack game in three separate phases. A text based version with no graphics, a text based object oriented version and lastly an object oriented 2D graphical version. Credits (money) is kept track of throughout the game by placing a number next to the player name. Everything shown to the user will be in plain text. No...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • How do I start this c++ code homework? Write a code in C++ for a BlackJack...

    How do I start this c++ code homework? Write a code in C++ for a BlackJack card game using the following simplified rules: Each card has a numerical value. Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on) An Ace count as either 1 point or 11 points (whichever suits the player best) Jack, queen and king count 10 points each The player will compete against the computer which represents...

  • 5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may...

    5.12 A5 Program Do you want to play. a. game? The Big Bang Theory fans may recognize Rock Paper Scissors Lizard Spock as a game of chance that expands on the standard Rock Paper Scissors game. It introduces two new hand signs and several more rules. The rules: • Scissors cuts Paper • Paper covers Rock • Rock crushes Lizard • Lizard poisons Spock • Spock smashes Scissors • Scissors decapitates Lizard • Lizard eats Paper • Paper disproves Spock...

  • [JS] [HTML] wanting to add this logic to existing code(see below) of a blackjack game, where...

    [JS] [HTML] wanting to add this logic to existing code(see below) of a blackjack game, where it will show the Suit emblem instead of spelled out suit name. example: display 7 ♣ instead of 7 clubs. //this is the logic, main obj is to display suits emblems to existing code if (card.Suit == 'Hearts') icon='&hearts;'; else if (card.Suit == 'Spades') icon = '&spades;'; else if (card.Suit == 'Diamonds') icon = '&diams;'; else icon = '&clubs;'; //existing code blackjack.js //variables for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT