Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python: Need help modifying my code and add logic to the play _ blackJack module to play a gameof blackjack with 5 players the rules
Python: Need help modifying my code and add logic to the playblackJack module to play a gameof blackjack with players the rules are given by the program stub print statements in the playblackJack function program stub. Run the program for four players to show it works.
This blackjack like game where two players are dealt cards until one player goes over on a deal in which case the other player wins. In the case where the next card is dealt and both players go over then the game is a tie.
MMy code:
import random,sys
try:
shell sysstdout.shell
except AttributeError:
raise RuntimeErroryou must run this program in IDLE"
def main:
# Create a deck of cards.
deck createdeck
# what game do you want to play
gamesAvailable blackjack'poker','none'
gameToPlay
while gameToPlay not in gamesAvailable:
for game in gamesAvailable:
printgame
gameToPlay inputEnter game to play from list above
none means no game or a made up game:
# Get the number of cards to deal.
if gameToPlay "blackjack":
numcards
elif gameToPlay "poker":
numcards
else:
numcards intinputHow many cards per hand should I deal?
# what game do you want to play # Get the number of hands or players
numhands intinputHow many players should I deal?
handList dict for x in rangenumhands #list of # hands empty dictionaries
# Shuffle the deck only works on lists so
l listdeckitems # list of tuples can be sorted
printshuffling deck"
random.shufflel
deck dictl
#printdeck
# Deal the cards.
printdealing
#play the game
if gameToPlay "blackjack":
playblackJackdeck numhands,numcards,handList
elif gameToPlay "poker":
playpokerdeck numhands,numcards,handList
# The createdeck function returns a dictionary
# representing a deck of cards.
def createdeck:
# Create a dictionary with each card and its value
# stored as keyvalue pairs.
deck A:::
:::
:::
:J:
Q:K:
A:::
:::
:::
:J:
Q:K:
A:::
:::
:::
:J:
Q:K:
A:::
:::
:::
:J:
Q:K:
# Return the deck.
return deck
def dealacarddeckhandL,player: #note player index relatve to
#note probably should detect end of deck and reshuffle new deck in Vegas many decks
card,v deck.popitem #take next card off deck
handLplayercardv #put card in player's hand
def printhandhandLplayer:
#print player and card for IDLE
printfplayer player hand:",end
for card in handLplayer:
if in card or in card:
shell.writecard"COMMENT"
else:
shell.writecard"TODO"
print
# The dealcards function deals a specified number of cards
# from the deck.
def dealthehandsdecknHands,nCards,handL:
# Initialize an accumulator for the hand value.
handvalue
# Make sure the number of cards to deal is not
# greater than the number of cards in the deck.
if nHandsnCards lendeck:
printsorry not enough cards"
sysexit
# Deal the cards and accumulate their values.
for countC in rangenCards:
for player in rangenHands:
kv deck.popitem #take next card off deck
handLplayerkv #store card in hand
for player in rangenHands:
print fplayer player hand: end
for card in handLplayer:
if in card or in card:
shell.writecard"COMMENT"
else:
shell.writecard"TODO"
print #newline
# Display the value of the hand. remove
#if gamePlaying "blackjack":
# printfValue of this hand: handvalue
REST OF CODE IN IMAGES CANT FIT
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started