Answered step by step
Verified Expert Solution
Link Copied!

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 play_blackJack module to play a gameof blackjack with 5 players the rules are given by the program stub print statements in the play_blackJack 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 21 on a deal in which case the other player wins. In the case where the next card is dealt and both players go over 21 then the game is a tie.
MMy code:
import random,sys
try:
shell = sys.stdout.shell
except AttributeError:
raise RuntimeError("you must run this program in IDLE")
def main():
# Create a deck of cards.
deck = create_deck()
# what game do you want to play
gamesAvailable =['blackjack','poker','none']
gameToPlay =""
while gameToPlay not in gamesAvailable:
for game in gamesAvailable:
print(game)
gameToPlay= input("Enter 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":
num_cards =2
elif gameToPlay == "poker":
num_cards =5
else:
num_cards = int(input('How many cards per hand should I deal? '))
# what game do you want to play # Get the number of hands or players
num_hands = int(input('How many players should I deal? '))
handList =[dict() for x in range(num_hands)] #list of # hands empty dictionaries
# Shuffle the deck only works on lists so
l = list(deck.items()) # list of tuples can be sorted
print("shuffling deck")
random.shuffle(l)
deck = dict(l)
#print(deck)
# Deal the cards.
print("dealing")
#play the game
if gameToPlay == "blackjack":
play_blackJack(deck, num_hands,num_cards,handList)
elif gameToPlay == "poker":
play_poker(deck, num_hands,num_cards,handList)
# The create_deck function returns a dictionary
# representing a deck of cards.
def create_deck():
# Create a dictionary with each card and its value
# stored as key-value pairs.
deck ={'A':1,'2':2,'3':3,
'4':4,'5':5,'6':6,
'7':7,'8':8,'9':9,
'10':10,'J':10,
'Q':10,'K': 10,
'A':1,'2':2,'3':3,
'4':4,'5':5,'6':6,
'7':7,'8':8,'9':9,
'10':10,'J':10,
'Q':10,'K': 10,
'A':1,'2':2,'3':3,
'4':4,'5':5,'6':6,
'7':7,'8':8,'9':9,
'10':10,'J':10,
'Q':10,'K': 10,
'A':1'2':2,'3':3,
'4':4,'5':5,'6':6,
'7':7,'8':8,'9':9,
'10':10,'J':10,
'Q':10,'K': 10}
# Return the deck.
return deck
def deal_a_card(deck,handL,player): #note player index relatve to 0
#note probably should detect end of deck and reshuffle new deck in Vegas many decks
card,v = deck.popitem() #take next card off deck
handL[player][card]=v #put card in player's hand
def print_hand(handL,player):
#print player and card for IDLE
print(f"player {player+1} hand:",end="")
for card in handL[player]:
if "" in card or "" in card:
shell.write(card+"","COMMENT")
else:
shell.write(card+"","TODO")
print()
# The deal_cards function deals a specified number of cards
# from the deck.
def deal_the_hands(deck,nHands,nCards,handL):
# Initialize an accumulator for the hand value.
hand_value =0
# Make sure the number of cards to deal is not
# greater than the number of cards in the deck.
if nHands*nCards > len(deck):
print("sorry not enough cards")
sys.exit(0)
# Deal the cards and accumulate their values.
for countC in range(nCards):
for player in range(nHands):
k,v = deck.popitem() #take next card off deck
handL[player][k]=v #store card in hand
for player in range(nHands):
print (f"player {player+1} hand: ",end="")
for card in handL[player]:
if "" in card or "" in card:
shell.write(card+"","COMMENT")
else:
shell.write(card+"","TODO")
print() #newline
# Display the value of the hand. remove
#if gamePlaying == "blackjack":
# print(f'Value of this hand: {hand_value}')
[[[[[[[REST OF CODE IN IMAGES CANT FIT]]]]]]]]]]
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

How can emotions cause communication breakdown?

Answered: 1 week ago