Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

my blackjack game freezes after bets are made in terminal. I don't know how to correct the bug. Python. Upvote game.py from blackjackgame.cards import Deck

my blackjack game freezes after bets are made in terminal. I don't know how to correct the bug. Python. Upvote

game.py

from blackjackgame.cards import Deck

from blackjackgame.player import Player

import random

class BlackjackGame:

def __init__(self, number_of_players):

self.number_of_players = number_of_players

self.players = {}

self.deck = Deck()

self.cut_card = None

self.dealer_hand = []

self.game_over = False

# Create players

for i in range(self.number_of_players):

name = input(f"Enter name for player {i + 1}: ")

bet = int(input(f"Enter starting bet for {name}: "))

self.players[name] = Player(name, bankroll=10000, bet=bet)

def shuffle_deck(self):

self.deck.shuffle()

def cut_deck(self):

num_cards = len(self.deck.cards)

while num_cards < 81:

self.deck = Deck()

num_cards = len(self.deck.cards)

cut_index = random.randint(60, 80)

self.cut_card = self.deck.cards[cut_index]

self.deck.cards = self.deck.cards[cut_index:] + \

self.deck.cards[:cut_index]

def deal_cards(self):

for i in range(2):

for name, player in self.players.items():

card = self.deck.draw_card()

player.hand.append(card)

print(f"{name} was dealt {card}")

card = self.deck.draw_card()

self.dealer_hand.append(card)

if i == 0:

print(f"Dealer was dealt {card}")

else:

print("Dealer was dealt a face-down card")

def deal_dealer_hand(self):

self.dealer_hand[1] = self.deck.draw_card()

print(f"Dealer's face-down card was {self.dealer_hand[0]}")

print(f"Dealer's face-up card is {self.dealer_hand[1]}")

def check_for_game_over(self):

for name, player in self.players.items():

player_score = self.calculate_score(player.hand)

if player_score > 21:

print(f"{name} busts with {player_score}.")

elif player_score == 21:

print(f"{name} has blackjack!")

else:

while True:

response = input(f"{name}, do you want to hit? (y/n): ")

if response.lower() == "y":

card = self.deck.draw_card()

player.hand.append(card)

print(f"{name} was dealt {card}")

player_score = self.calculate_score(player.hand)

if player_score > 21:

print(f"{name} busts with {player_score}.")

break

elif player_score == 21:

print(f"{name} has blackjack!")

break

elif response.lower() == "n":

print(f"{name} stays with {player_score}.")

break

else:

print("Invalid input. Please enter 'y' or 'n'.")

dealer_score = self.calculate_score(self.dealer_hand)

while dealer_score < 17:

card = self.deck.draw_card()

self.dealer_hand.append(card)

dealer_score = self.calculate_score(self.dealer_hand)

print(f"Dealer hits and is dealt {card}.")

if dealer_score > 21:

print("Dealer busts!")

break

print(f"Dealer stays with {dealer_score}.")

for name, player in self.players.items():

player_score = self.calculate_score(player.hand)

if player_score > 21:

print(f"{name} loses ${player.bet}.")

elif dealer_score > 21 or player_score > dealer_score:

print(f"{name} wins ${player.bet}.")

elif player_score == dealer_score:

print(f"{name} pushes.")

else:

print(f"{name} loses ${player.bet}.")

self.game_over = True

def run(self):

while not self.game_over:

self.shuffle_deck()

self.cut_deck()

self.deal_cards()

self.deal_dealer_hand()

self.check_for_game_over()

blackjack.py

from blackjackgame.game import BlackjackGame

from blackjackgame.cards import Deck

if __name__ == "__main__":

deck = Deck()

deck.reset_deck()

while True:

try:

number_of_players = int(input("Enter number of players (1-4): "))

if 1 <= number_of_players <= 4:

break

else:

print("Number of players must be between 1 and 4.")

except ValueError:

print("Invalid input. Please enter a number between 1 and 4.")

game = BlackjackGame(number_of_players)

game.run()

player.py

class Player:

def __init__(self, name, bankroll=10000, bet=0):

self.name = name

self.bankroll = bankroll

self.hand = []

self.bet = bet

def is_AI(self):

return False

cards.py

import random

class Deck:

suits = ["Clubs", "Diamonds", "Hearts", "Spades"]

ranks = [

"Ace",

"2",

"3",

"4",

"5",

"6",

"7",

"8",

"9",

"10",

"Jack",

"Queen",

"King",

]

def __init__(self):

self.reset_deck()

def reset_deck(self):

self.cards = [(rank, suit) for rank in self.ranks for suit in self.suits]

def shuffle(self):

random.shuffle(self.cards)

def draw_card(self):

return self.cards.pop()

def __repr__(self):

return f"Player({self.name}, {self.bankroll})"

def __str__(self):

return f"{self.name}"

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_2

Step: 3

blur-text-image_3

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

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

When will an acceptance sent by email or fax be effective?

Answered: 1 week ago

Question

What is the central issue of the situation facing the organization?

Answered: 1 week ago

Question

a. What is the purpose of the team?

Answered: 1 week ago

Question

a. How are members selected to join the team?

Answered: 1 week ago

Question

b. What are its goals and objectives?

Answered: 1 week ago