Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
0
Indexable
Never
from art import logo
import random
from replit import clear

def new_game():
  answer = input("Do you want to play a game of Blackjack? Press 'y' or 'n': ")
  if answer == "y":
    clear
    return True
  else:
    return False
  
def hands_showing():
  print(f"Your cards: {players_hand}, current score: {sum(players_hand)}")
  print(f"Computer's first card: {dealers_hand[0]} ")

def final_hand():
    print(f"Your final hand: {players_hand}, final score {sum(players_hand)}")
    print(f"Computer's final hand: {dealers_hand}, final score {sum(dealers_hand)}")

def ace_check(current_hand):
  if sum(current_hand) > 21 and current_hand[-1] == 11:
    current_hand[-1] = 1

start = new_game()

while start:
  cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

  print(logo)
  players_hand = []
  dealers_hand = []

  #dealing first two cards:

  for card in range(2):
    players_hand.append(random.choice(cards))
    dealers_hand.append(random.choice(cards))

  hands_showing()

  if sum(players_hand) == 21:
    print("You got a blackjack! You won!")
  elif sum(dealers_hand) == 21:
    hands_showing()
    print("Computer got a blackjack! You lose!")
  else:
    keep_dealing = True
    while keep_dealing:
      new_card = input("Type 'y' to get another card, type 'n' to pass: ")
      if new_card == 'y':
        players_hand.append(random.choice(cards))  
        ace_check(players_hand)      
        if sum(players_hand) > 21:
          final_hand()
          print("You went over. You lose 😭")
          break
        elif sum(players_hand) == 21:
          final_hand()
          print("You win 😃")
          break
        else:
          hands_showing()
      elif new_card == "n":
        while sum(dealers_hand) < sum(players_hand) or sum (dealers_hand) == sum(players_hand):
          dealers_hand.append(random.choice(cards))
          ace_check(dealers_hand)
        if sum(dealers_hand) > 21 and sum(players_hand) < 21:
          final_hand()
          print("Opponent went over. You win 😁")
          break
        if sum(dealers_hand) < 21 and sum (dealers_hand) > sum(players_hand):
          final_hand()
          print("You lose 😤")
          break
  
  start = new_game()