Question
How do I do a python blackjack simulation where 2 virtual players (computers) play backjack and the results are recorded i have a singleplayer version
How do I do a python blackjack simulation where 2 virtual players (computers) play backjack and the results are recorded i have a singleplayer version done but I'm required to have a simulation also, any help would be appreciated
import random player1St = True player2St = True dealerSt = True 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"]
P1Hand = [] P2Hand = [] dealerhand = [] Mode = input("Would you like to play singleplayer(1), multplayer(2) or do a simulation(3).(Type number): ")
while Mode != "1" or "2" or "3": if Mode == "1": print("We're gonna play singleplayer") def dealCard(turn): card = random.choice(Deck) turn.append(card) Deck.remove(card) def total(hand): total = 0 ace_11s = 0 for card in hand: if card in range(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 revealdealerhand(): if len(dealerhand) == 2: return dealerhand [0] elif len(dealerhand) > 2: return dealerhand[0], dealerhand[1] for _ in range(2): dealCard(dealerhand) dealCard(P1Hand) while player1St or dealerSt: print(f"Dealer has {revealdealerhand()} and X") print(f"You have {P1Hand} for total of {total(P1Hand)} ") if player1St: stayOrHit = input("S: Stay H: Hit ") if total(dealerhand) > 16: dealerSt = False else: dealCard(dealerhand) if stayOrHit == "S": player1St = False else: dealCard(P1Hand) if total(P1Hand) >= 21: break elif total(dealerhand) >= 21: break if total(P1Hand) == 21: print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("You have gotten BlackJack! You are the winner!") elif total(dealerhand) == 21: print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("Blackjack! the Dealer wins!") elif total(P1Hand) > 21: print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("You bust! Dealer wins!") elif total(dealerhand) > 21: print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("Dealer busts! You win!") elif 21 - total(dealerhand) < 21 - total(P1Hand): print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("The dealer wins!") elif 21 - total(P1Hand) < 21 - total(dealerhand): print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("You win!") elif total(dealerhand) == total(P1Hand): print(f" You have {P1Hand} for a total of {total(P1Hand)} and the dealer has {dealerhand} for a total of {total(dealerhand)}") print("it's a draw!") Replay = (input("Would you like to play again(Y/N): ")).lower() if Replay == "n": print ("Thank you for playing") break elif Replay == "y": P1Hand = [] dealerhand = [] Mode = input("Would you like to play single player(1), multiplayer(2) or do a simulation(3).(Type number): ") print (Mode) else: print("Please type Y/N") print(Replay) elif Mode == "3": print("We're going to do a simulation")
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