Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm doing a blackjack simulation, and im trying to store data from each time the simulation is run into a file elsewhere and im also

I'm doing a blackjack simulation, and im trying to store data from each time the simulation is run into a file elsewhere and im also trying to perform statistical analysis such as frequency, mean, mode etc of the cards the virtual players in the simulation picked. What would be the solution to this? here is my code

elif Mode == "3": def dealCard(hand, deck): card = random.choice(deck) hand.append(card) deck.remove(card)

def total(hand): total = 0 ace_11s = 0 for card in hand: if card in range(2, 11): total += card elif card in ["J", "K", "Q"]: total += 10 else: total += 11 ace_11s += 1 while ace_11s and total > 21: total -= 10 ace_11s -= 1 return total

def simulate_game(): deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10,"J", "Q", "K", "A", "J", "Q", "K", "A", "J", "Q", "K", "A", "J", "Q", "K", "A"] random.shuffle(deck) player1Hand = 0 player2Hand = 0 dealerHand = 0 dealCard(player1Hand, deck) dealCard(player2Hand, deck) dealCard(dealerHand, deck) dealCard(player1Hand, deck) dealCard(player2Hand, deck) dealCard(dealerHand, deck) player1St = True player2St = True dealerSt = True while player1St or player2St or dealerSt: print(f"Dealer has {dealerHand[0]} and X") print(f"Player 1 has {player1Hand} for a total of {total(player1Hand)} ") print(f"Player 2 has {player2Hand} for a total of {total(player2Hand)} ") if player1St: if total(player1Hand) > 21: player1St = False else: if total(dealerHand) > 16: dealerSt = False else: dealCard(dealerHand, deck) if total(player1Hand) >= 17: player1St = False else: dealCard(player1Hand, deck) if player2St: if total(player2Hand) > 21: player2St = False else: if total(dealerHand) > 16: dealerSt = False else: dealCard(dealerHand, deck) if total(player2Hand) >= 17: player2St = False else: dealCard(player2Hand, deck) if total(dealerHand) > 21: dealerSt = False elif total(dealerHand) >= 17: dealerSt = False if total(player1Hand) > 21: result = "Player 2" elif total(player2Hand) > 21: result = "Player 1" elif total(player1Hand) == total(player2Hand): result = "Draw" elif total(player1Hand) > total(player2Hand): result = "Player 1" else: result = "Player 2" return result

num_games = int(input("How many times will we run this simulation?: ")) results = {"Player 1": 0, "Player 2": 0, "Draw": 0}

for i in range(num_games): result = simulate_game() results[result] += 1

print(f"Player 1 won {results['Player 1']} times.") print(f"Player 2 won {results['Player 2']} times.") print(f"There were {results['Draw']} draws.")

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

Data Management Databases And Organizations

Authors: Watson Watson

5th Edition

0471715360, 978-0471715368

More Books

Students also viewed these Databases questions

Question

virtual reality store

Answered: 1 week ago

Question

8. Measure the effectiveness of the succession planning process.

Answered: 1 week ago