Day 11 - Blackjack (First project!)


Posted by pei_______ on 2022-04-21

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


版本一: 只看老師的成品製作
版本二: 看完老師給的21點完整規則表和流程圖重製
另外還有老師示範的版本,但前兩版已經花了4個小時沒心力再改了QQ


# Blackjack 版本一

from random import choice
from replit import clear
from art import logo
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def blackjack():
  print (logo)
  your_cards = []
  computer_cards = []

  for card in (0,1):
    computer_cards.append(choice(cards))  

  your_cards.append(choice(cards))
  current_score = your_cards[0]
  computer_score = computer_cards[0]+computer_cards[1]

  adding_card = True

  # 玩家發牌
  while adding_card :
    next_card = choice(cards)
    your_cards.append(next_card)
    current_score += next_card

      # Ace 在超過21點時,從11點置換成1點
    if current_score > 21 and 11 in your_cards:
      your_cards[your_cards.index(11)] = 1
      current_score -= 10

    print(f"Your cards :{your_cards}, current score: {current_score}")
    print(f"computer's first card: {computer_cards[0]}")  

    if computer_cards == [11,10] or computer_cards == [10,11]:
      break
    if your_cards == [11,10] or your_cards == [10,11]:
      break

    if current_score > 21:
      break     

    another_card =input ("\nType ' y' to get another card, type 'n' to pass: ")
    if another_card == "n":
      adding_card = False

    # 電腦發牌
  while computer_score < 16:
    next_card = choice(cards)
    computer_cards.append(next_card)
    computer_score += next_card
      # Ace 在超過21點時,從11點置換成1點
    if computer_score > 21 and 11 in computer_cards:
      computer_cards[computer_cards.index(11)] = 1
      computer_score -= 10

  print(f"\nYour final cards :{your_cards}, final score: {current_score}")
  print(f"Computer's final card: {computer_cards}, final score: {computer_score}")  

  if computer_cards == [11,10] or computer_cards ==[10,11]:
    print ("Lose! Opponent has Blackjack.\n")
  elif your_cards == [11,10] or your_cards ==[10,11]:
     print ("Win! You have Blackjack.\n")   
  elif current_score > 21:
    print ("You went over. You lose.\n")
  elif computer_score > 21:
    print ("Opponent went over. You win.\n")
  elif current_score > computer_score:
    print ("You win.\n")
  elif current_score < computer_score:
    print ("You lose.\n")
  elif current_score == computer_score:
    print ("Draw.\n")

game_playing = True

while game_playing:
  game_start = input("Do you want to play a game of Blackjack? Yype 'y' or 'n': ")

  if game_start == "y":
    clear()
    blackjack()
  else:
    game_playing = False
# Blackjack 版本二

from random import choice
from replit import clear
from art import logo

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
 # 發牌 function
def deal_card(player_got_deal):
  next_card = choice(cards)
  player_got_deal.append(next_card)

# 計分 fuction;並檢視Ace是否要當成1判斷
def calculate_score(player_want_calculate):
  score = sum(player_want_calculate)
  if len(player_want_calculate) < 2 and score == 21 :
    return 0
  if score > 21 and 11 in player_want_calculate:
      player_want_calculate[player_want_calculate.index(11)] = 1
      score -= 10
  return score

# 判斷輸贏 function
def compare(player,computer):
  print(f"\nYour final cards :{player}, final score: {calculate_score(player)}")
  print(f"Computer's final card: {computer}, final score: {calculate_score(computer)}") 
  if calculate_score(computer) == 0 :
    print("Lose! Opponent has Blackjack.\n")
  elif calculate_score(player) >21 :
    print("You went over. You lose.\n")
  elif calculate_score(computer) >21 :
    print("Opponent went over. You win.\n")
  elif calculate_score(player) == calculate_score(computer):
    print("Draw.\n")
  elif calculate_score(player) == 0 :
    print("Win! You have Blackjack.\n")
  elif calculate_score(player) > calculate_score(computer):
    print("You win.\n")
  elif calculate_score(player) < calculate_score(computer):
    print("You lose.\n")

# 遊戲進行 function
def game_start():
  player = []
  computer = []
  print(logo)

  # step1 : 發牌
  for _ in range(2):
    deal_card(player)
    deal_card(computer)

  # step2 : 加牌 (while loop)
  adding_card = True
  while adding_card:
    calculate_score(player) 
    if calculate_score(player) == 0 or calculate_score(computer) == 0:
      break
    if calculate_score(player) >21 or calculate_score(computer) >21 :
      break

    print(f"Your cards :{player}, current score: {calculate_score(player)}")
    print(f"computer's first card: {computer[0]}") 

    another_card = input ("\nType 'y' to get another card, type 'n' to pass: ")
    if another_card == "n":
      adding_card = False
    else:
      deal_card(player)

  # step3: 電腦加牌 (while loop)
  while calculate_score(computer) < 16:
    deal_card(computer)
    calculate_score(computer)

  # step4 : 判斷輸贏
  compare(player,computer)

# main 在以下:
game_playing = True

while game_playing:
  new_game = input("Do you want to play a game of Blackjack? Yype 'y' or 'n': ")
  if new_game == "y":
    clear()
    game_start()
  else:
    game_playing = False

#Python #課堂筆記 #100 Days of Code







Related Posts

[ Vue筆記 ] 計算屬性 computed 的使用

[ Vue筆記 ] 計算屬性 computed 的使用

Python Flask + Keras 建置圖片分類 API

Python Flask + Keras 建置圖片分類 API

[01] 程式設計簡介 - 述句、運算式、運算子

[01] 程式設計簡介 - 述句、運算式、運算子


Comments