Вот мой код:
Код: Выделить всё
import random
def cash_out():
global playerchips
total_value = playerchips[0] * 10 + playerchips[1] * 5 + playerchips[2] * 1
print(f"\nYou are cashing out. You have a total of ${total_value}.")
exit()
print("Welcome to the solo-blackjack casino.py. You will start with $45 dollars in chips.")
player1cards = []
dealercards = []
card = ''
dealt_cards = []
def draw_cards():
global player1cards, dealercards # Use global keyword to modify global variables
for _ in range(2): # Deal two cards to each
card = random.choice(list(cardlist.keys()))
while card in dealt_cards: # Check if the card has already been dealt
card = random.choice(list(cardlist.keys()))
player1cards.append(card)
dealt_cards.append(card) # Add the dealt card to the list of dealt cards
card = random.choice(list(cardlist.keys()))
while card in dealt_cards: # Check if the card has already been dealt
card = random.choice(list(cardlist.keys()))
dealercards.append(card) # Add the dealt card to the list of dealt cards
dealt_cards.append(card)
import sys
aces = 0
import time
dealt_cards=[]
def calculate_hand_value(cards):
value = 0
aces = 0
for card in cards:
card_value = cardlist[card]
if card_value == 11:
aces += 1
value += card_value
while value > 21 and aces:
value -= 10
aces -= 1
return value
def is_soft_17(cards):
# This function checks if the dealer has a "soft 17"
value = calculate_hand_value(cards)
if value == 17:
for card in cards:
if cardlist[card] == 11:
return True
return False
cardlist = {
'2 of Spades': 2,
'2 of Hearts': 2,
'2 of Clubs': 2,
'2 of Diamonds': 2,
'3 of Spades': 3,
'3 of Hearts': 3,
'3 of Clubs': 3,
'3 of Diamonds': 3,
'4 of Spades': 4,
'4 of Hearts': 4,
'4 of Clubs': 4,
'4 of Diamonds': 4,
'5 of Spades': 5,
'5 of Hearts': 5,
'5 of Clubs': 5,
'5 of Diamonds': 5,
'6 of Spades': 6,
'6 of Hearts': 6,
'6 of Clubs': 6,
'6 of Diamonds': 6,
'7 of Spades': 7,
'7 of Hearts': 7,
'7 of Clubs': 7,
'7 of Diamonds': 7,
'8 of Spades': 8,
'8 of Hearts': 8,
'8 of Clubs': 8,
'8 of Diamonds': 8,
'9 of Spades': 9,
'9 of Hearts': 9,
'9 of Clubs': 9,
'9 of Diamonds': 9,
'10 of Spades': 10,
'10 of Hearts': 10,
'10 of Clubs': 10,
'10 of Diamonds': 10,
'Jack of Spades': 10,
'Jack of Hearts': 10,
'Jack of Clubs': 10,
'Jack of Diamonds': 10,
'Queen of Spades': 10,
'Queen of Hearts': 10,
'Queen of Clubs': 10,
'Queen of Diamonds': 10,
'King of Spades': 10,
'King of Hearts': 10,
'King of Clubs': 10,
'King of Diamonds': 10,
'Ace of Spades': 11,
'Ace of Hearts': 11,
'Ace of Clubs': 11,
'Ace of Diamonds': 11
}
# Player starts with $45 in chips: 5 red ($5 each), 10 white ($1 each), 1 green ($10 each)
playerchips = [1, 5, 10]
print("Red chips are worth $5, White chips $1, and green chips $10")
def rungame():
player1cards = []
dealercards = []
draw_cards()
global playerchips
print(f"You have {playerchips[2]} white chips, {playerchips[1]} red chips, and {playerchips[0]} green chips.")
while True:
Chipsbet = input("Type how many chips you would like to put in in the format of green, red, white. Example: 1, 2, 4 would be 1 green, 2 red, and 4 white: ")
Chipsbet = Chipsbet.split(", ")
if len(Chipsbet) != 3:
print("Invalid input. Please enter the number of chips for each color.")
continue
try:
bet_green, bet_red, bet_white = map(int, Chipsbet)
except ValueError:
print("Invalid input. Please enter numeric values.")
continue
if bet_green < 0 or bet_red < 0 or bet_white < 0:
print("Invalid input. Chips cannot be negative.")
continue
total_bet = bet_green * 10 + bet_red * 5 + bet_white * 1
if total_bet == 0:
print("You need to bet at least one chip.")
continue
if bet_green > playerchips[0] or bet_red > playerchips[1] or bet_white > playerchips[2]:
print("You don't have enough chips to make that bet.")
continue
break
# Deduct the bet from the player's chips
playerchips[0] -= bet_green
playerchips[1] -= bet_red
playerchips[2] -= bet_white
# Calculate total bet in dollars
total_bet = bet_green * 10 + bet_red * 5 + bet_white * 1
player1cards = []
dealercards = []
draw_cards()
print(f'''
You have been dealt the cards '{player1cards[0]}' and '{player1cards[1]}'. The dealer has been dealt '{dealercards[0]}' and an unknown card.''')
while True:
hitorout = input("Would you like to 'hit' or 'stand'? ").lower()
if hitorout == 'hit':
dealcard = random.choice(list(cardlist.keys()))
del cardlist[card]
player1cards.append(dealcard)
print(f"You have been dealt '{dealcard}'.")
player_value = calculate_hand_value(player1cards)
print(f"Your current hand value is {player_value}.")
if player_value > 21:
print("Bust! You exceeded 21.")
break
elif hitorout == 'stand':
print("You chose to stand.")
break
else:
print("Invalid input. Please type 'hit' or 'stand'.")
player_value = calculate_hand_value(player1cards)
dealer_value = calculate_hand_value(dealercards)
while dealer_value < 17 or (dealer_value == 17 and is_soft_17(dealercards)):
dealcard = random.choice(list(cardlist.keys()))
del cardlist[card]
dealercards.append(dealcard)
dealer_value = calculate_hand_value(dealercards)
print(f"The dealer currently has the cards:")
for p in range(len(dealercards)):
time.sleep(1)
print(dealercards[p])
time.sleep(1)
print("The dealer has either hit a soft 17 or has a card value under 17. They must draw another card.")
time.sleep(1)
print(f"The dealer has drawn '{dealcard}'. Current dealer value: {dealer_value}")
print("The dealer's final hand is: ")
for y in range(len(dealercards)):
time.sleep(1)
print(dealercards[y])
time.sleep(1.5)
if player_value > 21:
print("You busted. You lose your bet.")
elif dealer_value > 21:
print("The dealer busted. You win!")
playerchips[0] += bet_green * 2
playerchips[1] += bet_red * 2
playerchips[2] += bet_white * 2
elif player_value > dealer_value:
print("You won! You get double the chips you put in.")
playerchips[0] += bet_green * 2
playerchips[1] += bet_red * 2
playerchips[2] += bet_white * 2
elif player_value < dealer_value:
print("You lost. You lose the chips you put in.")
else:
print("It's a tie. Dealer wins, you lose the chips you put in.")
print(f"Your current chip count is: {playerchips[0]} green chips, {playerchips[1]} red chips, and {playerchips[2]} white chips.")
rungame()
while True:
runanother=input("Do you want to 'play again', or 'cash out'? ").lower()
if runanother=='cash out':
cash_out()
elif runanother == 'play again':
rungame()
else:
print("Invalid response.")
bump
Подробнее здесь: https://stackoverflow.com/questions/785 ... ck-project