main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python3
  2. ############### Blackjack Project #####################
  3. from os import system
  4. from art import logo
  5. #Difficulty Normal 😎: Use all Hints below to complete the project.
  6. #Difficulty Hard 🤔: Use only Hints 1, 2, 3 to complete the project.
  7. #Difficulty Extra Hard 😭: Only use Hints 1 & 2 to complete the project.
  8. #Difficulty Expert 🤯: Only use Hint 1 to complete the project.
  9. ############### Our Blackjack House Rules #####################
  10. ## The deck is unlimited in size.
  11. ## There are no jokers.
  12. ## The Jack/Queen/King all count as 10.
  13. ## The the Ace can count as 11 or 1.
  14. ## Use the following list as the deck of cards:
  15. ## cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  16. ## The cards in the list have equal probability of being drawn.
  17. ## Cards are not removed from the deck as they are drawn.
  18. ## The computer is the dealer.
  19. ##################### Hints #####################
  20. def calculate_score(cards):
  21. if sum(cards) == 21 and len(cards) == 2:
  22. return 0
  23. if 11 in cards and sum(cards) > 21:
  24. cards.remove(11)
  25. cards.append(1)
  26. return sum(cards)
  27. import random
  28. def deal_cards():
  29. cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  30. card = random.choice(cards)
  31. return card
  32. def compare(user_score, computer_score):
  33. if user_score == computer_score:
  34. return 'Draw '
  35. elif computer_score == 0:
  36. return "Lose, opponent has Blackjack"
  37. elif user_score == 0:
  38. return "Win with a Blackjack"
  39. elif user_score > 21:
  40. return "You went over. You lose"
  41. elif computer_score > 21:
  42. return "Opponent went over. You win"
  43. elif user_score > computer_score:
  44. return "You win!"
  45. else:
  46. return "You lose!"
  47. def play_game():
  48. computer_cards = []
  49. computer_score = 0
  50. user_cards = []
  51. user_score = 0
  52. user_cards = []
  53. computer_cards = []
  54. is_game_over = False
  55. for _ in range(2):
  56. user_cards.append(deal_cards())
  57. computer_cards.append(deal_cards())
  58. system("clear")
  59. print(logo)
  60. while not is_game_over:
  61. computer_score = calculate_score(computer_cards)
  62. user_score = calculate_score(user_cards)
  63. print(f" Your cards: {user_cards}, current score: {user_score}")
  64. print(f" Computer's first card {computer_cards[0]}")
  65. if user_score == 0 or computer_score == 0 or user_score > 21:
  66. is_game_over = True
  67. else:
  68. user_should_deal = input("Type 'y' to get another card, type 'n' to pass: ")
  69. if user_should_deal == "y":
  70. user_cards.append(deal_cards())
  71. else:
  72. is_game_over = True
  73. while computer_score != 0 and computer_score < 17:
  74. computer_cards.append(deal_cards())
  75. computer_score = calculate_score(computer_cards)
  76. result = compare(user_score, computer_score)
  77. print(f"Game over. {result}")
  78. print(f" Your cards: {user_cards}, current score: {user_score}")
  79. print(f" Computer's cards: {computer_cards}, computer's score: {computer_score}")
  80. while input("Do you want to play a game of Blackjack? Type 'y' or 'n': ") == "y":
  81. play_game()