Python:
import random
from replit import clear
from art import logo
def deal_card():
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
return random.choice(cards)
def calculate_score(cards):
if sum(cards) == 21 and len(cards) == 2:
return 0
if 11 in cards and sum(cards) > 21:
cards.remove(11)
cards.append(1)
return sum(cards)
def compare(user_score,computer_score):
if user_score == computer_score:
print("Drawn!")
elif computer_score == 0:
print("Computer wins with a BlackJack")
elif user_score == 0:
print("User win with a BlackJack")
elif user_score > 21:
print("User loses with over scoring.")
elif computer_score > 21:
print("Computer lose with over scoring. ")
elif computer_score > user_score:
print("Computer win.")
elif computer_score < user_score:
print("User win.")
def play_game():
print(logo)
is_game_over = False
user_cards = []
computer_cards = []
for _ in range(2):
user_cards.append(deal_card())
computer_cards.append(deal_card())
while not is_game_over:
user_score = calculate_score(user_cards)
computer_score = calculate_score(computer_cards)
print(f" Your cards: {user_cards}, current score: {user_score}")
print(f" Computer's first card: {computer_cards[0]}")
if user_score == 0 or computer_score == 0 or user_score >21:
is_game_over = True
else:
continue_deal = input("Press 'y' if you want to deal another card or 'n' to stop: ").lower()
if continue_deal == 'y':
user_cards.append(deal_card())
else:
is_game_over = True
if not (user_score == 0 or computer_score == 0 or user_score >21):
while computer_score < 17:
computer_cards.append(deal_card())
computer_score = calculate_score(computer_cards)
print(f" Your final hand: {user_cards}, final score: {user_score}")
print(f" Computer's final hand: {computer_cards}, final score: {computer_score}")
print(compare(user_score, computer_score))
while input("Do you want to continue playing BlackJack? 'y' ").lower() == 'y':
clear()
play_game()
මේ කෝඩ් එකෙන් None කියලා ප්රින්ට් වෙනවානෙ. කොහෙන්ද හොයාගන්න බෑ.
#Day 12 done
------ Post added on Jul 9, 2021 at 8:13 AM