123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #!/usr/bin/python3
- ############### Blackjack Project #####################
- from os import system
- from art import logo
- #Difficulty Normal 😎: Use all Hints below to complete the project.
- #Difficulty Hard 🤔: Use only Hints 1, 2, 3 to complete the project.
- #Difficulty Extra Hard 😭: Only use Hints 1 & 2 to complete the project.
- #Difficulty Expert 🤯: Only use Hint 1 to complete the project.
- ############### Our Blackjack House Rules #####################
- ## The deck is unlimited in size.
- ## There are no jokers.
- ## The Jack/Queen/King all count as 10.
- ## The the Ace can count as 11 or 1.
- ## Use the following list as the deck of cards:
- ## cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
- ## The cards in the list have equal probability of being drawn.
- ## Cards are not removed from the deck as they are drawn.
- ## The computer is the dealer.
- ##################### Hints #####################
- 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)
- import random
- def deal_cards():
- cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
- card = random.choice(cards)
- return card
- def compare(user_score, computer_score):
- if user_score == computer_score:
- return 'Draw '
- elif computer_score == 0:
- return "Lose, opponent has Blackjack"
- elif user_score == 0:
- return "Win with a Blackjack"
- elif user_score > 21:
- return "You went over. You lose"
- elif computer_score > 21:
- return "Opponent went over. You win"
- elif user_score > computer_score:
- return "You win!"
- else:
- return "You lose!"
- def play_game():
- computer_cards = []
- computer_score = 0
- user_cards = []
- user_score = 0
- user_cards = []
- computer_cards = []
- is_game_over = False
- for _ in range(2):
- user_cards.append(deal_cards())
- computer_cards.append(deal_cards())
- system("clear")
- print(logo)
- while not is_game_over:
- computer_score = calculate_score(computer_cards)
- user_score = calculate_score(user_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:
- user_should_deal = input("Type 'y' to get another card, type 'n' to pass: ")
- if user_should_deal == "y":
- user_cards.append(deal_cards())
- else:
- is_game_over = True
- while computer_score != 0 and computer_score < 17:
- computer_cards.append(deal_cards())
- computer_score = calculate_score(computer_cards)
- result = compare(user_score, computer_score)
- print(f"Game over. {result}")
- print(f" Your cards: {user_cards}, current score: {user_score}")
- print(f" Computer's cards: {computer_cards}, computer's score: {computer_score}")
- while input("Do you want to play a game of Blackjack? Type 'y' or 'n': ") == "y":
- play_game()
|