Question
I'm doing a blackjack simulation, and im trying to store data such as the amount of player 1 wins, player 2 wins and draws (listed
I'm doing a blackjack simulation, and im trying to store data such as the amount of player 1 wins, player 2 wins and draws (listed in code below)from each time the simulation is run into a file elsewhere (csv file) and and im also trying to perform statistical analysis like, the most common card number picked, the mean card number picked, .the most common suit picked. What would be the solution to this? here is my code
import random import statistics
Mode = input("Would you like to play singleplayer(1), multplayer(2) or do a simulation(3).(Type number): ")
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 = [] player2Hand = [] dealerHand = [] 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
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